import java.util.Arrays;
/**
- A class contains testing methods for the SelfCheckoutKiosk program
- @author
*/
public class SelfCheckoutKioskTester {
/**
-
Checks whether SelfCheckoutKisok.getItemName() and SelfCheckoutKisok.getItemPrice() method work
-
as expected.
-
@return true when this test verifies a correct functionality, and false otherwise
*/
public static boolean testItemNameAndPriceGetterMethods() {
// consider all identifiers values as input arguments
// GROCERY_ITEMS array is a perfect size array. So, its elements are stored
// in the range of indexes from 0 … GROCERY_ITEMS.length -1
for (int i = 0; i < SelfCheckoutKiosk.GROCERY_ITEMS.length; i++) {
// check first for the correctness of the getItemName(i) method
if (!SelfCheckoutKiosk.getItemName(i).equals(SelfCheckoutKiosk.GROCERY_ITEMS[i][0])) {
// Print error message before returning false
System.out.println("Problem detected: Called your getItemName() method with "
+ "input value " + i + “. But it did not return the expected output.”);
return false;
}// Now, let’s check for the correctness of the getItemPrice(i) method
double expectedPriceOutput =
Double.valueOf(SelfCheckoutKiosk.GROCERY_ITEMS[i][1].substring(1).trim());
// We do not use == to compare floating-point numbers (double and float)
// in java. Two variables a and b of type double are equal if the absolute
// value of their difference is less or equal to a small threshold epsilon.
// For instance, if Math.abs(a - b) <= 0.001, then a equals b
if (Math.abs((SelfCheckoutKiosk.getItemPrice(i) - expectedPriceOutput)) > 0.001) {
// We recommend that you print a descriptive error message before returning false
System.out.println("Problem dectected: The returned price of item from your "
+ “getItemPrice() method does not match the expected item price, test failed.”);
return false;
}
}
return true; // No defect detected -> The implementation passes this test
}
/**
- Checks the correctness of SelfCheckoutKiosk.addItemToBaggingArea() method
- @return true when this test verifies a correct functionality, and false otherwise
*/
public static boolean testAddItemToBaggingArea() {
// Create an empty bagging area
String[] items = new String[10];
int size = 0;
// Define the test scenarios:
// (1) Add one item to an empty bagging area
// try to add an apple (id: 0) to the bagging area
size = SelfCheckoutKiosk.addItemToBaggingArea(0, items, size);
// Check if an item is added to the bagging area
if (size != 1) {
System.out.println("Problem detected: Tried to add one item to an empty, "
+ "bagging area. The returned size must be 1. But your addItemToBaggingArea "
+ "method returned a different output.");
return false;
}
// Check if the added item is the correct item
if (!items[0].equals(SelfCheckoutKiosk.getItemName(0))) {
// notice here the importance of checking for the correctness of your getItemName()
// method before calling it above
System.out.println("Problem detected: Tried to add only one item to an empty, "
+ "bagging area. But that item was not appropriately added to the contents "
+ "of the items array.");
return false;
}
// (2) Consider a non-empty bagging area
items = new String[] {"Milk", "Chocolate", "Onion", null, null, null, null};
size = 3;
size = SelfCheckoutKiosk.addItemToBaggingArea(10, items, size);
// Check if an item is added to the bagging area
if (size != 4) {
System.out.println("Problem detected: Tried to add only one item to an non-empty, "
+ "bagging area. The size must be incremented after the method returns. But "
+ "it was not the case");
return false;
}
// Check if the added item is the correct item
if (!items[3].equals(SelfCheckoutKiosk.getItemName(10))) {
System.out.println("Problem detected: Tried to add one item to an non-empty, "
+ "bagging area. But that item was not appropriately added to the contents "
+ "of the items array.");
return false;
}
// (3) Consider adding an item to a full bagging are
items = new String[] {"Pizza", "Eggs", "Apples"};
size = 3;
size = SelfCheckoutKiosk.addItemToBaggingArea(2, items, size);
// TODO Complete the implementation of this test scenario
// Check that the returned size is correct (must be 3), and that no
// changes have been made to the content of items array {"Pizza", "Eggs", "Apples"}
// Check if an item is added to the bagging area
if (size != 3) {
// the size has changed, print error message
System.out.println("Problem detected: Tried to add only one item to an full, "
+ "bagging area. The size must not change after the method returns. But "
+ "it was not the case");
return false;
}
// Check if the content of bagging area has changed
if (!items[0].equals("Pizza") || !items[1].equals("Eggs") || !items[2].equals("Apples")) {
// elements differ from the expected, content of items array has changed, print error message
System.out.println("Problem detected: Tried to add only one item to an full, "
+ "bagging area. The content of the items array must not change after the method returns."
+ " But it was not the case");
return false;
}
return true; // No defects detected by this unit test
}
/**
- Checks the correctness of SelfCheckoutKiosk.count() method
- @return true when this test verifies a correct functionality, and false otherwise
*/
// Checks the correctness of SelfCheckoutKiosk.count() method
// Try to consider different test scenarios: (1) a bagging area (defined by
// the items array and its size) which contains 0 occurrences of the item,
// (2) a bagging area which contains at least 4 items and only one occurrence
// of the item to count, and (3) a bagging area which contains at least 5 items
// and 2 occurrences of the item to count.
public static boolean testCount() {
// Create an empty bagging area and a String for the item name
String[] items = new String[10];
int size = 0;
// Define the test scenarios:
// (1) Consider a bagging area which contains 0 occurrences of the item
// first test of this scenario
items =
new String[] {"pineapple", "chocolate", "banana", "ice cream", "Strawberry", "juice", null};
size = 6;
String itemName = "apple"; // Specify the item to be counted for occurrences
int count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 0) {
// the items array contains 0 occurrences of the item, the returned count is wrong,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 0 occurrence of the item."
+ " But the returned count value does not match");
return false;
}
// second test of this scenario
items = new String[] {"pineapple", "chocolate", "banana"};
size = 3;
itemName = "sugar";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 0) {
// the items array contains 0 occurrences of the item, the returned count is wrong,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 0 occurrence of the item."
+ " But the returned count value does not match");
return false;
}
// (2) Consider bagging area which contains at least 4 items and only one occurrence
// of the item to count
// first test of this scenario
items = new String[] {"apple", "chocolate", "banana", "ice cream", "Strawberry", "juice", null};
size = 6;
itemName = "apple";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 1) {
// the items array contains only 1 occurrence of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 1 occurrence of the item."
+ " But the returned count value does not match");
return false;
}
// second test of this scenario
items = new String[] {"apple", "banana", "ice cream", "Strawberry", "juice", null, null};
size = 5;
itemName = "straWberRy";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 1) {
// the items array contains only 1 occurrence of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 1 occurrence of the item."
+ " But the returned count value does not match");
return false;
}
// (3) Consider a bagging area which contains at least 5 items
// and 2 occurrences of the item to count
// first test of this scenario
items = new String[] {"Apple", "chocolate", "banana", "ice cream", "applE", null};
size = 5;
itemName = "apple";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 2) {
// the items array contains 2 occurrences of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 2 occurrences of the item."
+ " But the returned count value does not match");
return false;
}
// second test of this scenario
items = new String[] {"Apple", "chocolate", "banana", "ice cream", "applE", "JuIcE", "juIcE"};
size = 7;
itemName = "JUICE";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 2) {
// the items array contains 2 occurrences of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 2 occurrences of the item."
+ " But the returned count value does not match");
return false;
}
// (4) Consider a bagging area which contains at least 8 items
// and 5 occurrences of the item to count
// first test of this scenario
items = new String[] {"Apple", "chocolate", "apple", "ice cream", "applE", "apPle", "APple",
"APP1E"};
size = 8;
itemName = "apple";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 5) {
// the items array contains 5 occurrences of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 5 occurrences of the item."
+ " But the returned count value does not match");
return false;
}
// second test of this scenario
items =
new String[] {"Apple", "Egg", "Eg1g", "Egg", "EGG", "JuIcE", "juIcE", "egG", "egg", null};
size = 9;
itemName = "egg";
count = SelfCheckoutKiosk.count(itemName, items, size);
if (count != 5) {
// the items array contains 5 occurrences of the item, the returned count is not the case,
// print error message
System.out.println("Problem detected: Tried to count the occurrence of an item in the "
+ "bagging area which has 5 occurrences of the item."
+ " But the returned count value does not match");
return false;
}
return true; // No defects detected by this unit test
}
/**
- Checks the correctness of SelfCheckoutKiosk.indexOf() method
- @return true when this test verifies a correct functionality, and false otherwise
*/
// Checks the correctness of SelfCheckoutKiosk.indexOf() method
// Consider the cases where the items array contains at least one match
// with the item to find, and the case when the item was not stored in
// the array and the expected output is -1
public static boolean testIndexOf() {
// Create an empty bagging area
String[] items = new String[10];
int size = 0;
// Define the test scenarios:
// (1) Consider the case where the items array contains at least one match
// with the item to find
// First test of this scenario
items = new String[] {"apple", "apple", "apple", "egg", "banana"};
size = 5;
String itemName = "apple"; // Specify the item to be searched for
int index = SelfCheckoutKiosk.indexOf(itemName, items, size);
if (index != 0) {
// the first occurrence of the item within the bagging area is at the index of 0,
// the returned value is not the case, print error message
System.out.println(
"Problem detected: Tried to determine the index of the first occurrence of an item"
+ " in the bagging area, but the returned value does not represent the correct index.");
return false;
}
// Second test of this scenario
items = new String[] {"Banana", "apple", "banaNa", "egg", "banana", "banana"};
size = 6;
itemName = "banana"; // Specify the item to be searched for
index = SelfCheckoutKiosk.indexOf(itemName, items, size);
if (index != 4) {
// the first occurrence of the item within the bagging area is at the index of 4,
// the returned value is not the case, print error message
System.out.println(
"Problem detected: Tried to determine the index of the first occurrence of an item"
+ " in the bagging area, but the returned value does not represent the correct index.");
return false;
}
// Third test of this scenario
items = new String[] {"ice", "cream", "ice cream", "Ice cream", "Ice cream", "ICe cream"};
size = 6;
itemName = "Ice cream"; // Specify the item to be searched for
index = SelfCheckoutKiosk.indexOf(itemName, items, size);
if (index != 3) {
// the first occurrence of the item within the bagging area is at the index of 3,
// the returned value is not the case, print error message
System.out.println(
"Problem detected: Tried to determine the index of the first occurrence of an item"
+ " in the bagging area, but the returned value does not represent the correct index.");
return false;
}
// (2) Consider the case that the item was not stored in
// the array and the expected output is -1
// First test of this scenario
items = new String[] {"ice", "cream", "ice cream", "Ice craam", "Ice craem", "ICe cream"};
size = 6;
itemName = "Ice cream"; // Specify the item to be searched for
index = SelfCheckoutKiosk.indexOf(itemName, items, size);
if (index != -1) {
// the bagging area does not contain the item, -1 should be returned
// the returned value is not the case, print error message
System.out.println(
"Problem detected: Tried to determine the index of the first occurrence of an item"
+ " in the bagging area which actually does not contain the item, -1 should be returned,"
+ " but the returned value is " + index + " .");
return false;
}
// Second test of this scenario
items = new String[] {"apple", "orange", "ice cream", "juice", "noodels"};
size = 5;
itemName = "chocolate"; // Specify the item to be searched for
index = SelfCheckoutKiosk.indexOf(itemName, items, size);
if (index != -1) {
// the bagging area does not contain the item, -1 should be returned
// the returned value is not the case, print error message
System.out.println(
"Problem detected: Tried to determine the index of the first occurrence of an item"
+ " in the bagging area which actually does not contain the item, -1 should be returned,"
+ " but the returned value is " + index + " .");
return false;
}
return true; // No defects detected by this unit test
}
/**
- Checks the correctness of SelfCheckoutKiosk.remove() method
- @return true when this test verifies a correct functionality, and false otherwise
*/
// Checks that when only one attempt to remove an item stored in the bagging area
// is made, only one occurrence of that item is removed from the array of items,
// that the returned size is correct, and that the items array contains all the
// other items.
public static boolean testRemove() {
// Create an empty bagging area
String[] items = new String[10];
int size = 0;
String itemToRemove = null; // Create the item to be removed
// Define the test scenarios
// (1) Consider that the bagging area does not contain the item that is attempted to be removed
// in this scenario, the bagging area should return with no change
items = new String[] {"apple", "juice", "pizza", "potato", "pepper"};
size = 5;
itemToRemove = "Onion";
// Create a duplicate of the original bagging area for comparison of size and content later
String[] itemsCopy = Arrays.copyOf(items, size);
int copySize = size;
size = SelfCheckoutKiosk.remove(itemToRemove, items, size);
// check if the size has changed, in this case, size should not change
if (size != copySize) {
// size has changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area which actually does not contain the item, size should not change,"
+ " but it was not the case.");
return false;
}
// check if the content of bagging area has changed, in this case, content should not change
if (Arrays.equals(items, itemsCopy) != true) {
// content has change, print error message
System.out.println(“Problem detected: Tried to remove the first occurrence of an item”
+ " in the bagging area which actually does not contain the item, "
+ “content should not change, but it was not the case.”);
return false;
}
// (2) Consider that the bagging area is empty
items = new String[10];
size = 0;
itemToRemove = "sugar";
size = SelfCheckoutKiosk.remove(itemToRemove, items, size);
itemsCopy = Arrays.copyOf(items, 10);
copySize = 0;
// check if the size has changed, in this case, size should not change
if (size != 0) {
// size has changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in an empty bagging area, size should not change, but it was not the case.");
return false;
}
// check if the content of bagging area has changed, in this case, content should not change
if (Arrays.equals(items, itemsCopy) != true) {
// content has change, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in an empty bagging area, content should not change, but it was not the case.");
return false;
}
// (3) Consider the bagging area that contains the item to be removed
// First case: when the item only has one occurrence in the bagging area, and it is removed
items = new String[] {"apple", "juice", "orange", "turkey", "sugar","pizza"};
size = 6;
itemToRemove = "orange";
size = SelfCheckoutKiosk.remove(itemToRemove, items, size);
// 1.Check if the size of bagging area is decreased by 1
if (size != 5) {
// size has changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, size should decreased by 1, but it was not the case.");
return false;
}
// 2.Check if the item is removed
for (int i = 0; i < size; i++) {
if (items[i].equals("orange")) {
// failed to remove the item, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, but faield to do so.");
return false;
}
}
// 3.Check if the bagging area is compacted after removal
// (contains no null reference in the middle)
for (int i = 0; i < size; i++) {
if (items[i] == null) {
// null reference exists in the middle, bagging area not compacted, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, but the bagging area is not compacted after removal.");
return false;
}
}
// 4.Check if other items in the bagging area remain the same
// Create a non-order simulation of the bagging area after removal
// the simulation of contains non-null elements
String[] itemsSimulation = new String[] {"apple", "juice", "turkey", "sugar","pizza"};
// create a duplicate of items, but only include non-null elements
// used for comparison with itemsSimulation later
String[] nonNullItems = new String[5];
for (int i = 0; i < size; i++) {
nonNullItems[i] = items[i];
}
Arrays.sort(nonNullItems);
Arrays.sort(itemsSimulation);
if(!Arrays.equals(nonNullItems, itemsSimulation)) {
// other items have changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, but caused other items to change.");
return false;
}
// Second case: the item to be removed within the bagging area has multiple occurrences
items = new String[] {"apple", "pork", "orange", "turkey", "sugar","pizza", "orange", "orange"};
size = 8;
itemToRemove = "orange";
size = SelfCheckoutKiosk.remove(itemToRemove, items, size);
// 1.Check if the size of bagging area is decreased by 1
if (size != 7) {
// size has changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, size should decreased by 1, but it was not the case.");
return false;
}
// 2.Check if one of the occurrences of the item is removed
int occurrence = 0; // create an value to count the occurrence of itemToRemove after removal
for (int i = 0; i < size; i++) {
if (items[i].equals("orange")) {
occurrence++;
}
}
if (occurrence != 2) {
// the occurrences of itemToRemove should decrease by 1, but failed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, its occurrence should decreased by 1 if removed,"
+ " but it was not the case.");
return false;
}
// 3.Check if the bagging area is compacted after removal
// (contains no null reference in the middle)
for (int i = 0; i < size; i++) {
if (items[i] == null) {
// null reference exists in the middle, bagging area not compacted, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, but the bagging area is not compacted after removal.");
return false;
}
}
// 4.Check if removed the first occurrence of itemToRemove
// 5.Check if other items in the bagging area remain the same
// Create a non-order simulation of the bagging area after removal
// the simulation of contains non-null elements
String[] itemsSimulation2 =
new String[] {"apple", "pork", "orange", "turkey", "sugar","pizza", "orange"};
// create a duplicate of items, but only include non-null elements
// used for comparison with itemsSimulation later
String[] nonNullItems2 = new String[7];
for (int i = 0; i < size; i++) {
nonNullItems2[i] = items[i];
}
Arrays.sort(nonNullItems2);
Arrays.sort(itemsSimulation2);
if(!Arrays.equals(nonNullItems2, itemsSimulation2)) {
// other items have changed, print error message
System.out.println("Problem detected: Tried to remove the first occurrence of an item"
+ " in the bagging area, but caused other items to change.");
return false;
}
return true; // No defects detected by this unit test
}
// Checks whether getSubTotalPrice method returns the correct output
public static boolean testGetSubTotalPrice() {
return true; // No defects detected by this unit test
}
// Checks whether getUniqueCheckedOutput functioning is correct
public static boolean testGetUniqueCheckedOutItems() {
return false;
}
/**
- main method used to call the unit tests in this class and will display their output, will also
- call the SelfCheckoutKiosk.printCatalog() to print the catalog of grocery items
- @param args input arguments if any
*/
public static void main(String[] args) {
// Call the test method for SelfCheckoutKisok.getItemName() and
// SelfCheckoutKisok.getItemPrice() methods, and print the test result
System.out
.println("testItemNameAndPriceGetterMethods(): " + testItemNameAndPriceGetterMethods());
// Call the SelfCheckoutKiosk.printCatalog() method, which prints the catalog of grocery items
SelfCheckoutKiosk.printCatalog();
// Call the test method for SelfCheckoutKisok.addItemToBaggingArea() and print the test result
System.out.println("testAddItemToBaggingArea(): " + testAddItemToBaggingArea());
// Call the test method for SelfCheckoutKisok.count() and print the test result
System.out.println("testCount(): " + testCount());
// Call the test method for SelfCheckoutKisok.indexOf() and print the test result
System.out.println("testIndexOf(): " + testIndexOf());
// Call the test method for SelfCheckoutKisok.remove() and print the test result
System.out.println("testRemove(): " + testRemove());
}
}