2014-01-30 76 views
0

我只需要從Service類調用方法getAmount()。我不想添加買方類的值。有沒有一種方法可以明確地從Service類中調用方法?我已經把**放在我指的地方。需要調用特定類的方法

package prog24178.assignment; 

    import java.util.Scanner; 

    public class Assignment3 { 

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

      double total = 0; 

      //loop to choose customer type and create a new object. 
      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 any number besides 1 or 2"); 
       choice = s.nextInt(); 
       switch (choice){ 
       case 1: 
        cust [cnt] = new Service(); 
        break; 
       case 2: 
        cust [cnt] = new Purchaser(); 
        break; 
       default: 
        break; 
       } 
      } 
      //loop to print the entries 
      for(int i=0; i < cnt; i++){ 
       if(cust[i]!= null) 
       cust[i].showData(); 
      } 
      //loop to print the total for the service objects.**THIS IS LOOP I AM //REFFERING TO 
      for(int i=0; i < cnt; i++){ 
       if(cust[i]!= null) 
       total = cust[i].getAmounts() + total; 
      } 
       System.out.println("Monthly invoice total: " + total); 
       s.close(); 

     } 
    } 
    interface Functions { 
     public void getData(); 
     public void showData(); 
     public double getAmounts(); 
    } 
    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); 
     } 

//**I DO NOT WANT TO CALL THIS METHOD 
     public double getAmounts(){ 
      return this.payment; 
     } 
    } 
    class Service extends Customer { 
     protected String date; 
     public 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); 
     } 
     //**THIS IS THE METHOD I NEED TO CALL 
     public double getAmounts(){ 
      return this.amount; 
     } 
    } 
+0

你真的嘗試過使用它,並且你有錯誤嗎? –

+0

是的,我做了,我沒有得到一個錯誤,但它增加了客戶類的值。我只想添加Service類中的值。 – user278153

+1

那麼......因爲你的Service類擴展了Customer類,所描述的實際上是一個特性,而且正是OOP的一個非常重要的特性。如果你不想這樣做,那麼你根本不應該擴展客戶類 –

回答

1

檢查您的客戶類型:

if (cust[i] instanceof Service) { 
    total = cust[i].getAmounts() + total; 
} 

這需要照顧的空檢查,並自動完成。

+0

我收到一個錯誤,說我無法解析爲變量。 – user278153

+1

它位於for循環中,您可以在其中解析i。 –

+1

他的意思是用他爲你寫的'if'代替'for'循環中的'if'。 – Rainbolt

相關問題