I am really confused by this code. The instructor posted as optional but I reall
ID: 3708512 • Letter: I
Question
I am really confused by this code. The instructor posted as optional but I really would like to know how to get it done.
The question of the problem is how to "add unit test for addInventory"
Prompt:
"Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:
Note: UNIT TEST FAILED is preceded by 3 spaces."
This is the code:
// ===== Code from file InventoryTag.java =====
public class InventoryTag {
private int quantityRemaining;
public InventoryTag() {
quantityRemaining = 0;
}
public int getQuantityRemaining() {
return quantityRemaining;
}
public void addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
return;
}
}
// ===== end =====
// ===== Code from file CallInventoryTag.java =====
public class CallInventoryTag {
public static void main (String [] args) {
InventoryTag redSweater = new InventoryTag();
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
System.out.println("Beginning tests.");
// FIXME add unit test for addInventory
/* Your solution goes here */
System.out.println("Tests complete.");
return;
}
}
// ===== end =====
Explanation / Answer
CallInventoryTag.java
public class CallInventoryTag {
public static void main (String [] args) {
InventoryTag redSweater = new InventoryTag();
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
System.out.println("Beginning tests.");
// FIXME add unit test for addInventory
/* Your solution goes here */
redSweater.addInventory(sweaterShipment);
System.out.println("initial quantity is "+sweaterInventoryBefore+" and sweaterShipment is "+redSweater.getQuantityRemaining());
System.out.println("Tests complete.");
return;
}
}
InventoryTag.java
public class InventoryTag {
private int quantityRemaining;
public InventoryTag() {
quantityRemaining = 0;
}
public int getQuantityRemaining() {
return quantityRemaining;
}
public void addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
return;
}
}
Output:
Beginning tests.
initial quantity is 0 and sweaterShipment is 25
Tests complete.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.