school/Week-11-final-start/part-2/ShoppingCart.java

101 lines
2.5 KiB
Java
Raw Normal View History

2020-11-11 10:46:50 -05:00
import java.util.ArrayList;
public class ShoppingCart {
private String customerName;
private String currentDate;
2020-11-11 14:30:19 -05:00
private ArrayList<ItemToPurchase> cartItems =
new ArrayList<ItemToPurchase>();
2020-11-11 10:46:50 -05:00
2020-11-11 14:30:19 -05:00
public ShoppingCart (){
2020-11-11 10:46:50 -05:00
customerName = "none";
currentDate = "January 1, 2016";
}
2020-11-11 14:30:19 -05:00
public ShoppingCart (String name, String date){
2020-11-11 10:46:50 -05:00
customerName = name;
currentDate = date;
}
public String getCustomerName(){
return customerName;
}
public String getDate(){
return currentDate;
}
2020-11-11 14:30:19 -05:00
public void printNameDate (){
2020-11-11 11:54:52 -05:00
System.out.println(customerName + " Shopping Cart - "+ currentDate);
}
2020-11-11 10:46:50 -05:00
public void addItem(ItemToPurchase item){
2020-11-11 11:54:52 -05:00
cartItems.add(item);
2020-11-11 10:46:50 -05:00
}
2020-11-11 14:30:19 -05:00
2020-11-11 10:46:50 -05:00
public void removeItem(String itemName){
2020-11-11 11:54:52 -05:00
for (int i=0; i<cartItems.size(); i++){
2020-11-11 14:30:19 -05:00
System.out.println("itemName: |"+itemName+"|");
System.out.println("Test: " + cartItems.get(i).getName());
if (cartItems.get(i).getName().equals(itemName)){
2020-11-11 11:54:52 -05:00
cartItems.remove(i);
return;
}
}
System.out.println("Item not found in cart. Nothing removed.");
2020-11-11 10:46:50 -05:00
}
public void modifyItem(ItemToPurchase item){
2020-11-11 14:30:19 -05:00
for (int i=0; i<cartItems.size(); i++){
if (cartItems.get(i).getName().equals(item.getName())){
if (item.getDescription() != "none"){
cartItems.get(i).setDescription(item.getDescription());
}
if (item.getPrice() != 0){
cartItems.get(i).setPrice(item.getPrice());
}
if (item.getQuantity() != 0){
cartItems.get(i).setQuantity(item.getQuantity());
}
return;
}
}
System.out.println("Item not found in cart. Nothing modified.");
2020-11-11 10:46:50 -05:00
}
public int getNumItemsInCart(){
2020-11-11 11:54:52 -05:00
return cartItems.size();
2020-11-11 10:46:50 -05:00
}
public int getCostOfCart(){
2020-11-11 11:54:52 -05:00
int runningTotal = 0;
for (int i=0; i<cartItems.size(); i++){
2020-11-11 14:30:19 -05:00
runningTotal += (cartItems.get(i).getPrice() *
cartItems.get(i).getQuantity());
2020-11-11 11:54:52 -05:00
}
return runningTotal;
2020-11-11 10:46:50 -05:00
}
2020-11-11 11:54:52 -05:00
2020-11-11 10:46:50 -05:00
public void printTotal(){
2020-11-11 15:26:52 -05:00
printNameDate();
System.out.println("Number of Items: " + cartItems.size());
System.out.println();
2020-11-11 11:54:52 -05:00
if (cartItems.size() == 0){
System.out.println("SHOPPING CART IS EMPTY");
}
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()));
}
2020-11-11 14:30:19 -05:00
System.out.println("\nTotal: $" + getCostOfCart());
2020-11-11 10:46:50 -05:00
}
public void printDescriptions(){
2020-11-11 11:54:52 -05:00
printNameDate();
System.out.println("\n Item Descriptions");
2020-11-11 10:46:50 -05:00
2020-11-11 11:54:52 -05:00
for (int i=0; i<cartItems.size(); i++){
cartItems.get(i).printItemDescription();
}
}
2020-11-11 10:46:50 -05:00
}