简体   繁体   English

对象的麻烦调用方法-JAVA

[英]Having Trouble Calling Method for Object - JAVA

So far I've managed to model an expense with a class called ExpenseItem. 到目前为止,我已经设法通过一个名为ExpenseItem的类对费用进行建模。 This class models an expense using the attributes name, amount and frequency. 此类使用属性名称,数量和频率对费用进行建模。 The attributes should be modeled as private members of that class. 属性应建模为该类的私有成员。 Thus I have used a constructor that takes these three arguments. 因此,我使用了带有这三个参数的构造函数。

I'm having trouble implementing the following methods into the class: 我无法在类中实现以下方法:

annualTotal ; AnnualTotal ; which is supposed to return the estimated total expense for the ExpenseItem Object. 它应该返回ExpenseItem对象的估算总费用。 it needs to be amount * frequency. 它必须是数量*频率。

Im having difficulty implementing the method so I can get it to grab all amounts and frequencies from ExpenseItem and sum the totals. 我在实施该方法时遇到困难,因此我可以从ExpenseItem获取所有数量和频率并将其总和求和。 So far I only have 3: 到目前为止,我只有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);

How do I implement a method which takes the doubles from e1,e2,e3, e(n)... and multiplies the expense for each? 如何实现从e1,e2,e3,e(n)中获取双精度数并乘以每项成本的方法? for Example, e1 annualTotal = 2.25 * 6. 例如,e1 AnnualTotal = 2.25 * 6。

Current code below: 当前代码如下:

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

  }

The class ExpenseItem has the elements you want for every calculation in the instance so instead of defining this method ExpenseItem类具有实例中每次计算所需的元素,因此无需定义此方法

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

do this 做这个

public double annualTotal(){

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

}

additional to that I would define a static acumulator member (since that vale is for the class and not the objects) 除此之外,我将定义一个静态的累加器成员(因为vale用于类而不是对象)

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

and you can get its value by calling 您可以通过致电获取其价值

ExpenseItem.totalAcumulator

you are using addition + operator instead of multiplication * operator 您正在使用加法+运算符而不是乘法*运算符

your function 你的职能

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

} }

should be like below 应该像下面

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

} }

Does that help? 有帮助吗?

For something like this, I feel it makes sense to use immutable objects. 对于这样的事情,我觉得使用不可变的对象很有意义。 Since total is a derived field: 由于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));
    }
}

Output: 输出:

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

Total                    80.00

You could also have the ExpenseItem s in a list: 您还可以在列表中包含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));
}

Which produces the same output. 产生相同的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM