java - 如何编写一个方法来计算数组中所有项目的总成本?

标签 java arrays methods

我卡住了。这是我到目前为止所写的,但我不知道如何设置方法调用来提示总数。我需要添加数组中所有项目的单独总计以获得总成本,并且需要在程序结束时显示。拜托,任何建议都是有帮助的。我很快就要上类了,去之前需要把它上交。谢谢

主文件

package inventory2;
import java.util.Scanner;

    public class RunApp
{
        public static void main(String[] args)
{

        Scanner input = new Scanner( System.in );

        Items theItem = new Items();

        int number;
        String Name = "";

    System.out.print("How many items are to be put into inventory count?:  ");
    number = input.nextInt();
    input.nextLine();

    Items[]inv = new Items[number];


     for(int count = 0; count < inv.length; ++count)
            {
                    System.out.print("\nWhat is item " +(count +1) + "'s name?:  ");
                            Name = input.nextLine();
                            theItem.setName(Name);

                    System.out.print("Enter " + Name + "'s product number:  ");
                            double pNumber = input.nextDouble();
                            theItem.setpNumber(pNumber);

                    System.out.print("How many " + Name + "s are there in inventory?:  ");
                            double Units = input.nextDouble();
                            theItem.setUnits(Units);

                    System.out.print(Name + "'s cost: ");
                            double Price = input.nextDouble();
                            theItem.setPrice (Price);

                    inv[count] = new Items(Name, Price, Units, pNumber);
                    input.nextLine();

                        System.out.print("\n Product Name:     " + theItem.getName());
                        System.out.print("\n Product Number:     " + theItem.getpNumber());
                        System.out.print("\n Amount of Units in Stock:     " + theItem.getUnits());
                        System.out.print("\n Price per Unit:   " + theItem.getPrice() + "\n\n");
                        System.out.printf("\n Total cost for %s in stock: $%.2f", theItem.getName(), theItem.calculateTotalPrice());
                    System.out.printf("Total Cost for all items entered: $%.2f", theItem.calculateTotalPrice());    //i need to prompt for output to show total price for all items in array
            }
    }
}

二等舱

package inventory2;

    public class Items
{
       private String Name;
       private double pNumber, Units, Price;          

public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}

    //constructor
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
    Name = productName;
    pNumber = productNumber;
    Units = unitsInStock;
    Price = unitPrice;
}
    //setter methods
public void setName(String n)
{
    Name = n;
}

public void setpNumber(double no)
{
    pNumber = no;
}

public void setUnits(double u)
{
    Units = u;
}

public void setPrice(double p)
{
    Price = p;
}

//getter methods
public String getName()
{
return Name;
}

public double getpNumber()
{
return pNumber;
}

public double getUnits()
{
return Units;
}

public double getPrice()
{
return Price;
}

public double calculateTotalPrice()
{
    return (Units * Price);
}

public double calculateAllItemsTotalPrice()             //i need method to calculate total cost for all items in array
{
    return (TotalPrice  );                              
}

最佳答案

在您的 for 循环中,您需要乘以单位 * 价格。这为您提供了该特定项目的总数。同样在 for 循环中,您应该将其添加到跟踪总计的计数器中。您的代码看起来像

float total;
total += theItem.getUnits() * theItem.getPrice();

total 应该有范围,这样它就可以从 main 中访问,除非你想在函数调用之间传递它。然后您可以只打印出总数或创建一个方法来为您打印出来。

关于java - 如何编写一个方法来计算数组中所有项目的总成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201576/

相关文章:

java - Solr - java.net.SocketException : Too many open files

java - 是否可以确定Selenium WebElements的html代码顺序

javascript - 如何使用展开运算符将对象数组插入特定元素

java - Spring security 阻止从后台线程导航到另一个 View

java - 通过蓝牙android接收字符串(没有换行或回车)

php - 在带有类私有(private)函数的 php 中使用 usort

c++ delete[] 二维数组导致堆损坏

android - 获取当前壁纸

php - 使用变量类名访问静态方法 (PHP)

python - 在对象列表 Python 3 中的所有对象上调用方法的惯用方式