How to write GetItemIndex method

144 Views Asked by At

I am creating a ShoppingCart class that represents a shopping cart. I am good with the basics of the class and the getTotalPrice method, but I cannot figure out how to do the getItemIndex problem... "Complete the getItemIndex method as follow: if the itemList has an item with the name passed into the parameter, return the index of that item in the array. Otherwise return -1. "

I know i have to call the Items class, but I do not understand how I can get the name from the item class and return the index.

I have created the Items class and the instance variables and constructor of the ShoppingCart class. I have looked at other shopping Cart methods, but I could not find one that does the getItemIndex

i Tried the code included in the bottom called getItemIndex... I included the getTotalPrice in case it is needed as a reference.

 public class ShoppingCart{


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(){
    if(itemList(itemList.getName))
        return Items[itemList.getName];
    else 
        return -1;
} 

}

Here is the 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);
}

}

I expect a method that is an if statement, but I am not sure how to get the ItemIndex...I am not sure if this requires a for loop either. In another class, I will call this method to use it to simulate a shopping experience.

1

There are 1 best solutions below

1
MrHolal On BEST ANSWER

This should works. You specify nameOfItem you are trying to find. Then iterate through all items in array, if its in array, returns index.

int getItemIndex(String nameOfItem){
   for(int i = 0; i < itemList.length; i++){
      if(itemList[i].getName().equals(nameOfItem){
         return i;
      }
   }
   return -1;
}