Question: Write 3 Java files: 1. ItemBase.java - Base class definition for an item 2. ShoppingCart.java - Extends the base class definition to add functionality for
Write 3 Java files: 1. ItemBase.java - Base class definition for an item 2. ShoppingCart.java - Extends the base class definition to add functionality for a shopping cart of several items 3. ShoppingCartClient.java - Client program that runs the application
Specification for ItemBase.java
| Class Name |
| ItemBase |
| Instance Variables |
- itemName: String - itemPrice: double - itemQuantity: double - itemSubtotal: double |
| Constructors |
+ ItemBase(): The default should initialize the instance variables to either the word "empty" (for string variables) or the value 0.0 (for double variables). + ItemBase(String item, double price, double quantity): The overloaded constructor should use the input parameters for initial values of the instance variables and should also calculate the item's subtotal by multiplying the item quantity to be purchased by the the item price |
| Instance Methods |
+ setItemName(String name) : void + setItemPrice(double price) : void + setItemQuantity(double quantity) : void + setItemSubtotal(double price, double quantity) : void + getItemName() : String + getItemPrice() : double + getItemQuantity() : double + getItemSubtotal() : double public String toString(): return a string suitable for printing in the following form itemQuantity itemName @ itemPrice each = $itemSubtotal for example, 4.0 Coke @ $2.99 each = $11.96 |
Specification for ShoppingCart.java
| Class Name |
| ShoppingCart |
| Constants and Instance Variables |
- MAXSIZE : int constant (max number of items that can be placed in shopping cart is 10) - ShoppingCart: ItemBase[MAXSIZE] - itemCount: static int |
| Constructor |
+ ShoppingCart(): initialize the instance variable count to the value of 0 |
| Instance Methods |
+ addItem(String itemName, double price, double quantity) : int This method adds a new item to the shopping cart in the next available location in the ShoppingCart array and updates the instance variable count by 1. If the shopping cart is full and the item cannot be added, this method returns a -1. Otherwise, it returns a count of the number of items currently in the shopping cart. + isFull() : boolean + isEmpty() : boolean + displayCart() : void This method prints the shopping cart contents and the amount due. for example, 50.0 Pepsi @ $0.99 each = $49.50 20.0 Bagels @ $1.29 each = $25.80 Total Amount due =======> $75.30 If the shopping cart array is empty, display the message "Shopping cart is empty" |
Specification for ShoppingCartClient.java
| void displayMainMenu() -- Displays main menu. |
| char getMenuChoice(Scanner console) -- Accepts the scanner and returns the menu choice entered by the user. |
| void processMenuChoice(Scanner console, char menuChoice, ShoppingCart myCart) -- Accepts the scanner, the menu choice from the user and the shopping cart and calls appropriate helper methods to perform the requested action. |
| String getItemName(Scanner console) -- Accepts the scanner and returns an item name entered by the user. |
| double getDouble(Scanner console, String what) -- Called for both price and the quantity. Accepts the scanner and returns a double. |
| void addToCart(Scanner console, ShoppingCart myCart) -- Accepts the scanner and the shopping cart and calls appropriate methods to add a new item to the shopping cart by calling the Shopping Cart method: addItem() |
| void displayShoppingCart(ShoppingCart myCart) -- Makes sure cart is not empty and then calls the displayCart method in the ShoppingCart Class to display the contents of the shopping cart. Displays the message: "Shopping Cart is empty" when needed. |
Sample Run:

SUZIE'S MAIN MENU AAdd item to cart DDisplay cart Quit Choose an option: A *ADD AN ITEM TO CART ** Enter the item name: Pepsi Enter the price: 0.99 Enter the quantity: 50 Item successfully added SUZIE'S MAIN MENU AAdd item to cart D - Display cart Quit Choose an option: A *ADD AN ITEM TO CART ** Enter the item name: Bagels Enter the price: 1.29 Enter the quantity: 20 Item successfully added SUZIE'S MAIN MENU AAdd item to cart DDisplay cart Q - Quit Choose an option: D Below is your order. . . $49.50 50.0 Pepsi @ $0.99 each = 20.0 Bagels $1.29 each- $25.80
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
