Question: This is a JAVA question: Write a class named ShoppingList that represents a list of items to buy, and another class named ShoppingItem that represents
This is a JAVA question: Write a class named ShoppingList that represents a list of items to buy, and another class named ShoppingItem that represents a shopping item in a given quantity (e.g., four boxes of cookies). The ShoppingList class should use an array field to store the shopping items and keep track of its size (number of items in the list so far). Assume that a shopping list will have no more than 8 items.
A ShoppingList class should have the following two fields:
private ShoppingItem[] items; //Array field to store //theShoppingItem Objects private int size; //number of ShoppingItems in the list
A ShoppingList class should have the following methods with other methods you want to add:
// constructs a new empty shopping list public ShoppingList () {..}
// adds the given ShoppingItem to this list if the list has fewer than 8 items public void add (ShoppingItem item) {..}
// returns the total sum cost of all shopping items in this list public double getTotalCost () {}
// returns String form of ShoppingList
// for example: ShoppingList has 3 shopping items: 3 Milk, 4 Tissues, 1 Toothpaste
public String toString () {..
A ShoppingItem class should have the following three fields:
private String name; // Name of the shopping item private int quantity; //quantity private double price; //price per unit.
A ShoppingItem class should have the following methods (feel free to add other methods as necessary):
/* * full constructor. * @param * name: a String represents name of the item. * quantity: a positive integer represents quantity of the item. * price: a positive double represents price of the item. */
// gets the cost of the shopping item public double getCost() {...}
//sets/changes the quantity public void setQuantity(int quantity) { }
// returns quantity and the shipping item name separated by space. For example, the method will return 3 Eggs given the shipping item name is Eggs, and the quantity is 3.
public String toString () {..}
Deliverables: 3 files (ShoppingList.java , ShoppingItem.java , ShoppingClient.java)
For this assignment, you will need to implement the two classes ShoppingList.java (5 pts) and ShoppingItem.java (5 pts)
Also Write the client program as follows (5 pts) :
(8 pts) Write a while loop (Sentinel value 4)
Option 1: add a shopping item (User input from the keyboard)
Option 2: display the shopping list and the total number of items. If the shopping list is empty, display No items in your shopping list
Option 3: Change the quantity of the item in the shopping list
Option 4: exit the loop
(2 pts) Error check
If user enters wrong values (i.e., enters a non-numeric value when asked for the quantity), display an error message and prompt the user again until they enter the correct value.
Use plenty of println statements as you go to see what is working and what isnt working. Printing out test variables can be very useful as you go. Make the programs work in stages as you go. Get individual methods working first. To check your class implementations, you may download the file ShoppingListClient.java and test your code with it. Down below is the ShoppingList Client Progam.:
/* Use this client program to test your class implementations * */
public class ShoppingListClient { public static void main (String [] args) { System.out.println("Creating ShoppingList.... "); ShoppingList g = new ShoppingList();
System.out.println("Creating new ShoppingItem... "); ShoppingItem go = new ShoppingItem("Milk", 2, 3.75);
System.out.println("Changing the quantity to 3... "); go.setQuantity( 3 );
System.out.println("The total cost should be 11.25. It is actually: " + go.getCost()+" " ); System.out.println("Adding ShoppingItems to ShoppingList ");
g.add( go );
g.add( new ShoppingItem("Tissues", 4, 2.30)); g.add (new ShoppingItem ("Toothpaste", 1, 4)); g.add (new ShoppingItem ("Eggs", 12, 0.50)); g.add (new ShoppingItem ("Apples", 5, 2.99)); g.add (new ShoppingItem ("Bananas", 4, 0.99)); System.out.println (g.toString()); System.out.println("The total cost of shopping list should be 49.36. It is actually: " + g.getTotalCost() ); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
