school/Week-11-final-start/part-2/ShoppingCart.java
2020-11-11 11:54:52 -05:00

88 lines
2.1 KiB
Java

import java.util.ArrayList;
public class ShoppingCart {
private String customerName;
private String currentDate;
private ArrayList<ItemToPurchase> cartItems;
ShoppingCart (){
customerName = "none";
currentDate = "January 1, 2016";
}
ShoppingCart (String name, String date){
customerName = name;
currentDate = date;
}
public String getCustomerName(){
return customerName;
}
public String getDate(){
return currentDate;
}
private void printNameDate (){
System.out.println(customerName + " Shopping Cart - "+ currentDate);
}
public void addItem(ItemToPurchase item){
cartItems.add(item);
}
public void removeItem(String itemName){
for (int i=0; i<cartItems.size(); i++){
if (cartItems.get(i).getName() == itemName){
cartItems.remove(i);
return;
}
}
System.out.println("Item not found in cart. Nothing removed.");
}
public void modifyItem(ItemToPurchase item){
//Modify desc, price, and/or quantity
//If item can be found by name in cart
//check if parameter has default values
//for desc, price, and quantity
//if not, modify item in cart
//if item cannot be found by name,
//"Item not found in cart. Nothing modified."
}
public int getNumItemsInCart(){
return cartItems.size();
}
public int getCostOfCart(){
int runningTotal = 0;
for (int i=0; i<cartItems.size(); i++){
runningTotal += cartItems.get(i).getPrice();
}
return runningTotal;
}
public void printTotal(){
if (cartItems.size() == 0){
System.out.println("SHOPPING CART IS EMPTY");
return;
}
printNameDate();
System.out.println("Number of Items: " + cartItems.size());
System.out.println();
for (int i=0; i<cartItems.size(); i++){
System.out.println(cartItems.get(i).getName() + " " +
cartItems.get(i).getQuantity() + " @ $"+
cartItems.get(i).getPrice() + " = $" +
(cartItems.get(i).getPrice() *
cartItems.get(i).getQuantity()));
}
System.out.println("\nTotal: " + getCostOfCart());
}
public void printDescriptions(){
printNameDate();
System.out.println("\n Item Descriptions");
for (int i=0; i<cartItems.size(); i++){
cartItems.get(i).printItemDescription();
}
}
}