I am trying to cover the protected synchronized boolean enoughIngredients(Recipe r) method in
my inventoryTest file. But it doesn't reach the code. I tried making a child class in my
InventoryTest class extending the Inventory class and overrriding the method in the Inventory
class using a public modifier, but it still wouldn't work. I tried changing the access modifier of
enoughIngredients() to public as a test in the Inventory class but it still didn't work. Why aren't I
reaching the code? Do it have something to do with boolean as the method type? Thank you
public class Inventory {
private static int coffee;
private static int milk;
private static int sugar;
private static int chocolate;
public Inventory() {
setCoffee(15);
setMilk(15);
setSugar(15);
setChocolate(15);
}
public int getChocolate() {
return chocolate;
}
public synchronized void setChocolate(int chocolate) {
if (chocolate >= 0) {
Inventory.chocolate = chocolate;
}
}
public synchronized void addChocolate(String chocolate) throws InventoryException {
int amtChocolate = 0;
try {
amtChocolate = Integer.parseInt(chocolate);
} catch (NumberFormatException e) {
throw new InventoryException("Units of chocolate must be a positive integer");
}
if (amtChocolate >= 0) {
Inventory.chocolate += amtChocolate;
} else {
throw new InventoryException("Units of chocolate must be a positive integer");
}
}
public int getCoffee() {
return coffee;
}
public synchronized void setCoffee(int coffee) {
if (coffee >= 0) {
Inventory.coffee = coffee;
}
}
public synchronized void addCoffee(String coffee) throws InventoryException {
int amtCoffee = 0;
try {
amtCoffee = Integer.parseInt(coffee);
} catch (NumberFormatException e) {
throw new InventoryException("Units of coffee must be a positive integer");
}
if (amtCoffee >= 0) {
Inventory.coffee += amtCoffee;
} else {
throw new InventoryException("Units of coffee must be a positive integer");
}
}
public int getMilk() {
return milk;
}
public synchronized void setMilk(int milk) {
if (milk >= 0) {
Inventory.milk = milk;
}
}
public synchronized void addMilk(String milk) throws InventoryException {
int amtMilk = 0;
try {
amtMilk = Integer.parseInt(milk);
} catch (NumberFormatException e) {
throw new InventoryException("Units of milk must be a positive integer");
}
if (amtMilk >= 0) {
Inventory.milk += amtMilk;
} else {
throw new InventoryException("Units of milk must be a positive integer");
}
}
public int getSugar() {
return sugar;
}
public synchronized void setSugar(int sugar) {
if (sugar >= 0) {
Inventory.sugar = sugar;
}
}
public synchronized void addSugar(String sugar) throws InventoryException {
int amtSugar = 0;
try {
amtSugar = Integer.parseInt(sugar);
} catch (NumberFormatException e) {
throw new InventoryException("Units of sugar must be a positive integer");
}
if (amtSugar >= 0) {
Inventory.sugar += amtSugar;
} else {
throw new InventoryException("Units of sugar must be a positive integer");
}
}
protected synchronized boolean enoughIngredients(Recipe r) {
.
I am trying to cover the protected synchronized boolean enoughIngred.pdf
1. I am trying to cover the protected synchronized boolean enoughIngredients(Recipe r) method in
my inventoryTest file. But it doesn't reach the code. I tried making a child class in my
InventoryTest class extending the Inventory class and overrriding the method in the Inventory
class using a public modifier, but it still wouldn't work. I tried changing the access modifier of
enoughIngredients() to public as a test in the Inventory class but it still didn't work. Why aren't I
reaching the code? Do it have something to do with boolean as the method type? Thank you
public class Inventory {
private static int coffee;
private static int milk;
private static int sugar;
private static int chocolate;
public Inventory() {
setCoffee(15);
setMilk(15);
setSugar(15);
setChocolate(15);
}
public int getChocolate() {
return chocolate;
}
public synchronized void setChocolate(int chocolate) {
if (chocolate >= 0) {
Inventory.chocolate = chocolate;
}
}
2. public synchronized void addChocolate(String chocolate) throws InventoryException {
int amtChocolate = 0;
try {
amtChocolate = Integer.parseInt(chocolate);
} catch (NumberFormatException e) {
throw new InventoryException("Units of chocolate must be a positive integer");
}
if (amtChocolate >= 0) {
Inventory.chocolate += amtChocolate;
} else {
throw new InventoryException("Units of chocolate must be a positive integer");
}
}
public int getCoffee() {
return coffee;
}
public synchronized void setCoffee(int coffee) {
if (coffee >= 0) {
Inventory.coffee = coffee;
}
}
public synchronized void addCoffee(String coffee) throws InventoryException {
int amtCoffee = 0;
try {
amtCoffee = Integer.parseInt(coffee);
} catch (NumberFormatException e) {
throw new InventoryException("Units of coffee must be a positive integer");
}
if (amtCoffee >= 0) {
Inventory.coffee += amtCoffee;
} else {
3. throw new InventoryException("Units of coffee must be a positive integer");
}
}
public int getMilk() {
return milk;
}
public synchronized void setMilk(int milk) {
if (milk >= 0) {
Inventory.milk = milk;
}
}
public synchronized void addMilk(String milk) throws InventoryException {
int amtMilk = 0;
try {
amtMilk = Integer.parseInt(milk);
} catch (NumberFormatException e) {
throw new InventoryException("Units of milk must be a positive integer");
}
if (amtMilk >= 0) {
Inventory.milk += amtMilk;
} else {
throw new InventoryException("Units of milk must be a positive integer");
}
}
public int getSugar() {
return sugar;
}
4. public synchronized void setSugar(int sugar) {
if (sugar >= 0) {
Inventory.sugar = sugar;
}
}
public synchronized void addSugar(String sugar) throws InventoryException {
int amtSugar = 0;
try {
amtSugar = Integer.parseInt(sugar);
} catch (NumberFormatException e) {
throw new InventoryException("Units of sugar must be a positive integer");
}
if (amtSugar >= 0) {
Inventory.sugar += amtSugar;
} else {
throw new InventoryException("Units of sugar must be a positive integer");
}
}
protected synchronized boolean enoughIngredients(Recipe r) {
boolean isEnough = true;
if (Inventory.coffee < r.getAmtCoffee()) {
isEnough = false;
}
if (Inventory.milk < r.getAmtMilk()) {
isEnough = false;
}
if (Inventory.sugar < r.getAmtSugar()) {
isEnough = false;
}
if (Inventory.chocolate < r.getAmtChocolate()) {
isEnough = false;
7. }
@Test
public void addMilkTest() throws InventoryException {
Inventory inventory = new Inventory();
String milkString = "15";
inventory.addMilk(milkString);
int amtMilk = Integer.parseInt(milkString);
int milk = inventory.getMilk();
if (amtMilk >= 0) {
milk += amtMilk;
}
int expectedOutput = 45;
assertEquals(expectedOutput, milk);
}
@Test
public void addSugarTest() throws InventoryException {
Inventory inventory = new Inventory();
String sugarString = "15";
inventory.addSugar(sugarString);
int amtSugar = Integer.parseInt(sugarString);
int sugar = inventory.getSugar();
if (amtSugar >= 0) {
sugar += amtSugar;
}
int expectedOutput = 45;
assertEquals(expectedOutput, sugar);
}
8. @Test
public void addChocolateTest() throws InventoryException {
Inventory inventory = new Inventory();
String chocolateString = "15";
inventory.addChocolate(chocolateString);
int amtChocolate = Integer.parseInt(chocolateString);
int chocolate = inventory.getChocolate();
if (amtChocolate >= 0) {
chocolate += amtChocolate;
}
int expectedOutput = 45;
assertEquals(expectedOutput, chocolate);
}
@Test
public boolean testForEnoughIngredients(Recipe r) throws InventoryException {
Inventory inventory = new Inventory();
boolean isEnough = true;
if (inventory.getCoffee() < r.getAmtCoffee()) {
isEnough = false;
}
if (inventory.getMilk() < r.getAmtMilk()) {
isEnough = false;
}
if (inventory.getSugar() < r.getAmtSugar()) {
isEnough = false;
}
if (inventory.getChocolate() < r.getAmtChocolate()) {
isEnough = false;
}
assertFalse(isEnough);
return isEnough;
9. }
}
public class Recipe {
private String name;
private int price;
private int amtCoffee;
private int amtMilk;
private int amtSugar;
private int amtChocolate;
public Recipe() {
this.name = "";
this.price = 0;
this.amtCoffee = 0;
this.amtMilk = 0;
this.amtSugar = 0;
this.amtChocolate = 0;
}
public int getAmtChocolate() {
return amtChocolate;
}
public void setAmtChocolate(String chocolate) throws RecipeException {
int amtChocolate = 0;
try {
amtChocolate = Integer.parseInt(chocolate);
} catch (NumberFormatException e) {
throw new RecipeException("Units of chocolate must be a positive integer");
}
if (amtChocolate >= 0) {
this.amtChocolate = amtChocolate;
10. } else {
throw new RecipeException("Units of chocolate must be a positive integer");
}
}
public int getAmtCoffee() {
return amtCoffee;
}
public void setAmtCoffee(String coffee) throws RecipeException {
int amtCoffee = 0;
try {
amtCoffee = Integer.parseInt(coffee);
} catch (NumberFormatException e) {
throw new RecipeException("Units of coffee must be a positive integer");
}
if (amtCoffee >= 0) {
this.amtCoffee = amtCoffee;
} else {
throw new RecipeException("Units of coffee must be a positive integer");
}
}
public int getAmtMilk() {
return amtMilk;
}
public void setAmtMilk(String milk) throws RecipeException {
int amtMilk = 0;
try {
amtMilk = Integer.parseInt(milk);
} catch (NumberFormatException e) {
11. throw new RecipeException("Units of milk must be a positive integer");
}
if (amtMilk >= 0) {
this.amtMilk = amtMilk;
} else {
throw new RecipeException("Units of milk must be a positive integer");
}
}
public int getAmtSugar() {
return amtSugar;
}
public void setAmtSugar(String sugar) throws RecipeException {
int amtSugar = 0;
try {
amtSugar = Integer.parseInt(sugar);
} catch (NumberFormatException e) {
throw new RecipeException("Units of sugar must be a positive integer");
}
if (amtSugar >= 0) {
this.amtSugar = amtSugar;
} else {
throw new RecipeException("Units of sugar must be a positive integer");
}
}
public String getName() {
return name;
}
public void setName(String name) {
12. if (name != null) {
this.name = name;
}
}
public int getPrice() {
return price;
}