Question

In: Computer Science

Create a class named GroceryList. The attributes will be threeLinkedLists. One LinkedList holds double (currency) values,...

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

Solutions

Expert Solution

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


Related Solutions

A. Create a Dollar currency class with two integer attributes and one string attribute, all of...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of the conversion...
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the...
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points) 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for...
C++ PROGRAM 1) Create a "CashOut" currency class with two integer attributes and one string attribute,...
C++ PROGRAM 1) Create a "CashOut" currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent the whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equal 1 whole part. The string attribute will represent the currency name. 2) Create a "MoreCash" derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of...
Needs to be done in PYTHON A. Create a Dollar currency class with two integer attributes...
Needs to be done in PYTHON A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US...
Here is the assignment description. * Create a class named 'Account' having the following private attributes...
Here is the assignment description. * Create a class named 'Account' having the following private attributes int accountNumber; double balance; * Write a constructor with parameters for each of the attributes. * Write another constructorwith one parameter for the accountNumber. * Write getter and setter methods for each of the private attributes. * Write a method void credit(double amount) which adds the given amount to the balance. * Write a method void debit(double amount) which subtracts the given amount from...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT