public class BurritoInventory {
// Protect the constructor, so no other class can call it
private BurritoInventory() { }
// Create the only instance, save it to a private static variable
private static BurritoInventory instance = new BurritoInventory();
// Make the static instance publicly available
public static BurritoInventory getInstance() { return instance; }
// How many "servings" of each item do we have?
private int cheese = 0;
private int rice = 0;
private int beans = 0;
private int chicken = 0;
// Add to the inventory
public void addCheese(int added) { cheese += added; }
public void addRice(int added) { rice += added; }
public void addBeans(int added) { beans += added; }
public void addChicken(int added) { chicken += added; }
// Called when it's time to make a burrito.
// Returns true if there are enough ingredients to make the burrito,
// false if not. Decrements the ingredient count when there are enough.
synchronized public boolean makeBurrito() {
// Burritos require one serving of each item
if (cheese > 0 && rice > 0 && beans > 0 && chicken > 0) {
cheese--; rice--; beans--; chicken--;
return true; // can make the burrito
}
else {
// Could order more ingredients
return false; // cannot make the burrito
}
}
}
|