Question
I need help modifying my code. ShoppingCart.java - Class definition ShoppingCartManager.java - Contains main() method 1. Build the ShoppingCart class with the following specifications. Note:
I need help modifying my code.
ShoppingCart.java - Class definition
ShoppingCartManager.java - Contains main() method
1. 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
Public member methods:
-getCustomerName() accessor (1 pt)
getDate() accessor (1 pt)
addItem()
-Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything.
removeItem()
-Deletes 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 deleted.
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() (2 pts)
-Returns quantity of all items in cart. Has no parameters. g
etCostOfCart() (2 pts)
-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 my code for ItemToPurchase.java
public class ItemToPurchase { //Private fields private String itemName; private String itemDescription; private int itemPrice; private int itemQuantity; public ItemToPurchase() { //Default constructors String itemDescription = "none"; itemName = "none"; itemQuantity = 0; itemPrice = 0; } //Parameterized constructor to assign name,descptn, price, quanitity public ItemToPurchase(String ItemName, String ItemDescription, int ItemPrice, int ItemQuantity) { //"this" refers to the object that is calling the method in the first place/reference this.itemDescription = ItemDescription; this.itemName = ItemName; this.itemPrice = ItemPrice; this.itemQuantity = ItemQuantity; } //Public member methods public String itemDescription() { return itemDescription; } //Outputs the item name followed by the quantity, price, and subtotal public void printItemCost() { System.out.println(itemName + " " + itemQuantity + " @ " + itemPrice + " = $" + (itemQuantity * itemPrice)); } public void printItemDescription() { System.out.println(itemName + " " + itemDescription); } public void getDescription(String itemDescription) { this.itemDescription = itemDescription; } public String getName() { return itemName; } public void setName(String itemName) { this.itemName = itemName; } public int getPrice() { return itemPrice; } public void setPrice(int itemPrice) { this.itemPrice = itemPrice; } public int getQuantity() { return itemQuantity; } public void setQuantity(int itemQuantity) { this.itemQuantity = itemQuantity; } }
my code for ShoppingCart.java
import java.util.ArrayList;
public class ShoppingCart { //Private fields private String customerName; private String currentDate; //ArrayList
my code for ShoppingCartManager.java
import java.util.Scanner;
public class ShoppingCartManager { public static void main(String[] args){ //prompt the user for a customer's name and today's date Scanner scan = new Scanner(System.in); System.out.println("Enter Customer's Name: "); String customerName = scan.nextLine(); System.out.println("Enter Today's Date: "); String currentDate = scan.nextLine(); ShoppingCart x = new ShoppingCart(customerName, currentDate); System.out.println(); //space //Output the name and date System.out.println("Customer Name: " + x.getCustomerName()); System.out.println("Today's Date: " + currentDate); printMenu(x); } public static void printMenu(ShoppingCart x){ while(true){ //Output of the menu System.out.println("MENU"); System.out.println("a - Add item to cart"); System.out.println("d - Remove item from cart"); System.out.println("c - Change item quantity"); System.out.println("i - Output items' descriptions"); System.out.println("o - Output shopping cart"); System.out.println("q - Quit Choose an option: "); Scanner scanner = new Scanner(System.in); //User chooses option char input = scanner.next().charAt(0); scanner.nextLine(); //user input //Add item to cart if(input == 'a'){ System.out.println("ADD ITEM TO CART"); System.out.println("Enter Item Name: "); String name = scanner.nextLine(); System.out.println("Enter Item Description: "); String itemDescritpion = scanner.nextLine(); System.out.println("Enter Item Price: "); int itemPrice = scanner.nextInt(); System.out.println("Enter Item Quantity: "); int quantity = scanner.nextInt(); scanner.nextLine(); ItemToPurchase item = new ItemToPurchase(name, itemDescritpion, itemPrice, quantity); x.addItem(item); } //Remove item from cart else if(input == 'd'){ System.out.println("REMOVE ITEM FROM CART"); System.out.println("Enter name of item to remove: "); String name = scanner.nextLine(); x.removeItem(name); } //Change item quantity else if(input == 'c'){ System.out.println("CHANGE ITEM QUANTITY"); System.out.println("Enter the item name: "); String name = scanner.nextLine(); System.out.println("Enter the new quantity: "); int quantity = scanner.nextInt(); ItemToPurchase item = new ItemToPurchase(); item.setName(name); item.setQuantity(quantity); x.modifyItem(item); } //Output items' descriptions else if(input == 'i'){ System.out.println("OUTPUT ITEMS' DESCRIPTIONS"); x.printDescriptions(); } //Output shopping cart else if(input == 'o'){ System.out.println("OUTPUT SHOPPING CART"); x.printTotal(); } //Quit else if(input == 'q'){ break; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started