首页 > 解决方案 > 如何修复类的 addToCart 方法?

问题描述

我必须为 addToCart 方法创建代码以将项目添加到 itemList。我在这里查看了其他 ShoppingCart 问题,但我没有看到增加商品数量并检查商品名称以查看它是否在数组中的问题...问题是

1) 如果在 itemList 中已经存在具有传入参数名称的项目,则通过将其增加传入的数量来更新该项目数量。

2) 如果 itemList 中不存在商品名称并且有空间容纳新商品,则使用作为参数传入的给定名称、数量和价格创建一个新商品对象,将 numItems 更新为 1。

3) 两种情况都应该返回 true。

4) 如果具有给定名称的项目不在数组中并且数组没有容量,则该方法应返回 false...

5)完成dispay方法打印出itemList中每件商品的名称、总价、数量

我做了一个 getItemIndex 方法,但不确定是否需要在这个方法中使用它。当我查看其他 addtoCart 方法时,我认为这个是不同的。

我尝试了一个 for 循环,但它没有返回我想要的,所以我创建了新对象,但我无法让对象保留在对象数组中。

物品类

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);
    }
}

购物车类

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();

    }
}

结果应该将项目放入数组并保留在其中,因此当我在我的 main 方法中调用它时,它将保留在数组中,并且当我调用它时它应该像这样打印

牛奶 $3.00 1 $3.00
鸡蛋 $3.00 1 $3.00

等等......这将通过我的模拟方法打印,但我无法弄清楚如何让物品留在购物车中。

标签: javaarraysobjectaggregator

解决方案


这里有几个问题。首先,您在itemList[numItems]检查传递给的项目名称是否addToCart已存在于项目列表中之前进行设置。其次,您不检查整个列表,您只检查 index 处的元素,numItems在这种情况下,它将始终评估为 true,因为您itemList[numItems]在检查名称是否已存在之前设置为新项目。为了解决这个问题,我建议运行一个 for 循环,从 i = 0 到 numItems,并检查是否存在新项目的名称。一旦您确认它不存在,然后添加新项目并递增numItems. 但是,如果该项目已经存在,则更新数量并返回。最后,检查项目列表是否已满应在您迭代整个列表以查看项目是否已存在之后进行。这样做的原因是因为即使列表已满,您也需要更新现有的项目数量。因此,在尝试添加新项目之前,您应该检查列表中是否有足够的空间。

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;
}

推荐阅读