JAVA CODING Must be able to compile. Thank you. Extend the ItemToPurchase class
ID: 3573200 • Letter: J
Question
JAVA CODING
Must be able to compile.
Thank you.
Extend the ItemToPurchase class per the following specifications: *Already Extended** code below!
*********************************************
Create two new files:
ShoppingCart.java - Class definition
ShoppingCartManager.java - Contains main() method
Build the ShoppingCart class with the following specifications. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.
Private fields
String customerName - Initialized in default constructor to "none"
String currentDate - Initialized in default constructor to "January 1, 2016"
ArrayList cartItems
Default constructor
Parameterized constructor which takes the customer name and date as parameters (1 pt)
Public member methods
getCustomerName() accessor
getDate() accessor
addItem()
Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything.
removeItem()
Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything.
If item name cannot be found, output this message: Item not found in cart. Nothing removed.
modifyItem()
Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.
If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.
If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.
getNumItemsInCart()
Returns quantity of all items in cart. Has no parameters.
getCostOfCart()
Determines and returns the total cost of items in cart. Has no parameters.
printTotal()
Outputs total of objects in cart.
If cart is empty, output this message: SHOPPING CART IS EMPTY
printDescriptions()
Outputs each item's description.
**********************************************************************************
Here are the examples of what the output needs to MATCH
Ex. of printTotal() output:
Ex. of printDescriptions() output:
In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart.
Ex.
(4) Implement the printMenu() method. printMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the method.
If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to execute the menu until the user enters q to Quit.
Ex:
(5) Implement Output shopping cart menu option.
Ex:
(6) Implement Output item's description menu option.
Ex.
(7) Implement Add item to cart menu option.
Ex:
(8) Implement Remove item menu option.
Ex:
Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method.
Ex:
*****************************************************************************************************************************************
expanded ItemToPurchase
//This is my ItemToPurchase class with all of my get/set:
public class ItemToPurchase
{
private String itemName;
private double itemPrice;
private int itemQuantity;
//(1) Extend the ItemToPurchase class per the following specifications:
//Private fields
//string itemDescription
private String itemDescription;
public ItemToPurchase()
{
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
//Initialized in default constructor to "none"
itemDescription = "none";
}
//Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
public ItemToPurchase(String iDesc)
{
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
itemDescription = iDesc;
}
//setDescription() mutator & getDescription() accessor (2 pts)
public void setDescription(String iDesc)
{
itemDescription = iDesc;
}
public String getDescription()
{
return itemDescription;
}
//printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
public void printItemCost()
{
System.out.println(itemName + " " + itemQuantity + " @ $" + itemPrice + " = $" + itemQuantity * itemPrice);
}
//printItemDescription() - Outputs the item name and description
public void printItemDescription()
{
System.out.println(itemName + ": " + itemDescription);
}
public void setName(String name)
{
itemName = name;
}
public String getName()
{
return itemName;
}
public void setPrice(double price)
{
itemPrice = price;
}
public double getPrice()
{
return itemPrice;
}
public void setQuantity(int quantity)
{
itemQuantity = quantity;
}
public int getQuantity()
{
return itemQuantity;
}
}
Explanation / Answer
import java.util.Scanner;
public class ShoppingCartManager {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String customerName = "none";
String currentDate = "January 1, 2016";
System.out.println("Enter Customer's Name: ");
customerName = scnr.nextLine();
System.out.println("Enter Today's Date: ");
currentDate = scnr.nextLine();
ShoppingCart cart = new ShoppingCart(customerName, currentDate);
System.out.println("Customer Name: " + cart.getCustomerName());
System.out.println("Today's Date: " + cart.getDate());
printMenu(cart, scnr);
}
public static void printMenu(ShoppingCart cart, Scanner scnr) {
ItemToPurchase cartItems = new ItemToPurchase();
char userChoice = '?';
System.out.println("");
System.out.print("MENU" + " a - Add item to cart" + " d - Remove item from cart"
+ " c - Change item quantity" + " i - Output items' descriptions"
+ " o - Output shopping cart" + " q - Quit");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Choose an option: ");
userChoice = scnr.next().charAt(0);
boolean rePrintMenu = true;
do {
switch (userChoice) {
case 'a':
System.out.println("ADD ITEM TO CART");
System.out.println("Enter the item name: ");
cartItems.setName(scnr.nextLine());
System.out.println(" Enter the item description: ");
cartItems.setDescription(scnr.nextLine());
System.out.println(" Enter the item price: ");
cartItems.setPrice(scnr.nextInt());
System.out.println("Enter the item quantity: ");
cartItems.setQuantity(scnr.nextInt());
break;
case 'd':
System.out.println("REMOVE ITEM FROM CART");
System.out.println("Enter name of item to remove: ");
cart.removeItem(scnr.nextLine());
break;
case 'c':
//ItemToPurchase newQuantity = new ItemToPurchase();
System.out.println("CHANGE ITEM QUANTITY");
System.out.println("Enter the item name: ");
break;
case 'i':
System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate());
System.out.println("");
System.out.println("");
System.out.println("Item Descriptions");
System.out.println(cart.printDescriptions());
System.out.println();
break;
case 'o':
System.out.println("OUTPUT SHOPPING CART");
System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate());
System.out.println( cart.getNumItemsInCart() );
System.out.println("");
System.out.println("");
System.out.println(cart.printTotal());
break;
case 'q':
rePrintMenu = false;
System.exit(0);
break;
}
} while (rePrintMenu);
return;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.