I have to create code for an addToCart method to add an item to the itemList. I have looked at the other ShoppingCart problems on here, but I do not see one that adds to the quantity of the item and checks the name of the items to see if it is in the array... The problem is
1) if an item with the name passed in the parameter already exists in the itemList, update that items quantity by increasing it by the quantity passed in.
2) if no item name exists in itemList and there is space for a new item, create a new item object with the given name, quantity, and price passed in as parameter, update numItems by 1.
3) both cases should return true.
4) if the item with the given name was not in the array and the array has no capacity the method should return false...
5) complete the dispay method to print out the name, total price, and quantity of each item in the itemList
I made a getItemIndex method, and am not sure if i need to use that within this method. As i have looked at the other addtoCart methods, I think that this one is different.
I have tried a for loop and it did not return what i wanted, so i have created new objects, but i cannot get the objects to stay contained in the array of objects.
Items class
public class Items{
private String name;
private double price;
private int quantity;
public Items (String n, double p, int q){
name = n;
price = p;
quantity = q;
}
public double getPrice(){
return price;
}
public String getName(){
return name;
}
public int getQuantity(){
return quantity;
}
public void addQuantity(int amt){
int newQuantity = amt + quantity;
quantity = newQuantity;
}
public String toString(){
return "item name: " + name + ", item quantity: " + quantity +
", total price: " + (price * quantity);
}
}
ShoppingCart class
public class ShoppingCart{
//TODO: declare a cart of Items
private Items[] itemList;
//TODO: declare the number of distinct items in the cart
private int numItems = 0;
private static final int INITIAL_CAP = 5; // the initial size of the cart
private static final int GROW_BY=3;
// ---------------------------------------------------------
// Creates an empty shopping cart with a capacity for 5 items.
// ---------------------------------------------------------
public ShoppingCart(){
itemList = new Items[INITIAL_CAP];
numItems = 0;
}
public double getTotalPrice(){
double totalPrice = 0;
numItems = 0;
for(int i = 0; i<itemList.length; i++){
if(itemList[i]!= null){
totalPrice = totalPrice + (itemList[i].getQuantity()*itemList[i].getPrice());
numItems++;
}
}
return totalPrice;
}
private int getItemIndex(String nameOfItem){
for(int i = 0; i < itemList.length; i++){
if(itemList[i].getName().equals(nameOfItem))
return i;
}
return -1;
}
public boolean addToCart(String name, double price, int quantity){
Items n = new Items (name, price, quantity);
if(numItems == INITIAL_CAP)
return false;
itemList[numItems]=n;
if(itemList[numItems].getName().equals(name)){
quantity = quantity + 1;
return true;
}
else if (!itemList[numItems].getName().equals(name)){
numItems = numItems + 1;
return true;
}
return false;
}
public String display(){
for(int i = 0; i<numItems; i++){
System.out.println(itemList[i].toString());
}
return toString();
}
}
The result should put the items into the array and stay contained in there, so when I call it in my main method it will stay in the array and it should print like this when i call it
milk $3.00 1 $3.00
eggs $3.00 1 $3.00
etc... That will be printed b my simulation method, but i cannot figure out how to get the items to stay in the cart.
There are a couple issues here. First, you're setting itemList[numItems]
before you've even checked if the name of the item passed to addToCart
already exists in the items list. Second, you don't check the entire list, you only check the element at index numItems
, which will always evaluate to true in this case since you set itemList[numItems]
to the new item before checking if the name already exists. To resolve this, I would suggest running a for loop, from i = 0 to numItems, and checking for the existence of the name of the new item. Once you've confirmed that it does not exist, then add the new item and increment numItems
. If the item already exists, however, then update the quantity and return. Lastly, the check to see if the item list is full should be done after you've iterated the entire list to see if the item already exists. The reason for this is that because you need to update existing item quantities even if the list is full. So before trying to add a new item, you should check if there is enough space in the list.
public boolean addToCart(String name, double price, int quantity){
Items n = new Items (name, price, quantity);
for(int i = 0; i < numItems; i++){
if(itemList[i].getName().equals(name)) {
itemList[i].quantity += quantity;
return true;
}
}
if(numItems == INITIAL_CAP)
return false;
itemList[numItems] = n;
numItems += 1;
return true;
}