This is my code so far. Shopping Cart Manager: import java.util.Scanner; public
ID: 3604686 • Letter: T
Question
This is my code so far.
Shopping Cart Manager:
import java.util.Scanner;
public class ShoppingCartManager {
public static void main(String[] args)
{
String customerName, currentDate;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Customer's Name:");
customerName = sc.nextLine().toString().trim();
System.out.println();
System.out.print("Enter Today's Date:");
currentDate = sc.nextLine().toString().trim();
System.out.println();
ShoppingCart cart = new ShoppingCart(customerName, currentDate);
System.out.println("Customer Name:" + cart.getcustomerName());
System.out.println("Today's Date:" + ShoppingCart.getcurrentDate());
printMenu(cart);
}
/**
* Prints the menu, and builds the shopping cart
* object passed in as parameter
*/
private static void printMenu(ShoppingCart cart)
{
Scanner sc = new Scanner(System.in);
char choice = 'z';
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' Description");
System.out.println("o - Output Shopping Cart");
System.out.println("q - Quit");
System.out.print("Enter an option:");
choice = sc.nextLine().trim().charAt(0);
while(choice != 'q')
{
if(choice != 'a' && choice != 'd' && choice != 'c' && choice != 'i' && choice != 'o')
{
//An invalid choice is made
//Prompt for choice again
System.out.print("Enter an option:");
choice = sc.nextLine().trim().charAt(0);
continue;
}
else
{
switch (choice)
{
case 'o':
{
System.out.println("OUTPUT SHOPPING CART");
cart.printTotal();
break;
}
case 'i':
{
System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
cart.printDescriptions();
break;
}
case 'a':
{
String name, desc;
int price, qty;
System.out.println("ADD ITEM TO CART");
System.out.println("Enter Item name:");
name = sc.nextLine().trim().toString();
System.out.println("Enter Item description:");
desc = sc.nextLine().trim().toString();
System.out.println("Enter Item price:");
price = sc.nextInt();
sc.nextLine();
System.out.println("Enter Item quantity:");
qty = sc.nextInt();
sc.nextLine();
ItemToPurchase itemToPurchase = new ItemToPurchase(name, desc, qty, price);
cart.addItem(itemToPurchase);
break;
}
case 'd':
{
String itemToRemove;
System.out.println("REMOVE ITEM FROM CART");
System.out.println("Enter name of item to remove:");
itemToRemove = sc.nextLine().trim().toString();
cart.removeItem(itemToRemove);
break;
}
case 'c':
{
String itemName;
int itemQty;
System.out.println("CHANGE ITEM QUANTITY");
System.out.println("Enter item name:");
itemName = sc.nextLine().trim().toString();
System.out.println("Enter the new quantity:");
itemQty = sc.nextInt();
sc.nextLine();
ItemToPurchase item = new ItemToPurchase(itemName, "none", itemQty, 0);
cart.modifyItem(item);
break;
}
}
System.out.print("Enter an option:");
choice = sc.nextLine().trim().charAt(0);
}
}
}
}
ShoppingCart:
import java.util.ArrayList;
public class ShoppingCart
{
// Private fields
private String customerName;
private String currentDate;
private ArrayList<ItemToPurchase> cartItems;
public ShoppingCart()
{
this.customerName = "none";
this.currentDate = "January 1, 2016";
cartItems = new ArrayList<ItemToPurchase>();
}
public ShoppingCart(String customerName, String currentDate)
{
this.customerName = customerName;
this.currentDate = currentDate;
cartItems = new ArrayList<ItemToPurchase>();
}
public String getCustomerName()
{
return customerName;
}
public String getCurrentDate()
{
return currentDate;
}
public void addItem(ItemToPurchase item)
{
cartItems.add(item);
}
public void removeItem(String itemName)
{
boolean found = false;
for (int i = 0; i < cartItems.size(); i++)
{
if (cartItems.get(i).getItemName().
equalsIgnoreCase(itemName))
{
cartItems.remove(i);
found = true;
break;
}
}
if (!found)
System.out.println("Item not found in"
+ " cart. Nothing removed.");
}
public void modifyItem(ItemToPurchase item)
{
if (!((item.getItemDescription().
equalsIgnoreCase("none")) &&
(item.getItemPrice() == 0)
&& (item.getItemQuantity() == 0)))
{
boolean found = false;
for (int i = 0; i < cartItems.size(); i++)
{
ItemToPurchase cartItem = cartItems.get(i);
if (cartItem.getItemName().equalsIgnoreCase
(item.getItemName()))
{
cartItem.setItemQuantity(item.getItemQuantity());
found = true;
break;
}
}
if (!found)
System.out.println("Item not found in"
+ " cart. Nothing modified.");
}
}
public int getNumItemsInCart()
{
int numItems = 0;
for (ItemToPurchase item : cartItems)
{
numItems += item.getItemQuantity();
}
return numItems;
}
public int getCostOfCart()
{
int ttlCost = 0;
for (ItemToPurchase item : cartItems)
{
ttlCost += item.getItemQuantity() *
item.getItemPrice();
}
return ttlCost;
}
public void printTotal()
{
if (cartItems.size() == 0)
System.out.println("SHOPPING CART IS EMPTY");
else {
System.out.println(this.customerName + "'s"
+ " Shopping Cart - " + this.currentDate);
System.out.println("Number of Items: "
+ this.cartItems.size());
for (ItemToPurchase item : cartItems) {
item.printItemCost();
}
System.out.println("Total: $" + getCostOfCart());
}
}
public void printDescriptions()
{
if (cartItems.size() == 0)
System.out.println("SHOPPING CART IS EMPTY");
else
{
System.out.println(this.customerName + "'s "
+ "Shopping Cart - " + this.currentDate);
System.out.println("Item Descriptions");
for (ItemToPurchase item : cartItems)
{
item.printItemDescription();
}
}
}
}
ItemsToPurchase:
public class ItemToPurchase
{
// Declare the private fields
private String itemName;
private int itemPrice;
private int itemQuantity;
private String itemDescription;
// DEFAULT CONSTRUCTOR
public ItemToPurchase()
{
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
this.itemDescription = "none";
}
public ItemToPurchase(String itemName,
int itemPrice, int itemQuantity,
String itemDescription)
{
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
this.itemDescription = itemDescription;
}
public String getItemName()
{
return itemName;
}
public void setItemName(String itemName)
{
this.itemName = itemName;
}
public int getItemPrice()
{
return itemPrice;
}
public void setItemPrice(int itemPrice)
{
this.itemPrice = itemPrice;
}
public int getItemQuantity()
{
return itemQuantity;
}
public void setItemQuantity(int itemQuantity)
{
this.itemQuantity = itemQuantity;
}
public String getItemDescription()
{
return itemDescription;
}
public void setItemDescription(
String itemDescription)
{
this.itemDescription = itemDescription;
}
public void printItemCost()
{
System.out.println(this.itemName + " "
+ this.itemQuantity + " @ $"
+ this.itemPrice + " = $"
+ (this.itemQuantity * this.itemPrice));
}
public void printItemDescription()
{
System.out.println(this.itemName + ": "
+ this.itemDescription);
}
}
I cannot seem to get this to work. The program that assess it's accuracy is extremely strict. Cannot import any utilities other than the ones I have.
7.20 Ch 7 Program: Online shopping cart (continued) (Java) Note: Crealing muliple Scanner objecis for the sarne input strea yields unexpecled behavior. Thus, good practice is to use a single Scanner objecl for reading inpul from Systein. Thal Scanner objecl can be passed as an argumenl lo any melhods tha! read inpul This program extends the earlier Online shopping cart program. (Consider tirst saving your earlier program) (1) Extend the ItemToPurchase class per the following specifications .Private tields . string itemDescription- Initialized in default constructor ta 'hone . Parameterized constructor to assign item name, item d scription, item prin , and item quantity (derault values of 0) (1 pt) . Public member method:s .selDescriplionmulalor& gelDescriplion accessor (2 pls) printltemCost)- Outputs the item nane followed by the quantity, price, and subtotal printltemDescription-Outputs the item name and description Ex. of printltenCost output Eottled water 10 1 -10 Cx. of printltemDescription) output Bottled Nater D Park, 12 oz (2) Creale lwo new files ·ShoppingCarljava . Class definition ShoppingCartManagerjava Contains main) method Build the ShoppingCart class with the following specifications. Note: Some can be method stubs (empty methods) initially, to be completerd in later steps . Private fields .String customerName Initialized in default constuctor to 'none" . String crrentDate Initialized in default constructor to January 1, 2016 .ArrayList cartliterms . Default constructor . Paramelried constructor which lakes the customer name ad dale as pararnelers (1 pl) . Public member mcthods .getCustomerName) accessor (1 pt) . getDate0 accessor (1 pt) . additem . removeltem Adds an item to cartitems array Has parameter ltemToPurchase. Does not return anything. Rernoves item from cart llems array. Has a string (an iiris name) pararreter Does not return anything. Inarie canot be found, outpui this message: Item not found in cart, Nothing removed modifyltem) o Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Docs 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 tound (by name) in cart, output this message: Item not found in cart. Nothing modified . getNumltemsinCart0(2 pts) Returns quantity of all iterns in cart. Has no pararnelers. .gelCoslOCar0 2 pls) Determines and returns the total cost of iterns in cart. Has no parameters printTotal0 Outputs total of objects in cart. If cart is empty, output this message. SHOPPING CART IS EMPTY printDescriptions) Outputs each items descriptionExplanation / Answer
ShoppingCartManager.java
import java.util.Scanner;
public class ShoppingCartManager {
public static void main(String[] args)
{
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 s = new ShoppingCart(customerName, currentDate);
System.out.println();
System.out.println("Customer Name: "+s.getCustomerName());
System.out.println("Today's Date: "+currentDate);
printMenu(s);
}
public static void printMenu(ShoppingCart s){
while(true){
System.out.println("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 Choose an option: ");
Scanner scan = new Scanner(System.in);
char ch = scan.next().charAt(0);
scan.nextLine();
if(ch == 'a' || ch == 'A' ){
System.out.println("ADD ITEM TO CART");
System.out.println("Enter Item Name: ");
String name = scan.nextLine();
System.out.println("Enter Item Description: ");
String itemDescritpion = scan.nextLine();
System.out.println("Enter Item Price: ");
int itemPrice = scan.nextInt();
System.out.println("Enter Item Quantity: ");
int quantity = scan.nextInt();
scan.nextLine();
ItemToPurchase item = new ItemToPurchase(name, itemDescritpion, itemPrice, quantity);
s.addItem(item);
}
else if(ch == 'd' || ch == 'D'){
System.out.println("REMOVE ITEM FROM CART");
System.out.println("Enter name of item to remove: ");
String name = scan.nextLine();
s.removeItem(name);
}
else if(ch == 'c' || ch == 'C'){
System.out.println("CHANGE ITEM QUANTITY");
System.out.println("Enter the item name: ");
String name = scan.nextLine();
System.out.println("Enter the new quantity: ");
int quantity = scan.nextInt();
ItemToPurchase item = new ItemToPurchase();
item.setName(name);
item.setItemQuantity(quantity);
s.modifyItem(item);
}
else if(ch == 'I' || ch == 'i'){
System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
s.printDescriptions();
}
else if(ch == 'O' || ch == 'o'){
System.out.println("OUTPUT SHOPPING CART");
s.printTotal();
}
else if(ch == 'q' || ch == 'Q'){
break;
}
}
}
}
ItemToPurchase.java
public class ItemToPurchase {
private String itemDescription;
private String name;
private double itemPrie;
private int itemQuantity;
public ItemToPurchase() {
itemDescription = "none";
itemQuantity = 0;
itemPrie = 0;
name = "";
}
public ItemToPurchase(String name, String description, double price, int itemQuantity) {
this.itemDescription = description;
this.name = name;
this.itemPrie = price;
this.itemQuantity = itemQuantity;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public void printItemCost() {
System.out.println(name+" "+itemQuantity+" @ "+itemPrie+" = $"+(itemQuantity * itemPrie) );
}
public void printItemDescription() {
System.out.println(name+" "+itemDescription);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getItemPrie() {
return itemPrie;
}
public void setItemPrie(double itemPrie) {
this.itemPrie = itemPrie;
}
public int getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
ShoppingCart.java
import java.util.ArrayList;
public class ShoppingCart {
private String customerName;
private String currentDate ;
ArrayList<ItemToPurchase> cartItems = new ArrayList<ItemToPurchase>();;
public ShoppingCart() {
currentDate = "January 1, 2016";
customerName = "none";
}
public ShoppingCart(String name , String date) {
this.currentDate = date;
this.customerName = name;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
}
public void addItem( ItemToPurchase i) {
cartItems.add(i);
}
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)
System.out.println("Item not found in cart. Nothing removed");
}
public void modifyItem(ItemToPurchase item) {
boolean found = false;;
for(ItemToPurchase i: cartItems){
if(i.getName().equalsIgnoreCase(item.getName())) {
if(i.getItemQuantity()!=0){
i.setItemQuantity(item.getItemQuantity());
}
found = true;
break;
}
}
if(!found)
System.out.println("Item not found in cart. Nothing removed");
}
public int getNumItemsInCart() {
int sum = 0;
for(ItemToPurchase i: cartItems){
sum = sum + i.getItemQuantity();
}
return sum;
}
public double getCostOfCart() {
double sum = 0;
for(ItemToPurchase i: cartItems){
sum = sum + i.getItemPrie();
}
return sum;
}
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");
}
}
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();
}
}
}
Output:
Enter Customer's Name:
Suresh
Enter Today's Date:
11/11/11
Customer Name: Suresh
Today's Date: 11/11/11
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
Choose an option:
a
ADD ITEM TO CART
Enter Item Name:
sfsdfsd
Enter Item Description:
bvcbcv
Enter Item Price:
333
Enter Item Quantity:
2
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
Choose an option:
i
OUTPUT ITEMS' DESCRIPTIONS
Suresh's Shopping Cart - 11/11/11
Number of Items: 1
Item Descriptions
sfsdfsd bvcbcv
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
Choose an option:
o
OUTPUT SHOPPING CART
Suresh's Shopping Cart - 11/11/11
Number of Items: 1
sfsdfsd 2 @ 333.0 = $666.0
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
Choose an option:
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.