Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 cartItems = new ArrayList();; public ShoppingCart() { //default constructor customerName = "none"; currentDate = "January 1, 2016"; } //ArrayList cartItems ArrayList cartItems = new ArrayList();; //Parameterized constructor which takes customer name and date as paramenters public ShoppingCart(String CustomerName , String CurrentDate) { this.currentDate = CurrentDate; this.customerName = CustomerName; //Field member Parameter } //Public member methods public String getCustomerName() { //accessor return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; //Field member Parameter } public String getDate() { //accessor return currentDate; } public void setCurrentDate(String currentDate) { this.currentDate = currentDate; //Field member Parameter } //Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything. public void addItem( ItemToPurchase i) { cartItems.add(i); } //Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything. public void removeItem(String name) { boolean found = false;; for(ItemToPurchase i: cartItems){ if(i.getName().equalsIgnoreCase(name)) { cartItems.remove(i); found = true; break; } } //If item name cannot be found if(!found) System.out.println("Item not found in cart. Nothing removed"); } //Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything. public void modifyItem(ItemToPurchase item) { boolean found = false; for(ItemToPurchase i: cartItems){ //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(i.getName().equalsIgnoreCase(item.getName())) { if(i.getQuantity()!=0){ i.setQuantity(item.getQuantity()); } found = true; break; } } //If item cannot be found (by name) in cart if(!found) System.out.println("Item not found in cart. Nothing removed"); } //Returns quantity of all items in cart. Has no parameters. public int getNumItemsInCart() { int sum = 0; for(ItemToPurchase i: cartItems){ sum = sum + i.getQuantity(); } return sum; } //Determines and returns the total cost of items in cart. Has no parameters. public double getCostOfCart() { int sum = 0; for(ItemToPurchase i: cartItems){ sum = sum + i.getPrice(); } return sum; } //Outputs total of objects in cart. public void printTotal(){ if(cartItems!=null&& cartItems.size() >0 ){ System.out.println(customerName + "'s Shopping Cart - " + currentDate); System.out.println("Number of Items: " + cartItems.size()); System.out.println(); for(ItemToPurchase i: cartItems){ i.printItemCost(); } } else{ System.out.println("SHOPPING CART IS EMPTY"); } } //Outputs each item's description. public void printDescriptions() { System.out.println(customerName+"'s Shopping Cart - "+currentDate); System.out.println("Number of Items: " + cartItems.size()); System.out.println(); System.out.println("Item Descriptions"); for(ItemToPurchase i: cartItems){ i.printItemDescription(); } } }

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

(c) Can you fit the original model using this type of analysis?

Answered: 1 week ago

Question

Define Management by exception

Answered: 1 week ago

Question

Explain the importance of staffing in business organisations

Answered: 1 week ago

Question

What are the types of forms of communication ?

Answered: 1 week ago

Question

Explain the process of MBO

Answered: 1 week ago

Question

7-16 Compare Web 2.0 and Web 3.0.

Answered: 1 week ago