101 lines
2.5 KiB
Java
101 lines
2.5 KiB
Java
import java.util.ArrayList;
|
|
|
|
public class ShoppingCart {
|
|
private String customerName;
|
|
private String currentDate;
|
|
private ArrayList<ItemToPurchase> cartItems =
|
|
new ArrayList<ItemToPurchase>();
|
|
|
|
public ShoppingCart (){
|
|
customerName = "none";
|
|
currentDate = "January 1, 2016";
|
|
}
|
|
public ShoppingCart (String name, String date){
|
|
customerName = name;
|
|
currentDate = date;
|
|
}
|
|
|
|
public String getCustomerName(){
|
|
return customerName;
|
|
}
|
|
|
|
public String getDate(){
|
|
return currentDate;
|
|
}
|
|
|
|
public 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++){
|
|
System.out.println("itemName: |"+itemName+"|");
|
|
System.out.println("Test: " + cartItems.get(i).getName());
|
|
if (cartItems.get(i).getName().equals(itemName)){
|
|
cartItems.remove(i);
|
|
return;
|
|
}
|
|
}
|
|
System.out.println("Item not found in cart. Nothing removed.");
|
|
}
|
|
public void modifyItem(ItemToPurchase item){
|
|
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.");
|
|
}
|
|
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() *
|
|
cartItems.get(i).getQuantity());
|
|
}
|
|
return runningTotal;
|
|
}
|
|
|
|
public void printTotal(){
|
|
printNameDate();
|
|
System.out.println("Number of Items: " + cartItems.size());
|
|
System.out.println();
|
|
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()));
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|