Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ShoppingCart.java :- package main; import java.util.ArrayList; public class ShoppingCart extends ItemToPurchase { //Private Data Members private String customerName; private String currentDate; public ShoppingCart () {

ShoppingCart.java :-

package main; import java.util.ArrayList;

public class ShoppingCart extends ItemToPurchase { //Private Data Members private String customerName; private String currentDate; public ShoppingCart () { // The default Constructor customerName = "none"; currentDate = "January 1, 2020"; } //ArrayList cartItems ArrayList cartItems = new ArrayList();;

/* * parameterized constructor which takes the customer name and date as parameters */ public ShoppingCart (String CustomerName, String CurrentDate, int x, int y) { this.currentDate = CurrentDate; this.customerName = CustomerName; } //Public member methods public String getCustomerName() { // accessor return customerName; }

public void setCustomerName(String customerName) { this.customerName = customerName; }

public String getDate() { //accessor return currentDate; } public void setCurrentDate(String currentDate) { this.currentDate = currentDate; } /* * This method adds an item to the cartItems array. It also has a parameter ItemToPurchase. It does not return anything. */ public void addItem( ItemToPurchase i) { cartItems.add(i); } /* * This method removes item from the cartItems array. It has a string (an item's name) parameter. It 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(!found) //If an item name cannot be found System.out.println("Item not found in cart. Nothing removed"); } /* * This method modifies an item's description, price and/or quantity. * It has a parameter ItemToPurchase. It does not return anything. */

public void modifyItem(ItemToPurchase i) { boolean found = false; for(ItemToPurchase k: cartItems){ /* * If item can be found (by name) in cart, check if the parameter has default values for description, price, and quantity. * If not, iR modifies item in cart. */ if(k.getName().equalsIgnoreCase(k.getName())) { if(k.getQuantity()!=0){ k.setQuantity(k.getQuantity()); } found = true; break; } } if(!found) // If an item cannot be found (by name) in cart. System.out.println("Item not found in cart. Nothing removed"); } /* * This method returns quantity of all items in cart. It has no parameters. */

public int getNumItemsInCart() { int sum = 0; for(ItemToPurchase i: cartItems){ sum = sum + i.getQuantity(); } return sum; }

/* * This method determines and returns the total cost of items in cart. It has no parameters. */

public double getCostOfCart() { int sum = 0; for(ItemToPurchase i: cartItems){ sum = sum + i.getPrice();

} return sum; }

public void printTotal(){ // This method outputs total of objects in the cart.

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();

}

else{ System.out.println("SHOPPING CART IS EMPTY"); }

} public void printDescriptions() { // This method outputs each item's description.

System.out.println(customerName+"'s Shopping Cart - "+currentDate); System.out.println("Number of Items: " + cartItems.size()); System.out.println(); System.out.println("Item Descriptions");

/*

System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Cost); System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Cost); // get the tax double tax = item1.totalTax() + item2.totalTax(); System.out.printf("Tax = $%.2f ",tax); // display the tax formatted to 2 decimal places // Add both item cost and tax to get the total cost double totalCost = item1Cost + item2Cost + tax; //Display the total cost of both items. System.out.printf(" Total: $%.2f", totalCost);

*/

}

}

ShoppingCartManager.java:-

package main;

import java.util.Scanner;

public class ShoppingCartManager { public static void main(String[] args){ // Prompts the user for 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 y = new ShoppingCart(); ShoppingCart x = new ShoppingCart(customerName, currentDate,0,0); System.out.println(); // Prints 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){ // This method prints the menu while(true){ System.out.println("MENU"); System.out.println("a - Add item to cart"); System.out.println("b - Remove item from cart"); System.out.println("c - Change item quantity"); System.out.println("d - Apply Coupon Code "); // So you have to created Else if statement below for applying coupon code 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); // The User chooses an option char input = scanner.next().charAt(0); scanner.nextLine(); // Adding an item to the cart if(input == 'a'){ System.out.println("ADD ITEM TO CART"); System.out.println("Enter Item Name: "); String itemName = 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 itemQuantity = scanner.nextInt(); scanner.nextLine(); ItemToPurchase item = new ItemToPurchase(); x.addItem(item);

}

// Removing an item from the cart else if(input == 'b'){ System.out.println("REMOVE ITEM FROM CART"); System.out.println("Enter name of item to remove: "); String name = scanner.nextLine(); x.removeItem(name); }

// Changing the quantity of an item 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); }

// Printing items' descriptions else if(input == 'i'){ System.out.println("OUTPUT ITEMS' DESCRIPTIONS"); x.printDescriptions(); }

// printing the shopping cart else if(input == 'o'){ System.out.println("OUTPUT SHOPPING CART"); x.printTotal(); x.getCostOfCart(); }

// Quit else if(input == 'q'){ break; } } } }

ItemToPurchase.java :-

package main; enum ItemTaxCategory { N,// no tax for item X,// 8% T;//10% } public class ItemToPurchase { private String itemName; private int itemPrice; private int itemQuantity; private String itemDiscription; private ItemTaxCategory category; public ItemToPurchase(){ this.itemName = "none"; this.itemPrice = 0; this.itemQuantity = 0; this.itemDiscription=""; this.category = ItemTaxCategory.N; }

/** * @return the itemName */ public String getName() { return itemName; }

/** * @param itemName the itemName to set */ public void setName(String itemName) { this.itemName = itemName; }

/** * @return the itemPrice */ public int getPrice() { return itemPrice; }

/** * @param itemPrice the itemPrice to set */ public void setPrice(int itemPrice) { this.itemPrice = itemPrice; }

/** * @return the itemQuantity */ public int getQuantity() { return itemQuantity; }

/** * @param itemQuantity the itemQuantity to set */ public void setQuantity(int itemQuantity) { this.itemQuantity = itemQuantity; }

/** * @return the category */ public ItemTaxCategory getTaxCategory() { return category; }

/** * @param category the category to set */ public void setTaxCategory(ItemTaxCategory category) { this.category = category; } public double itemTotalPrice() { return getQuantity() * getPrice(); } public double totalTax() { double tax; if(category == ItemTaxCategory.X) { tax = 0.08 * getPrice(); }else if(category == ItemTaxCategory.T) { tax = 0.10 * getPrice(); }else { tax = 0; } return tax; } }

I want to make a new method in Shoppingcart.java that gives discount as mentioned in the following screenshot:-

and mentain trend between ShopingCart and ShopingCartManager

image text in transcribedimage text in transcribed

stain viruses. Unless you need to edit, it's safer to stay in Protected View. Enable Editing Enter name of item to remove: Chocolate Chips 9. Implement Change item quantity menu option. Output Example: CHANGE ITEM QUANTITY Enter the item name: Nike Romaleos Enter the new quantity: 3 10. Implement the Apply coupon code menu option. When an invalid coupon code or a coupon code which doesn't meet the condition is applied, it should be rejected with the message "INVALID CODE'. Else, display the message 'COUPON APPLIED'. Output Example: APPLY COUPON CODE Enter coupon code: 200FF INVALID CODE Enter coupon code: 100FF COUPON APPLIED Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem( uantity. If not, modify item in cart. o If item cannot be found (by name) in cart, output this message: Item not fc cart. Nothing modified getNumItemsInCart() 0 Returns quantity of all items in cart. Has no parameters. getCostOfCarto O Determines and returns the total cost of items in cart. Has no parameters. applyCoupon Code A coupon code can be applied based on the following conditions: 100FF - $10 off the total price if current total exceeds $50 200FF - $20 off the total price if current total exceeds $100 300FF - $20 off the total price if current total exceeds $150 . O O O print Total outputs total of objects in cart. Apply any applicable coupon codes. If invalid coupon code is applied, output the message: INVALID CODE O

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

10-4: What is creativity, and what fosters it?

Answered: 1 week ago