In: Computer Science
Create a class named GroceryList. The attributes will be threeLinkedLists. One LinkedList holds double (currency) values, one holds integer values, and the last holds String values. The String LinkedList is a list of grocery item names. The double LinkedList holds the cost of that item. The integer LinkedList is the number of those items being purchased.
The constructor will simply create empty lists.
The following are the methods to create for this class:
1. add(String, cost, quantity) : Add the specified items to the next position on each LinkedList (note that will ensure that the index of each item is the same on both LinkedLists).
2. remove(String) : Remove the specified item from BOTH lists. This means find it in the String LinkedList, save that index, and then remove the item at that index from each list.
3. bill() : This method will return the total cost of the items being purchased.
4. receipt() : This method will print the name of each item being purchased, how many of those items are being purchased, and the total cost of that item. The last line will be the total cost of all items being purchased.
java language and it's not custom
Explanation: I have created the class GroceryList and have created three LinkedList class variables as mentioned in the question.I have implemented all the method add(),remove(),bill(),receipt() and a constructor to initialize the GroceryList with empty lists.I have also shown the output,please find it attached with the answer.Please upvote if you liked my answer and conment if you need any modification.
//code starts
import java.util.LinkedList;
public class GroceryList {
LinkedList<Double> costList;
LinkedList<Integer> quantityList;
LinkedList<String> itemList;
public GroceryList() {
costList = new
LinkedList<>();
quantityList = new
LinkedList<>();
itemList = new
LinkedList<>();
}
public void add(String item, Double cost, Integer
quantity) {
itemList.add(item);
costList.add(cost);
quantityList.add(quantity);
}
public void remove(String item) {
int index = -1;
for (int i = 0; i <
itemList.size(); i++) {
if
(itemList.get(i).equalsIgnoreCase(item)) {
index = i;
itemList.remove(i);
break;
}
}
costList.remove(index);
quantityList.remove(index);
}
public double bill() {
double totalCost = 0;
for (int i = 0; i <
itemList.size(); i++) {
totalCost +=
(quantityList.get(i) * costList.get(i));
}
return totalCost;
}
public void receipt() {
System.out.println("Item \t
Quantity \t Cost");
for (int i = 0; i <
itemList.size(); i++) {
System.out.println(itemList.get(i) + "\t\t" + quantityList.get(i) +
"\t" + costList.get(i));
}
System.out.println("Total cost is:
" + bill());
}
public static void main(String[] args) {
GroceryList groceryList = new
GroceryList();
// Adding items into grocery
list
groceryList.add("Cheese", 15.5,
3);
groceryList.add("Bread", 10.5,
2);
groceryList.add("Milk", 12.75,
1);
groceryList.add("Chips", 5.5,
3);
// print receipt
groceryList.receipt();
// removing items from grocery
list
groceryList.remove("cheese");
// print receipt
groceryList.receipt();
}
}
Output:
Note: I have used java's LinkedList class becuase it is mentioned in the question not to make custom