2014-01-30 19 views
-1

我正在寫一個程序,從用戶的名稱,日期和數字,他們輸入它的次數,然後加起來輸入的數字。如果你看第三個循環,你會看到我想添加的位置。我嘗試過總= 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); 
    } 
+0

可能重複的[爲什麼我得到一個錯誤!?(空指針異常)](http://stackoverflow.com/questions/21446262/why-am-i-getting-an-errornull-pointer-exception) –

+0

這是一個不同的問題...相同的代碼,是的。 – user278153

+0

不相關,但'for'循環中存在一個細微的錯誤。它需要'for(cnt = 0; cnt

回答

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

@DavidWallace好點,我改了它。最近一直和PHP一起工作...... Lol – BlackHatSamurai

+0

還需要在'Service'類中進行更改以使其工作。 –

+0

這是一個任務(我知道不應該在這裏發佈作業問題,但我很難過)。客戶唯一的數據成員是姓名。 – user278153

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

如果您想要獲取金額(或者爲了使此代碼正常工作 - 更改訪問修改器並將名爲amountpublic字段添加到Customer並刪除您的子類中的陰影字段),那麼請在您的接口中添加一個getAmount。

或者,您可以瞭解Collections(我強烈建議您無論如何都要這麼做) - 並且實際使用正確的存儲方法 - 可能是ArrayList

+0

我無法訪問金額... – user278153

+0

試圖公開,這沒有奏效。我會嘗試getter – user278153

+0

@ user278153那麼還有另一個問題。我建議看看你如何聲明金額變量。 – Steve

4

在客戶類中沒有字段金額

+0

+1。這是迄今爲止唯一正確的答案。 –

+1

+1。我只想補充一點,也許OP忘記了'amount'在'Customer'類的'Service'類中。 OP可能想了解繼承。 –

+0

我意識到它在服務類中。無論如何用cust [i]對象訪問它嗎? – user278153

相關問題