需要添加对象中的数字

时间:2014-01-30 02:46:52

标签: java object

我正在编写一个程序,从用户输入名称,日期和数字的次数,然后将输入的数字相加。如果你看第三个for循环,你会看到我想要添加的位置。我试过做total = cust [i] .amount + total;但由于某种原因我无法访问金额。

这是一个作业。(我知道不应该在这里发布作业问题,但我很难过。)客户唯一的数据成员是名字

public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        final int MAX = 9999;
        Customer [] cust = new Customer[MAX];
        int choice = 0;
        int cnt;

        double total = 0;

        for(cnt=0; cnt < MAX && (choice == 1 || choice ==2 || choice == 0); cnt++){
            System.out.println("For a Service customer type 1, for a Purchaser type 2, to terminate the program press 9");
            choice = s.nextInt();
            switch (choice){
            case 1:
                cust [cnt] = new Service();
                break;
            case 2:
                cust [cnt] = new Purchaser();
                break;
            default:
                break;
            }
        }
        for(int i=0; i < cnt; i++){
            if(cust[i]!= null)
            cust[i].showData();
        }
        for(int i=0; i < cnt; i++ ){
            total = cust[i].amount + total;
        }
            s.close();

    }
}
interface Functions {
    public void getData();
    public void showData();
}
abstract class Customer implements Functions {
    protected String name;


}
class Purchaser extends Customer {
    protected double payment;

    public Purchaser(){
        getData();
    }

    public void getData() {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter payment amount: ");
        payment = s.nextDouble();
    }
    public void showData() {    
        System.out.printf("Customer name: %s Payment amount is: %.2f\n",name,payment);

    }   
}
class Service extends Customer {
    protected String date;
    protected double amount;
    public Service () {
        getData();

    }

    public void getData() {     
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter date of Service: ");
        date = s.nextLine();
        System.out.println("Enter the cost of Service: ");
        amount = s.nextDouble();
    }
    public void showData() {
        System.out.printf("Customer name: %s The date is: %s, the Amount owed is: %.2f\n",name, date, amount);
    }

3 个答案:

答案 0 :(得分:4)

Customer类中没有字段金额

答案 1 :(得分:2)

有几个问题,但你应该

// Use one loop...
for(int i=0; i < cnt; i++){
  if(cust[i]!= null) { // <-- check for null.
    cust[i].showData();
    /* or
    if (cust instanceof Service) {
      total += ((Service)cust[i]).amount; // <-- assuming a public amount 
                                   // field in Service. This is not a good
                                   // practice, but your question is difficult to 
                                   // answer.
    }
    */
    total += cust[i].getAmount(); // <-- a method.
  }
}

如果您想获取金额(或者,为了使此代码有效),请在界面中添加一个getAmount - 更改访问修饰符并将public amount字段添加到{{1}并删除子类中的阴影字段。)

或者,您可以了解集合(我强烈建议您继续使用) - 并且实际使用正确的存储方法 - 也许ArrayList

答案 2 :(得分:1)

这是我可以提出的一个解决方案,我们在界面中添加getAmount方法:

import java.util.Scanner;


public class home {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        final int MAX = 2;
        Customer [] cust = new Customer[MAX];
        int choice = 0;
        int cnt;

        double total = 0;

        for(cnt=0; cnt < MAX && (choice == 1 || choice ==2 || choice == 0); cnt++){
            System.out.println("For a Service customer type 1, for a Purchaser type 2, to terminate the program press 9");
            choice = s.nextInt();
            switch (choice){
            case 1:
                cust [cnt] = new Service();
                break;
            case 2:
                cust [cnt] = new Purchaser();
                break;
            default:
                break;
            }
        }
        for(int i=0; i < cnt; i++){
           if(cust[i]!= null)
           cust[i].showData();
           total = cust[i].getAmount() + total;
        }
            s.close();

    }
}

abstract class Customer implements Functions {
    protected String name;


}

import java.util.Scanner;


class Service extends Customer {
    protected String date;
    protected double amount;
    public Service () {
        getData();

    }

    public void getData() {     
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter date of Service: ");
        date = s.nextLine();
        System.out.println("Enter the cost of Service: ");
        amount = s.nextDouble();
    }
    public double getAmount(){
        return this.amount; 
    }
    public void showData() {
        System.out.printf("Customer name: %s The date is: %s, the Amount owed is: %.2f\n",name, date, amount);
    }

}


    import java.util.Scanner;

class Purchaser extends Customer {
    protected double payment;

    public Purchaser(){
        getData();
    }

    public double getAmount(){
        return this.payment; 
    }
    public void getData() {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the name of the customer");
        name = s.nextLine();
        System.out.println("Enter payment amount: ");
        payment = s.nextDouble();
    }
    public void showData() {    
        System.out.printf("Customer name: %s Payment amount is: %.2f\n",name,payment);

    }   
}



interface Functions {
    public void getData();
    public void showData();
    public double getAmount();
}