将数组中对象的值相加

时间:2019-02-09 15:30:47

标签: java arrays object return-value add

我正在尝试将所有成本值和数量值相加,并将它们与方法list[i]中的数组对象getTotalCost相乘(如代码所示):

public class GroceryList {
    private int i;

    GroceryItemOrder list[] = new GroceryItemOrder[10];

    public void addItem(String name, double cost, int quantity) {
        try {
            for (i = 0; i < list.length; i++) {
                list[i] = new GroceryItemOrder(name, cost, quantity);

                if (list[i] == list[9] && list[i] != null) {
                    System.out.format("%6s %-15S %10gKR. %4d stk.%n", "",
                            list[i].getName_Food(),
                            list[i].getCost_Food(),
                            list[i].getQuantity_Food()
                    );
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("You put too many groceries in your cart!");
            System.exit(0);
        }
    }

    public void getTotalCost(){
        System.out.println();

        try {
            double totalAmount;
            totalAmount = list[i].getCost_Food() * list[i].getQuantity_Food();
            System.out.println("The Total Amount is: " + totalAmount);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Something went wrong");
        }
    }
}

this below is my main class:    

package com.company;

public class Main {

public static void main(String[] args) {

    GroceryList gL = new GroceryList();

    receipt();

    gL.addItem("Bananer",10,6);
    gL.addItem("Toiletpapir",40,2);
    gL.addItem("Svinekød",30,1);
    gL.addItem("Papir",25,1);
    gL.addItem("Sødmælk",10,3);
    gL.addItem("Færdigret",15,5);

    //=130
    /*
    gL.addItem("Kylling",10,2);
    gL.addItem("Kaffe",10,2);
    gL.addItem("Bønner",10,2);
    gL.addItem("Kage",10,2);
    //*/

    gL.getTotalCost();


}

public static void receipt(){
    System.out.println("");
    System.out.printf("%9S %1S%n","","\"~discount supermarket receipt~\"");
    System.out.printf("%15S %15S %15S%n","Grocery:","Cost:","Quantity:");
    System.out.println("");
}

}

这是删除try / catch时出现的输出错误:

  

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:10       在com.company.GroceryList.getTotalCost(GroceryList.java:33)       在com.company.Main.main(Main.java:26)

我想得到:

  

总金额为:xxx

1 个答案:

答案 0 :(得分:0)

您必须跟踪添加到阵列中的项目数。
因此,请在类级别而不是i声明此变量:

private int counter = 0;

您的addItem()方法包含一个for循环。为什么?
此方法应在list中添加1个项目,因此请更改为:

public void addItem(String name, double cost, int quantity) {
    if (counter == list.length) {
        System.out.println("Your cart is full! No more items can be added.");
        return;
    }
    list[counter] = new GroceryItemOrder(name, cost, quantity);
    counter++;
}

现在在getTotalCost()中,您确实需要一个for循环来遍历列表中的所有项目并获得所需的总数:

public void getTotalCost(){
    double totalAmount = 0.0;
    System.out.println();
    for (int i = 0; i < counter; i++) {
        totalAmount += list[i].getCost_Food() * list[i].getQuantity_Food();
        System.out.format("%6s %-15S %10gKR. %4d stk.%n", "",
                        list[i].getName_Food(),
                        list[i].getCost_Food(),
                        list[i].getQuantity_Food()
        );
    }
    System.out.println("The Total Amount is: " + totalAmount);
}