I did my best not to copy.. I swear.. Here is what I have this morning..
TestShoppingCart.java
import java.util.*;
public class TestShoppingCart {
public static void main(String [] args){
//Create an array for 2 shopping carts (might need some guidance on how this will work)
ShoppingCart[]myCarts = new ShoppingCart[2];
for(int i = 0; i < myCarts.length; i++){
myCarts[i] = new ShoppingCart(size);
}
int choice;
String itemList = "Please select one of our items:\n1)Bananas\n2)Apples\n3)Oranges]\n4)Grapes\n5)Cherries";
String enterQuantity = "Please enter a quantity";
storeItems[0] = new LineItem("Bananas" ,2.50, 0);
storeItems[1] = new LineItem("Apples" ,1.50, 0);
storeItems[2] = new LineItem("Oranges" ,3.50, 0);
storeItems[3] = new LineItem("Grapes" ,0.50, 0);
storeItems[4] = new LineItem("Cherries" ,0.10, 0);
//Prompt the user to select/Initialize a shopping cart
Scanner selectCart = new Scanner(System.in);
System.out.println("Please select your shopping cart: \n1)Shopping Cart 1.\n2)Shopping Cart 2");
int cartInput = selectCart.nextInt();
//ensure the user input is either 0 or 1
int userCart = ((cartInput + 1)%myCarts.length);
//The next section asks the user to select a cart, items and quantities and displays the content
do {
//read the next input from the user
Scanner input = new Scanner(System.in);
System.out.println("Please select and option:\n1)Change your shopping cart.\n2)To add an item to your shopping cart.\n3)To view your cart and total.\n4)Quit");
choice = input.nextInt();
if(choice == 1){
//Prompt the user to select a shopping cart
selectCart = new Scanner(System.in);
System.out.println("Please select your shopping cart: \n1)Shopping Cart 1.\n2)Shopping Cart 2");
cartInput = selectCart.nextInt();
//ensure the user input is either 0 or 1
userCart = ((cartInput + 1)%mycarts.length);
}
else if(choice == 2){
//Prompt the user to select an item
System.out.println(itemList);
Scanner selectItem = new Scanner(System.in);
int addedItem = selectItem.nextInt();
//Prompt user to enter a quantity
System.out.println(enterQuantity);
Scanner selectQuantity = new Scanner(System.in);
int addedQuantity = selectQuantity.nextInt();
//Add the item and quantity to the cart
addedItem -=1;
//Set the quantity of the selected item to the cart
storeItems[addedItem].setQuantity(addedQuantity);
//Add the item to the cart
myCarts[userCart].addItem(storeItems[addItem]);
}
else if (choice == 3){
//Display Cart contents
System.out.println(ShoppingCart.getDisplayData());
//Display Cart total
System.out.println(ShoppingCart.getTotalCost());
}
else{
continue;
}
}while(choice != 4);
}
}
ShoppingCart.java
import java.text.DecimalFormat;
import java.util.*;
public class ShoppingCart {
private double totalCost;
private LineItem[] myItems;
private int max = 100;
int numberOfItems;
//Provide a constructor for this class
public ShoppingCart(int size){
max = size;
//Set every new cart to "empty"
this.totalCost = 0;
//Intialize the array
myItems = new LineItem[max];
numberOfItems = 0;
}
//add a method to add a line item object to this class
public void addItem(LineItem newItemObject){
if(numberOfItems < max - 1){ //max - 1 due to the actual index
myItems[numberOfItems++] = newItemObject;
//update the totalcost
totalCost += newItemObject.costPerItem * newItemObject.itemQuantity;
}
}
//a method printing out the carts contents with the total cost of all the items/quantities
public String getDisplayData(){
DecimalFormat df = new DecimalFormat("#.##");
String output = "";
output += "Your shopping cart contains: \n";
output += "\n\t----------------------------------------------";
//Create a loop to display the contents and quantity of the selected cart
for(int x = 0; x < numberOfItems; x++){
//Append the string with a new lineItem
output += "\n\tItem : " + myItems[x].itemQuantity + " x " +
myItems[x].productName + " @ " +
myItems[x].costPerItem + " = " +
myItems[x].costPerItem * myItems[x].itemQuantity))
;
}
output += "\n\tTotal : " + myCarts.totalCost;
output += "\n\t------------------------------------------";
return output;
}
public double getTotalCost(){
return totalCost;
}
}
LineItem.java
import java.util.*;
class LineItem {
String productName;
double costPerItem;
int itemQuantity;
public LineItem(String productName, double costPerItem, int itemQuantity){
this.costPerItem = costPerItem;
this.productName = productName;
this.itemQuantity = itemQuantity;
}
public double getCostPerItem(){
return costPerItem;
}
public String getProductName(){
return productName;
}
public int getItemQuantity(){
return itemQuantity;
}
public void setQuantity(int qty){
itemQuantity = qty;
}
}