Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write 3 Java files: 1. ItemBase.java - Base class definition for an item 2. Shop

ID: 3710263 • Letter: W

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 a shopping cart of several items 3. ShoppingCartClient.java - Client program that runs the application

Specification for ItemBase.java

Class Name

ItemBase

Instance Variables

Constructors

Instance Methods

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

Constructor

Instance Methods

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:

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

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

Explanation / Answer

import java.util.Scanner;
public class ShoppingCartClient {
    void displayMainMenu(){
System.out.println("SUZIES'S MAIN MENU");
System.out.println("A - ADD item to cart");
System.out.println("D - Display cart");
System.out.println("Q - Quit");
}
char getMenuChoice(Scanner console){

System.out.print("Choose an option: ");
char option = console.next().charAt(0);
return option;
}

void processMenuChoice(Scanner console, char menuChoice, ShoppingCart myCart){

if(menuChoice == 'A'){
addToCart(console, myCart);
}else if(menuChoice=='D'){
displayShoppingCart(myCart);
}else if(menuChoice=='Q'){
System.exit(0);
}

}
String getItemName(Scanner console){
System.out.print("Enter the item name: ");
String itemName = console.next();
return itemName;
}

double getDouble(Scanner console, String what){
System.out.print("Enter the " + what + ": ");
double out = console.nextDouble();
return out;
}

void addToCart(Scanner console, ShoppingCart myCart) {
System.out.print("** ADD AN ITEM TO CART **");
System.out.println();
String itemName = getItemName(console);
double price = getDouble(console, "price");
double quantity = getDouble(console, "quantity");
myCart.addItem(itemName, price, quantity);

}
void displayShoppingCart(ShoppingCart myCart){

if(!myCart.isEmpty()){
System.out.println("Below is your order...");
myCart.displayCart();
}else{
System.out.println("Shopping Cart is empty");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
char option = 0;
Scanner console = new Scanner(System.in);
ShoppingCart myCart = new ShoppingCart();
ShoppingCartClient client = new ShoppingCartClient();
do{
client.displayMainMenu();
option = client.getMenuChoice(console);
client.processMenuChoice(console, option, myCart);


}while(option!='Q');

}
}
================================
public class ShoppingCart {
    final int MAXSIZE = 10;
static int itemCount;
ItemBase[] shoppingCart;

public ShoppingCart(){
itemCount = 0;
shoppingCart = new ItemBase[MAXSIZE];
}

public boolean isFull(){
return itemCount == MAXSIZE ? true : false;
}
public int addItem(String itemName,double price,double quantity){

if(!isFull()){
ItemBase item = new ItemBase(itemName, price, quantity, price * quantity);
shoppingCart[itemCount] = item;
itemCount = itemCount + 1;
}

return itemCount;
}

public boolean isEmpty(){
return itemCount == 0 ? true : false;
}

public void displayCart(){
double total = 0;
int count = 1;
if(!isEmpty()){
for(ItemBase item : shoppingCart){
if(count<=itemCount){
System.out.println(item.toString());
total +=item.itemSubtotal;
count++;
}else{
break;
}

}
System.out.println("Total Amount Due=====> " + total);
}else
System.out.println("Shopping cart is empty");

}
}
=========================
public class ItemBase {
   
    String itemName;
double itemPrice;
double itemQuantity;
double itemSubtotal;

public ItemBase(){

}
/**
* @param itemName
* @param itemPrice
* @param itemQuantity
* @param itemSubtotal
*/
public ItemBase(String itemName, double itemPrice, double itemQuantity, double itemSubtotal) {
super();
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
this.itemSubtotal = itemSubtotal;
}
/**
* @return the itemName
*/
public String getItemName() {
return itemName;
}
/**
* @param itemName the itemName to set
*/
public void setItemName(String itemName) {
this.itemName = itemName;
}
/**
* @return the itemPrice
*/
public double getItemPrice() {
return itemPrice;
}
/**
* @param itemPrice the itemPrice to set
*/
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
/**
* @return the itemQuantity
*/
public double getItemQuantity() {
return itemQuantity;
}
/**
* @param itemQuantity the itemQuantity to set
*/
public void setItemQuantity(double itemQuantity) {
this.itemQuantity = itemQuantity;
}
/**
* @return the itemSubtotal
*/
public double getItemSubtotal() {
return itemSubtotal;
}
/**
* @param itemSubtotal the itemSubtotal to set
*/
public void setItemSubtotal(double itemSubtotal) {
this.itemSubtotal = itemSubtotal;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

return itemQuantity +" " + itemName + " @ "+ itemPrice +"each = $" +itemSubtotal ;
}

}

Output:
SUZIES'S MAIN MENU
A - ADD item to cart
D - Display cart
Q - Quit
Choose an option: A
** ADD AN ITEM TO CART **
Enter the item name: Pepsi
Enter the price: 10
Enter the quantity: 8
SUZIES'S MAIN MENU
A - ADD item to cart
D - Display cart
Q - Quit
Choose an option: A
** ADD AN ITEM TO CART **
Enter the item name: Coke
Enter the price: 8
Enter the quantity: 9
SUZIES'S MAIN MENU
A - ADD item to cart
D - Display cart
Q - Quit
Choose an option: D
Below is your order...
8.0 Pepsi @ 10.0each = $80.0
9.0 Coke @ 8.0each = $72.0
Total Amount Due=====> 152.0
SUZIES'S MAIN MENU
A - ADD item to cart
D - Display cart
Q - Quit
Choose an option: Q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote