有对象的麻烦调用方法 - JAVA

时间:2016-04-09 19:39:07

标签: java object methods constructor

到目前为止,我已经设法使用名为ExpenseItem的类建立费用。此类使用属性名称,数量和频率对费用进行建模。应将属性建模为该类的私有成员。因此我使用了一个带有这三个参数的构造函数。

我在课程中实现以下方法时遇到了麻烦:

annualTotal ;应该返回ExpenseItem对象的估计总费用。它需要是数量*频率。

我很难实现这个方法,所以我可以让它从ExpenseItem中获取所有金额和频率并总计。到目前为止我只有3:

ExpenseItem e1 = new ExpenseItem ("Coffee", 2.25 , 6);
ExpenseItem e2 = new ExpenseItem ("Food", 5.30 , 5);
ExpenseItem e3 = new ExpenseItem ("Gas", 20.00 , 2);

如何实现一个方法,该方法从e1,e2,e3,e(n)中取出双倍数...并将每个的费用相乘?例如,e1 annualTotal = 2.25 * 6.

以下当前代码:

public class ExpenseItem {

private String name;
public double amount;
public double frequency;
public double total;
public ExpenseItem(String name, double amount, double frequency) {
    this.name = name;
    setAmount(amount);
    setFrequency(frequency);

}
public double annualTotal(double amount, double frequency){
    double total= amount+frequency;
    return total;
    //System.out.printf("the total is:",total );
}

public void setName(String name){
    this.name = name;
}

public String getName(){
    return name;
}
public void setAmount(double amount){
    this.amount = amount>=0 ? amount:0 ;
}
public double getAmount(){
    return amount;
}
public void setFrequency(double frequency){
        this.frequency = frequency>=0 ? frequency:0 ;
}
public double getFrequency(){
        return frequency;
}

public static void main(String[] args) {
ExpenseItem e1 = new ExpenseItem ("Coffee", 2.25 , 6);
ExpenseItem e2 = new ExpenseItem ("Food", 5.30 , 5);
ExpenseItem e3 = new ExpenseItem ("Gas", 20.00 , 2);

System.out.println("Expense Item     Amount   Frequency\n");
  outputInfo(e1);outputInfo(e2);outputInfo(e3);  
 }

private static void outputInfo(ExpenseItem e){
System.out.printf("%-15s\t%5.2f\t%5.0f\n",
 e.getName(),e.getAmount(), e.getFrequency() );
    }

  }

3 个答案:

答案 0 :(得分:0)

ExpenseItem具有实例中每个计算所需的元素,因此不是定义此方法

public double annualTotal(double amount, double frequency){
    double total= amount+frequency;
    return total;
    //System.out.printf("the total is:",total );
}

这样做

public double annualTotal(){

    return (double)this.amount * this.frequency;

}

除此之外,我将定义一个静态累加器成员(因为该值是针对类而不是对象)

public class ExpenseItem {

private String name;
public double amount;
public double frequency;
public double total;

public static double totalAcumulator = 0;  //here is the trick
public ExpenseItem(String name, double amount, double frequency) {
    this.name = name;
    setAmount(amount);
    setFrequency(frequency);
    totalAcumulator += annualTotal(); // and you call it every time you construct an object.
}

你可以通过调用

来获得它的价值
ExpenseItem.totalAcumulator

答案 1 :(得分:0)

您正在使用加法+运算符而不是乘法*运算符

你的功能

public double annualTotal(double amount, double frequency){
double total= amount+frequency;
return total;
//System.out.printf("the total is:",total );

}

应该如下

public double annualTotal(double amount, double frequency){
double total= amount * frequency;
return total;
//System.out.printf("the total is:",total );

}

这有帮助吗?

答案 2 :(得分:0)

对于这样的事情,我觉得使用不可变对象是有意义的。由于total是派生字段:

public class ExpenseItem {

    private final String name;
    private final double amount;
    private final double frequency;
    private final double total;

    public ExpenseItem(String name, double amount, double frequency) {
        this.name = name;
        this.amount = amount >= 0 ? amount : 0;
        this.frequency = frequency >= 0 ? frequency : 0;
        this.total = amount * frequency;
    }

    public String getName() {
        return name;
    }

    public double getAmount() {
        return amount;
    }

    public double getFrequency() {
        return frequency;
    }

    public double getTotal() {
        return total;
    }

    @Override
    public String toString() {
        return String.format("%-10s%-10.2f%-5.0f%1.2f", name, amount, frequency, total);
    }

    public static void main(String[] args) {
        ExpenseItem e1 = new ExpenseItem ("Coffee", 2.25 , 6);
        ExpenseItem e2 = new ExpenseItem ("Food", 5.30 , 5);
        ExpenseItem e3 = new ExpenseItem ("Gas", 20.00 , 2);

        System.out.println(e1);
        System.out.println(e2);
        System.out.println(e3);

        double total = e1.getTotal() + e2.getTotal() + e3.getTotal();       
        System.out.println(String.format("\n%-25s%1.2f", "Total", total));
    }
}

输出:

Coffee    2.25      6    13.50
Food      5.30      5    26.50
Gas       20.00     2    40.00

Total                    80.00

您还可以在列表中添加ExpenseItem

public static void main(String[] args) {
    List<ExpenseItem> list = Arrays.asList(
            new ExpenseItem ("Coffee", 2.25 , 6),
            new ExpenseItem ("Food", 5.30 , 5),
            new ExpenseItem ("Gas", 20.00 , 2)
            );

    double total = 0;
    for(ExpenseItem e : list) {
        System.out.println(e);
        total += e.getTotal();
    }

    System.out.println(String.format("\n%-25s%1.2f", "Total", total));
}

产生相同的输出。