In: Computer Science
IN JAVA PLEASE!!!
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.
(1) Create an 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
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
getDate() accessor
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.
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.
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.
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.
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.
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.
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.
Ex:
CHANGE ITEM QUANTITY Enter the item name: Nike Romaleos Enter the new quantity: 3
/*** ItemToPurchase.java ***/
public class ItemToPurchase
{
private String itemName = "";
private int itemPrice = 0;
private int itemQuantity = 0;
private int quantity = 0;
private int price = 0;
private String itemDescription = "none";
public ItemToPurchase()
{
this.itemDescription =
"none";
this.itemName = "";
this.itemPrice = 0;
this.itemQuantity = 0;
}
public ItemToPurchase(String itemName, String
itemDescription, int itemPrice, int itemQuantity )
{
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity =
itemQuantity;
this.itemDescription =
itemDescription;
}
public void setName(String itemName)
{
this.itemName = itemName;
}
public String getName()
{
return itemName;
}
public void setPrice(int itemPrice)
{
this.itemPrice = itemPrice;
}
public int getPrice()
{
return itemPrice;
}
public void setQuantity(int itemQuantity)
{
this.itemQuantity =
itemQuantity;
}
public int getQuantity()
{
return itemQuantity;
}
//Part 2 public member methods
public void setDescription(String
itemDescription)
{
this.itemDescription =
itemDescription;
}
public String getDescription()
{
return itemDescription;
}
public void printItemCost()
{
System.out.println(itemName + " " +
itemQuantity + "@ $" + itemPrice + " = $" + (itemQuantity *
itemPrice));
}
public void printItemDescription()
{
System.out.println(itemName +": " +
itemDescription);
}
}
/*** ShoppingCart.java ***/
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingCart
{
private String customerName;
private String currentDate;
private ArrayList<ItemToPurchase>
cartItems;
Scanner scnr = new Scanner(System.in);
//Default Constructor
public ShoppingCart()
{
customerName = "none";
currentDate = "January 1,
2016";
cartItems = new
ArrayList<ItemToPurchase>();
}
//Pramaterized Constructor/Overloader
public ShoppingCart(String customerName, String
currentDate)
{
this.customerName =
customerName;
this.currentDate =
currentDate;
cartItems = new
ArrayList<ItemToPurchase>();
}
//Public member methods
public String getCustomerName()
{
return customerName;
}
public String getDate()
{
return currentDate;
}
public void addItem(ItemToPurchase item)
{
cartItems.add(item);
}
public void removeItem(String itemName)
{
int found = 0;
for (int i = 0; i <
cartItems.size(); i++)
{
if((cartItems.get(i).getName().equals(itemName)))
{
cartItems.remove(i);
return;
}
}
System.out.println("Item not found
in cart. Nothing removed.");
}
public void modifyItem(String ItemToPurchase)
{
int found = 0;
for (int i = 0; i <
cartItems.size(); i++)
{
if((cartItems.get(i).getName().equals(ItemToPurchase)))
{
System.out.println("Enter the new
quantity");
int amount = scnr.nextInt();
cartItems.get(i).setQuantity(amount);
return;
}
}
System.out.println("Item not found
in cart. Nothing modified.");
}
public int getNumItemsInCart()
{
int totalQuant = 0;
for (int i = 0; i <
cartItems.size(); i++)
{
totalQuant =
totalQuant + cartItems.get(i).getQuantity();
}
return totalQuant;
}
public int getCostOfCart()
{
int totalCost = 0;
for (int i = 0; i <
cartItems.size(); i++)
{
totalCost =
totalCost + (cartItems.get(i).getQuantity() *
cartItems.get(i).getPrice());
}
return totalCost;
}
public void printTotal()
{
System.out.println(this.customerName + "Shopping Cart" +
currentDate);
if(cartItems != null &&
!cartItems.isEmpty()) {
System.out.println("Number of
items: " + getNumItemsInCart());
for(ItemToPurchase item:
cartItems)
{
item.printItemCost();
}
System.out.println();
System.out.println("Total:" + "$" +
getCostOfCart());
}else {
System.out.println("Empty
Cart");
}
}
public void printDescriptions()
{
System.out.println(customerName +
"'s Shopping Cart - " + currentDate);
System.out.println("Item
Descriptions");
for (ItemToPurchase item :
cartItems)
{
item.printItemDescription();
}
}
}
/*** ShoppingCartManager.java ***/
import java.util.Scanner;
public class ShoppingCartManager
{
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args)
{
String customerName;
String currentDate;
System.out.println("Enter
Customer's Name:");
customerName =
scnr.nextLine();
System.out.println("Enter Today's
Date:");
currentDate =
scnr.nextLine();
ShoppingCart cartItems = new
ShoppingCart(customerName, currentDate);
System.out.println("");
System.out.println("Customer Name:
" + customerName);
System.out.println("Today's Date: "
+ currentDate);
printMenu(cartItems);
}
public static void printMenu(ShoppingCart
cartItems)
{
char select;
String userInput;
System.out.println("\nMENU");
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\n");
System.out.println("Choose an
option:");
select =
scnr.nextLine().charAt(0);
while (select != 'q' &&
select != 'a' && select != 'd' && select != 'c'
&& select != 'i' && select != 'o')
{
System.out.println("Choose an option:");
select =
scnr.nextLine().charAt(0);
}
switch (select)
{
case 'q':
System.exit(0);
break;
// i - Output
items' Description
case 'i':
System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
cartItems.printDescriptions();
break;
// o - Output
shopping cart
case 'o':
System.out.println("OUTPUT SHOPPING CART");
cartItems.printTotal();
break;
//a - Add item
to cart
case 'a':
String
Name;
String
Description;
int Price;
int
Quantity;
System.out.println("");
System.out.println("ADD ITEM TO CART");
System.out.println("Enter the item name:");
Name =
scnr.nextLine().trim().toString();
System.out.println("Enter the item description:");
Description =
scnr.nextLine().trim().toString();
System.out.println("Enter the item price:");
Price =
scnr.nextInt();
scnr.nextLine();
System.out.println("Enter the item quantity:");
Quantity =
scnr.nextInt();
scnr.nextLine();
ItemToPurchase
itemToPurchase = new ItemToPurchase(Name, Description, Price,
Quantity);
cartItems.addItem(itemToPurchase);
break;
// d- remove
item from cart
case 'd':
String
remove;
System.out.println("");
System.out.println("REMOVE ITEM FROM CART");
System.out.println("Enter name of item to remove:");
remove =
scnr.nextLine();
cartItems.removeItem(remove);
break;
// c - Change
item quantity
case 'c':
int quant;
String
Name2;
System.out.println("CHANGE ITEM QUANTITY");
System.out.println("Enter the item name:");
Name2 =
scnr.nextLine();
System.out.println("Enter the new quantity:");
quant =
scnr.nextInt();
scnr.nextLine();
cartItems.modifyItem(Name2);
break;
}
printMenu(cartItems);
}
}
Output:
Enter Customer's Name:
John Doe
Enter Today's Date:
February 1, 2016
Customer Name: John Doe
Today's Date: February 1, 2016
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 the item name:
Nike Romaleos
Enter the item description:
Volt color, Weightlifting shoes
Enter the item price:
189
Enter the 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:
o
OUTPUT SHOPPING CART
John DoeShopping CartFebruary 1, 2016
Number of items: 2
Nike Romaleos 2@ $189 = $378
Total:$378
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
Solving your question and helping you to well understand it
is my focus. So if you face any difficulties regarding this please
let me know through the comments. I will try my best to assist you.
However if you are satisfied with the answer please don't forget to
give your feedback.
Thank you.