Ch 7 Program: Online shopping cart (continued) (Java)
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).
(1) Extend the ItemToPurchase class per the following specifications:
Private fields
string itemDescription - Initialized in default constructor to "none"
Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
Public member methods
setDescription() mutator & getDescription() accessor (2 pts)
printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
printItemDescription() - Outputs the item name and description
Ex. of printItemCost() output:
Bottled Water 10 @ $1 = $10
Ex. of printItemDescription() output:
Bottled Water: Deer Park, 12 oz.
(2) Create two new files:
ShoppingCart.java - Class definition
ShoppingCartManager.java - Contains main() method
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 (1 pt)
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()
Removes 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 removed.
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.
getCostOfCart() (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.
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
MY CODE DOES NOT WORK PROPERLY AND I GET A
Exception in thread "main" java.lang.NullPointerException
at onlineShoppingCart.ShoppingCart.addItem(ShoppingCart.java:29)
at onlineShoppingCart.ShoppingCartManager.printMenu(ShoppingCartManager.java:77)
at onlineShoppingCart.ShoppingCartManager.main(ShoppingCartManager.java:134)
package onlineShoppingCart;
import java.util.Scanner;
public class ShoppingCartManager {
@SuppressWarnings("resource")
public static void printMenu(ShoppingCart shoppingCart) {
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");
System.out.println("\n\nChoose an option:");
// Create a new scanner.
Scanner scnr = new Scanner(System.in);
Scanner scnrInt = new Scanner(System.in);
// Declare a new item to purchase.
ItemToPurchase item = null;
String itemName = "";
String itemDescription = "";
int itemPrice = 0;
int itemQuantity = 0;
// A menu of options to manipulate the shopping cart.
String option = scnr.next();
boolean isEnter = option.equals("a") || option.equals("d") ||
option.equals("c") || option.equals("i") || option.equals("o") ||
option.equals("q");
if(isEnter) {
switch(option.charAt(0)) {
case 'a':
// Implementation for item to purchase.
scnr = new Scanner(System.in);
System.out.println("ADD ITEM TO CART");
System.out.println("Enter the item name:");
itemName = scnr.nextLine();
System.out.println("Enter the item description:");
itemDescription = scnr.nextLine();
System.out.println("Enter the item price:");
itemPrice = scnrInt.nextInt();
System.out.println("Enter the item quantity:");
itemQuantity = scnrInt.nextInt();
// Create a new item to purchase and add it to linked list of items.
item = new ItemToPurchase(itemName, itemDescription, itemPrice, itemQuantity);
shoppingCart.addItem(item);
printMenu(shoppingCart);
break;
case 'd':
System.out.println("REMOVE ITEM FROM CART");
shoppingCart.removeItem(itemName);
printMenu(shoppingCart);
break;
case 'c':
System.out.println("CHANGE ITEM QUANTITY");
shoppingCart.modifyItem(item);
printMenu(shoppingCart);
break;
case 'i': shoppingCart.printDescriptions();
printMenu(shoppingCart);
break;
case 'o': shoppingCart.getNumItemsInCart();
printMenu(shoppingCart);
break;
case 'q': break;
}
} else {
System.out.println("Invalid Choice!");
printMenu(shoppingCart);
}
}
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter Customer's Name:");
String customerName = scnr.nextLine();
System.out.println("Enter Today's Date:");
String currentDate = scnr.nextLine();
System.out.println("\n\nCustomer Name: " + customerName);
System.out.println("Today's Date: " + currentDate);
System.out.println("\n\n");
// Create a linked list shopping cart.
ShoppingCart shoppingCart = new ShoppingCart(customerName, currentDate);
// Print menu.
printMenu(shoppingCart);
}
}
package onlineShoppingCart;
import java.util.ArrayList;
public class ShoppingCart {
private String customerName;
private String currentDate;
private ArrayList cartItems;
public ShoppingCart() {
customerName = "none";
currentDate = "January 1, 2016";
}
public ShoppingCart(String customerName, String currentDate) {
this.customerName = customerName;
this.currentDate = currentDate;
}
public String getCustomerName() {
return customerName;
}
public String getDate() {
return currentDate;
}
public void addItem(ItemToPurchase item) {
cartItems.add(item);
}
public void removeItem(String itemName) {
while(!cartItems.isEmpty()) {
for(int i = 0; i < cartitems.size();="" i++)="">
if(cartItems.get(i).getName().equals(itemName)) {
cartItems.remove(i);
} else {
System.out.println("Item not found in cart. Nothing removed.");
}
}
}
}
public void modifyItem(ItemToPurchase item) {
while(!cartItems.isEmpty()) {
for(int i = 0; i < cartitems.size();="" i++)="">
if(cartItems.get(i).getName().equals(item.getName())) {
if(cartItems.get(i).getName().equals("none")) {
cartItems.get(i).setName(item.getName());
cartItems.get(i).setPrice(item.getPrice());
cartItems.get(i).setQuantity(item.getQuantity());
}
} else {
System.out.println("Item not found in cart. Nothing modified.");
}
}
}
}
public int getNumItemsInCart() {
int totalQuantity = 0;
while(!cartItems.isEmpty()) {
for(int i = 0; i < cartitems.size();="" i++)="">
totalQuantity += cartItems.get(i).getQuantity();
}
}
return totalQuantity;
}
public int getCostOfCart() {
int totalCost = 0;
while(!cartItems.isEmpty()) {
for(int i = 0; i < cartitems.size();="" i++)="">
totalCost += cartItems.get(i).getPrice();
}
}
return totalCost;
}
public void printTotal() {
if(!cartItems.isEmpty()) {
for(ItemToPurchase item : cartItems) {
System.out.println(item);
}
} else {
System.out.println("SHOPPING CART IS EMPTY");
}
}
public void printDescriptions() {
while(!cartItems.isEmpty()) {
for(int i = 0; i < cartitems.size();="" i++)="">
System.out.println(customerName + "'s Shopping Cart -" + currentDate);
System.out.println("\n\nItem Descriptions");
for(ItemToPurchase item : cartItems) {
item.printItemDescription();
}
}
}
}
}
package onlineShoppingCart;
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuantity;
// New fields
private String itemDescription;
public ItemToPurchase() {
this.itemName = "none";
this.itemDescription = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
// New constructor.
public ItemToPurchase(String itemName, String itemDescription, int itemPrice, int itemQuantity) {
this.itemName = itemName;
this.itemDescription = itemDescription;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
}
public void setName(String itemName) {
this.itemName = itemName;
}
public String getName() {
return this.itemName;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getPrice() {
return this.itemPrice;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
public int getQuantity() {
return this.itemQuantity;
}
// New methods.
public void setDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getDescription() {
return itemDescription;
}
public void printItemCost() {
System.out.println(itemName + " " + itemQuantity + " @ $" + itemPrice +
itemPrice*itemQuantity);
}
public void printItemDescription() {
System.out.println(itemName + ":" + itemDescription);
}
}
package onlineShoppingCart;
import java.util.Scanner;
public class ShoppingCartPrinter {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enther the item name:");
item1.setName(scnr.nextLine());
System.out.println("Enter the item price:");
item1.setPrice(scnr.nextInt());
System.out.println("Enter the item quantity");
item1.setQuantity(scnr.nextInt());
scnr.nextLine();
System.out.println("\n\nItem 2");
System.out.println("Enther the item name:");
item2.setName(scnr.nextLine());
System.out.println("Enter the item price:");
item2.setPrice(scnr.nextInt());
System.out.println("Enter the item quantity");
item2.setQuantity(scnr.nextInt());
// Print details.
System.out.println("\nTOTAL COST");
System.out.println(item1.getName() + " " + item1.getQuantity()
+ " @ $" + item1.getPrice() + " = $" + item1.getPrice()*item1.getQuantity());
System.out.println(item2.getName() + " " + item2.getQuantity()
+ " @ $" + item2.getPrice() + " = $" + item2.getPrice()*item2.getQuantity());
int totalCost = item1.getPrice()*item1.getQuantity() + item2.getPrice()*item2.getQuantity();
System.out.println("\n\nTotal: $" + totalCost);
}
}