/**
- A class contains a program that allows the user to scan each item in their cart and add or remove
- items from their bagging area.
- @author
*/
public class SelfCheckoutKiosk {
public static final double TAX_RATE = 0.05; // sales tax
// a perfect-size two-dimensional array that stores the available items in the grocery store
// GROCERY_ITEMS[i][0] refers to a String that represents the name of the item
// identified by index i
// GROCERY_ITEMS[i][1] refers to a String that represents the unit price of the item
// identified by index i in dollars.
public static final String[][] GROCERY_ITEMS = new String[][] {{“Apple”, “$1.59”},
{“Avocado”, “$0.59”}, {“Banana”, “$0.49”}, {“Beef”, “$3.79”}, {“Blueberry”, “$6.89”},
{“Broccoli”, “$1.79”}, {“Butter”, “$4.59”}, {“Carrot”, “$1.19”}, {“Cereal”, “$3.69”},
{“Cheese”, “$3.49”}, {“Chicken”, “$5.09”}, {“Chocolate”, “$3.19”}, {“Cookie”, “$9.5”},
{“Cucumber”, “$0.79”}, {“Eggs”, “$3.09”}, {“Grape”, “$2.29”}, {“Ice Cream”, “$5.39”},
{“Milk”, “$2.09”}, {“Mushroom”, “$1.79”}, {“Onion”, “$0.79”}, {“Pepper”, “$1.99”},
{“Pizza”, “$11.5”}, {“Potato”, “$0.69”}, {“Spinach”, “$3.09”}, {“Tomato”, “$1.79”}};
/**
- Returns the item’s name given its index
- @param index - unique identifier of an item
- @return the item name
*/
// Returns the name of the item given its index
// index - unique identifier of an item
public static String getItemName(int index) {
//Get the name of item at given index in the GROCERY_ITEMS array
String itemName = GROCERY_ITEMS[index][0];
return itemName;
}
/**
- Returns the item’s price given its index
- @param index - unique identifier of an item
- @return price of the given item
*/
// Returns the price of an item given its index (unique identifier)
// index - unique identifier of an item
public static double getItemPrice(int index) {
//Create a double that contains the price value of item at given index in GROCERY_ITEMS array.
//The price value is cast from String to double
double itemPrice = Double.parseDouble(GROCERY_ITEMS[index][1].substring(1).trim());
return itemPrice;
}
/**
- Prints the Catalog of the grocery store, including item identifiers, names, and prices
/
public static void printCatalog() {
// Complete the missing code / */ in the following implementation
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(“Item id \tName \t\tPrice”);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
for (int i = 0; i < GROCERY_ITEMS.length; i++) {
System.out.println(i + “\t\t” + GROCERY_ITEMS[i][0] +
" \t " + GROCERY_ITEMS[i][1]);
}
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++");
}
/**
- Returns the number of items in the bagging area after adding an item
- If number of items within the bagging area exceeds the capacity of bagging area, when trying to
- add an item, an error message "Error! No additional item can be scanned. Please wait for
- assistance." will be printed, and this adding will not be made to the content of bagging area.
- @param id - the index of the item that is about be added at the end of the bagging area
- @param items - the bagging area storing items
- @param size - number of items stored in bagging area
- @return an int representing the number of items within the bagging area after adding
*/
// Adds the name of a grocery item given its identifier at the end of
// (the bagging area) the oversize array defined by the items array and its size
// If the items array reaches its capacity, the following message:
// “Error! No additional item can be scanned. Please wait for assistance.”
// will be displayed and the method returns without making any change
// to the contents of the items array.
// id - identifier of the item to be added to the bagging area
// (index of the item in the GROCERY_ITEMS array)
// items - array storing the names of the items checked out and
// placed in the bagging area
// size - number of elements stored in items before trying to add a new item
// Returns the number of elements stored in bagging area after the item
// with the provided identifier was added to the bagging area
public static int addItemToBaggingArea(int id, String[] items, int size) {
if(size >= items.length) {
//The capacity of bagging area is reached, print error message
System.out.println(“Error! No additional item can be scanned. Please wait for assistance.”);
}
else {
items[size] = getItemName(id);
size += 1; // size is increased by one if an item is added to the bagging area
}
return size;
}
/**
- Returns the number of occurrences of a given item in the bagging area The comparison to find
- the occurrences of item is case insensitive.
- @param item - the item whose occurrences will be counted
- @param items - the bagging area storing items
- @param size - number of items stored in bagging area
- @return an int counting the occurrence of a specific item in the bagging area
*/
// Returns the number of occurrences of a given item in an oversize array of
// strings. The comparison to find the occurrences of item is case insensitive.
// item - item to count its occurrences
// items - a bag of string items
// size - number of items stored in items
public static int count(String item, String[] items, int size) {
int count = 0; //initialize occurrence to 0
// implement for loop to check and increment the occurrence
for (int i = 0; i < size; i++) {
if (items[i].equalsIgnoreCase(item)) {
count++;
}
}
return count;
}
/**
- Returns the index of the first occurrence of item in items if found, and -1 if the item not
- found
- @param item - the item to be searched for
- @param items - the bagging area storing items
- @param size - number of items stored in bagging area
- @return an int representing the index of the item’s first occurrence,
- return -1 if no match found
*/
// Returns the index of the first occurrence of item in items if found,
// and -1 if the item not found
// item - element to search for
// items - an array of string elements
// size - number of elements stored in items
public static int indexOf(String item, String[] items, int size) {
int index = 0; // initialize the index to 0
// implement for loop to compare and determine the index of first occurrence of the item
for (index = 0; index < size; index++) {
if (items[index].equals(item)) {
return index; // if the first occurrence of item is found, return index
}
}
return -1;// if the item is never found within the bagging area
}
/**
- Removes the first occurrence of itemToRemove from the bagging area defined by the array items
- and its size. If no match with itemToRemove is found, the method displays the following error
- message “WARNING: item not found.” without making any change to the items array. This method
- compacts the contents of the items array after removing the itemToRemove so there are no empty
- spaces in the middle of the array.
- This will replace the item to be removed with the last item within the cart,
- and then set the last item to null.
- @param itemToRemove - item to be removed from the bagging area
- @param items - the bagging area storing items
- @param size - number of items stored in bagging area
- @return an int representing the number of items in the bagging area after the itemToRemove is
-
removed
*/
// Removes the first occurrence of itemToRemove from the bagging area
// defined by the array items and its size. If no match with
// itemToRemove is found, the method displays the following error
// message “WARNING: item not found.” without making any change
// to the items array. This method compacts the contents of the items
// array after removing the itemToRemove so there are no empty spaces
// in the middle of the array.
// itemToRemove - item to remove from the bagging area
// items - a bag of items
// size - number of elements stored in the bag of items
// returns the number of items present in the cart after the
// itemToRemove is removed from the cart
public static int remove(String itemToRemove, String[] items, int size) {
int index = 0; // the index of the itemToRemove, if found in the items
if (size == 0 || count(itemToRemove, items, size) == 0){
// an empty bagging area, or the bagging does not contain the given item,
//just return without change, and print error message
System.out.println(“WARNING: item not found.”);
return size;
}
// when the bagging area contains at least one item to be removed,
//remove it and change update size
index = indexOf(itemToRemove, items, size);
items[index] = items[size - 1]; // replace itemToRemove with the last item in the bagging area
items[size - 1] = null;// then set the last to null;
size -= 1;// after removing, decrement the size
return size;
}
/**
- Adds every unique item stored within the items array to the initially empty itemsSet array then
- returns the number of elements in that array of unique items
- @param items - list of items added to the bagging area
- @param size - number of elements stored in items
- @param itemsSet - reference to an empty array which is going to contain every unique item
-
stored within the items array
- @return an int representing the number of items in itemsSet array
*/
// Gets a copy of the items array without duplicates. Adds every unique item
// stored within the items array to the itemsSet array.The itemsSet array is
// initially empty. Recall that a set is a collection which does not contain
// duplicate items).
// On the other hand, this method does not make any change to the contents
// of the items array.
// items - list of items added to the bagging area
// size - number of elements stored in items
// itemsSet - reference to an empty array which is going to contain the set
// of items checked out (it does not contain duplicates)
// returns the number of elements in items without accounting duplicates.
// In other words, this method returns the new size of the itemsSet array
public static int getUniqueCheckedOutItems(String[] items, int size, String[] itemsSet) {
// Note that we assume that the length of itemsSet equals
// at least the size of items. This means that itemsSet array
// can store the set of scanned items at checkout
return 1; //default return
}
/**
- Returns the total value (price) of the scanned items at checkout without tax
- @param items
- @param size
- @return a double representing the total price of checked out items without tax
*/
// Returns the total value (price) of the scanned items at checkout
// without tax in $ (double)
// items - an array which stores the items checked out
// size - number of elements stored in the items array
public static double getSubTotalPrice(String[] items, int size) {
// [Hint] Try to break down this problem into subproblems.
// define helper methods to help implement the behavior of this method
return 0.0; //default return
}
}