In: Computer Science
9.25 LAB*: Program: Online shopping cart (Part 2)
Note: Creating multiple Scanner objects for the same input
stream yields unexpected behavior. Thus, good practice is to use a
single Scanner object for reading input from System.in. That
Scanner object can be passed as an argument to any methods that
read input.
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).
(1) Extend the ItemToPurchase class per the following specifications:
Ex. of printItemCost() output:
Bottled Water 10 @ $1 = $10
Ex. of printItemDescription() output:
Bottled Water: Deer Park, 12 oz.
(2) Create two new files:
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
Default constructor
Parameterized constructor which takes the customer name and date as parameters (1 pt)
Public member methods
Ex. of printTotal() output:
John Doe's Shopping Cart - February 1, 2016 Number of Items: 8 Nike Romaleos 2 @ $189 = $378 Chocolate Chips 5 @ $3 = $15 Powerbeats 2 Headphones 1 @ $128 = $128 Total: $521
Ex. of printDescriptions() output:
John Doe's Shopping Cart - February 1, 2016 Item Descriptions Nike Romaleos: Volt color, Weightlifting shoes Chocolate Chips: Semi-sweet Powerbeats 2 Headphones: Bluetooth headphones
(3) 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. (1 pt)
Ex.
Enter Customer's Name: John Doe Enter Today's Date: February 1, 2016 Customer Name: John Doe Today's Date: February 1, 2016
(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. (3 pts)
Ex:
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:
(5) Implement Output shopping cart menu option. (3 pts)
Ex:
OUTPUT SHOPPING CART John Doe's Shopping Cart - February 1, 2016 Number of Items: 8 Nike Romaleos 2 @ $189 = $378 Chocolate Chips 5 @ $3 = $15 Powerbeats 2 Headphones 1 @ $128 = $128 Total: $521
(6) Implement Output item's description menu option. (2 pts)
Ex.
OUTPUT ITEMS' DESCRIPTIONS John Doe's Shopping Cart - February 1, 2016 Item Descriptions Nike Romaleos: Volt color, Weightlifting shoes Chocolate Chips: Semi-sweet Powerbeats 2 Headphones: Bluetooth headphones
(7) Implement Add item to cart menu option. (3 pts)
Ex:
ADD ITEM TO CART Enter the item name: Nike Romaleos Enter the item description: Volt color, Weightlifting shoes Enter the item price: 189 Enter the item quantity: 2
(8) Implement Remove item menu option. (4 pts)
Ex:
REMOVE ITEM FROM CART Enter name of item to remove: Chocolate Chips
(9) Implement Change item quantity menu option. Hint: Make new
ItemToPurchase object and use ItemToPurchase modifiers before using
modifyItem() method. (5 pts)
Ex:
CHANGE ITEM QUANTITY Enter the item name: Nike Romaleos Enter the new quantity: 3
First code below, please help me edit this code to fit the parameters set above.:
ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name:");
String name1 = scan.nextLine();
System.out.println("Enter the item price:");
int price1 = scan.nextInt();
System.out.println("Enter the item quantity:");
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
System.out.println("");
System.out.println("Item 2");
System.out.println("Enter the item name:");
String name2 = scan.nextLine();
System.out.println("Enter the item price:");
int price2 = scan.nextInt();
System.out.println("Enter the item quantity:");
int quantity2 = scan.nextInt();
System.out.println("");
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item1.getName()+" "+item1.getQuantity()+" @
$"+item1.getPrice()+" = $"+item1Total);
System.out.println(item2.getName()+" "+item2.getQuantity()+" @
$"+item2.getPrice()+" = $"+item2Total);
System.out.println();
System.out.println("Total: $"+(item1Total + item2Total));
}
}
ItemToPurchase.java
public class ItemToPurchase {
private String itemName ;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase(){
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
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;
}
}
Short Summary:
Implemented the program as per requirement
Attached source code and sample output
**************Please do upvote to appreciate our time.
Thank you!******************
Source Code:
ItemToPurchase.java
public class ItemToPurchase {
private String itemDescription;
private String itemName ;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase(){
itemDescription="none";
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
public ItemToPurchase(String itemDescription,
String itemName, int itemPrice, int itemQuantity) {
this.itemDescription =
itemDescription;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity =
itemQuantity;
}
//printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
void printItemCost()
{
//Eg., Bottled Water 10 @ $1 =
$10
System.out.println(itemName+"
"+itemQuantity+" @ $"+itemPrice+" =
$"+(itemPrice*itemQuantity));
}
//printItemDescription() - Outputs the item name and description
void printItemDescription()
{
//Bottled Water: Deer Park, 12
oz.
System.out.println(itemName+":
"+itemDescription);
}
public String getName() {
return itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String
itemDescription) {
this.itemDescription =
itemDescription;
}
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;
}
}
ShoppingCart.java
package com.core.java;
import java.util.ArrayList;
//This class holds customer info and cart details
public class ShoppingCart {
private String customerName;
private String currentDate;
ArrayList<ItemToPurchase> cartItems=new
ArrayList<>();
//Default cons
public ShoppingCart()
{
customerName="none";
currentDate="January 1,
2016";
}
//Parameter cons
public ShoppingCart(String customerName, String
currentDate) {
super();
this.customerName =
customerName;
this.currentDate =
currentDate;
}
//Accessors
public String getCustomerName() {
return customerName;
}
public String getCurrentDate() {
return currentDate;
}
//This method is used to add item to cart
public void addItem(ItemToPurchase item)
{
cartItems.add(item);
}
//This method is used to remove item to cart
public void removeItem(String itemName)
{
boolean found=false;
ItemToPurchase item=null;
for(ItemToPurchase
item1:cartItems)
{
if(item1.getName().equalsIgnoreCase(itemName))
{
found=true;
item=item1;
}
}
cartItems.remove(item);
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
itemObj:cartItems)
{
if(itemObj.getName().equalsIgnoreCase(item.getName()))
{
found=true;
if(!(item.getItemDescription().equalsIgnoreCase("none") &&
item.getPrice()==0 && item.getQuantity()==0))
{
itemObj.setQuantity(item.getQuantity());
}
}
}
if(!found)
System.out.println("Item not found in cart. Nothing
modified.");
}
//Returns quantity of all items in cart. Has no parameters.
public int getNumItemsInCart()
{
int sum=0;
for(ItemToPurchase
itemObj:cartItems)
{
sum=sum+itemObj.getQuantity();
}
return sum;
}
//Determines and returns the total cost of items in
cart. Has no parameters.
public double getCostOfCart()
{
double cost=0;
for(ItemToPurchase
itemObj:cartItems)
{
cost=cost+itemObj.getPrice();
}
return cost;
}
//Outputs total of objects in cart.
public void printTotal()
{
if(cartItems.isEmpty())
System.out.println("SHOPPING CART IS EMPTY");
else
{
System.out.println(customerName+"'s Shopping Cart -
"+currentDate);
System.out.println("Number of Items: "+getNumItemsInCart());
for(ItemToPurchase itemObj:cartItems)
{
itemObj.printItemCost();
}
System.out.println("Total: $"+getCostOfCart());
}
}
//Outputs each item's description.
public void printDescriptions()
{
System.out.println(customerName+"'s Shopping Cart -
"+currentDate);
System.out.println("Item
Descriptions");
for(ItemToPurchase
itemObj:cartItems)
{
System.out.println(itemObj.getItemDescription());
}
}
}
ShoppingCartManager.java
package com.core.java;
import java.util.Scanner;
/** This class gets user input and modifies shopping cart**/
public class ShoppingCartManager {
static Scanner input=new Scanner(System.in);
public static void main(String[] args) {
//Scanner to get user input
System.out.println("Enter
Customer's Name:");
String custName=input.nextLine();
//Get customer name
System.out.println("Enter Today's
Date:");
String date=input.nextLine(); //Get
date
System.out.println("Customer Name:
"+custName);//Print
System.out.println("Today's Date:
"+date);
ShoppingCart cart=new
ShoppingCart(custName, date); //Create cart
//Print menu to user
printMenu(cart);
input.close();
}
//This method displays menu to user
static void printMenu(ShoppingCart cart)
{
//Initialize to any char
char userInput='a';
//Loop until user enters quit
while(userInput!='Q')
{
System.out.println("MENU\r\n" +
"a - Add item to cart\r\n"
+
"d - Remove item from
cart\r\n" +
"c - Change item
quantity\r\n" +
"i - Output items'
descriptions\r\n" +
"o - Output shopping
cart\r\n" +
"q - Quit\r\n" +
"\r\n" +
"Choose an option: ");
//Get
input
userInput=input.nextLine().charAt(0);
//Switch for
different inputs
switch(userInput)
{
case 'o':
System.out.println("OUTPUT SHOPPING
CART");
cart.printTotal();
break;
case 'i':
System.out.println("OUTPUT ITEMS'
DESCRIPTIONS");
cart.printDescriptions();
break;
case 'a':
System.out.println("Enter the item
name:");
String itemName=input.nextLine();
System.out.println("Enter the item
description:");
String desc=input.nextLine();
System.out.println("Enter the item
price:");
int price=input.nextInt();
input.nextLine();
System.out.println("Enter the item
quantity:");
int quan=input.nextInt();
input.nextLine();//skip line
ItemToPurchase item=new ItemToPurchase(desc,
itemName, price, quan); //create new object
cart.addItem(item); //add to cart
break;
case 'd':
System.out.println("REMOVE ITEM FROM CART"
);
System.out.println("Enter name of item to
remove:");
String itemNm=input.nextLine();
cart.removeItem(itemNm);
case 'c':
System.out.println("CHANGE ITEM QUANTITY\n"
+
"Enter the
item name:\n" );
itemName=input.nextLine();
System.out.println("Enter the new quantity:\n"
);
quan=input.nextInt();
input.nextLine();
ItemToPurchase item1=new ItemToPurchase();
item1.setQuantity(quan);
item1.setName(itemName);
cart.modifyItem(item1);
}
}
input.close();
}
}
Output:
**************Please do upvote to appreciate our time. Thank you!******************