Write 3 Java files: 1. ItemBase.java - Base class definition for an item 2. Shop
ID: 3711207 • 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.80Explanation / Answer
ItemBase.java
public class ItemBase {
String itemName;
double itemPrice;
double itemQuantity;
double itemSubtotal;
public ItemBase() {
}
public ItemBase(String itemName, double itemPrice, double itemQuantity,
double itemSubtotal) {
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
this.itemSubtotal = itemSubtotal;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public double getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(double itemQuantity) {
this.itemQuantity = itemQuantity;
}
public double getItemSubtotal() {
return itemSubtotal;
}
public void setItemSubtotal(double itemSubtotal) {
this.itemSubtotal = itemSubtotal;
}
@Override
public String toString() {
System.out.printf("%.1f %s @ $%.2f each = $%.2f ",itemQuantity,itemName,itemPrice,itemSubtotal);
return "";
}
}
__________________
ShoppingCart.java
public class ShoppingCart {
final int MAXSIZE = 10;
ItemBase items[] = new ItemBase[MAXSIZE];
static int itemCount;
public ShoppingCart() {
itemCount = 0;
}
boolean isFull() {
if (itemCount == MAXSIZE)
return true;
else
return false;
}
boolean isEmpty() {
if (itemCount == 0)
return true;
else
return false;
}
void addItem(String itemName, double price, double quantity) {
if (isFull()) {
System.out.println("** ShoppingCart is full **");
} else {
ItemBase i = new ItemBase(itemName, price, quantity, price
* quantity);
items[itemCount] = i;
itemCount++;
}
}
public void displayCart() {
double subTot=0.0;
if(isEmpty())
{
System.out.println("Shopping cart is empty");
}
else
{
for (int i = 0; i < itemCount; i++) {
System.out.println(items[i]);
subTot+=items[i].getItemSubtotal();
}
}
System.out.printf("Total Amount due =======> $%.2f ",subTot);
}
}
_____________________
I will Develop the Client class also.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.