2011-03-27 98 views
0

我有基類,客戶類和子類ACC1截至3繼承和多態的java

當我讀文件初始化我的客戶對象的數組,他們都只是顯示爲空。當我發現客戶有什麼賬戶類型時,我將變量分配給相應的對象(temp1-3,first1-3)。不太確定他們爲什麼仍然空着。

我粘貼我的基類和派生類之一的類定義。當我將第一個和第一個臨時(客戶類objects_分配給第一個(1/2/3)和臨時(1/2/3)(子類,Account1,Account2和Account3)時,readFile()方法中發生錯誤

不知道,如果類定義是錯誤的,因爲我只需調用超級構造函數中所有的人。試圖調試它,但沒有成功。幫助將不勝感激。

package lab4; 

import java.io.*; 
import java.util.*; 


public class lab4{ 
    static int count =0; // number of records read or written 
    public static void main(String args[]) 
     throws IOException 
    { 

     Customer[] records = new Customer[30]; 
     for (int j=0; j<30; j++){ 
      records[j] = new Customer(); 
     } 

     menu(records); 
    } 



    public static int readFile(String filename, Customer[] review) 
     throws IOException 
    { 

     Scanner scan = new Scanner (new File (filename)); 

     /*Reading the first record separatly*/ 
     Customer first = new Customer(); 
     Account1 first1= new Account1(); 
     Account2 first2= new Account2(); 
     Account3 first3 = new Account3(); 

     String[] a = scan.nextLine().split("="); 
     first.set_account_id(Integer.parseInt(a[1].trim())); 

     a = scan.nextLine().split("="); 
     first.set_name(a[1].toUpperCase().trim()); 

     a = scan.nextLine().split("="); 
     first.set_address(a[1].trim()); 

     a = scan.nextLine().split("="); 
     first.set_phone_number(a[1].trim()); 

     a = scan.nextLine().split("="); 
     first.set_date_of_birth(a[1].trim()); 

     a = scan.nextLine().split("="); 
     first.set_balance(Double.parseDouble(a[1].trim())); 

     a= scan.nextLine().split("="); 
     first.set_accType(a[1].trim()); 

     if (first.get_accType().equals("Saving")){ 
      first = first1; 
     } 

     else if(first.get_accType().equals("Checking")){ 

      first = first2; 
     } 

     else if(first.get_accType().equals("Fixed")){ 

      first = first3; 
      a = scan.nextLine().split("="); 
      first3.set_intRate(Double.parseDouble(a[1].trim())); 
     } 

     System.out.println(first.get_name()); 

     scan.nextLine();// resets the buffer reader 
     review[0]= first; 
     count = count+1; 

     while (scan.hasNext()&& count>0){ 
      Customer temp = new Customer(); 
      Account1 temp1 = new Account1(); 
      Account2 temp2 = new Account2(); 
      Account3 temp3 = new Account3(); 

      String[] st = scan.nextLine().split("="); 

      for(int i=0;i<count;i++){ 
       if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records 
        System.out.println("This account id is already in use so the record won't be read"); 
        for (int k=0; k<7; k++) 
         scan.nextLine(); 
       } 
       else 
        break; 
      } 

      temp.set_account_id(Integer.parseInt(st[1].trim())); 
      st = scan.nextLine().split("="); 
      temp.set_name(st[1].toUpperCase().trim()); 
      st = scan.nextLine().split("="); 
      temp.set_address(st[1].trim()); 
      st = scan.nextLine().split("="); 
      temp.set_phone_number(st[1].trim()); 
      st = scan.nextLine().split("="); 
      temp.set_date_of_birth(st[1].trim()); 
      st = scan.nextLine().split("="); 
      temp.set_balance(Double.parseDouble(st[1].trim())); 
      st= scan.nextLine().split("="); 
      temp.set_accType(st[1].trim()); 

      if (temp.get_accType().equals("Saving")){ 
       temp = temp1; 
      } 

      else if(temp.get_accType().equals("Checking")){ 

       temp = temp2; 
      } 


      else if(temp.get_accType().equals("Fixed")){ 
       temp = temp3; 
       st = scan.nextLine().split("="); 
       temp3.set_intRate(Double.parseDouble(a[1].trim())); 
      } 

      if (scan.hasNextLine()){ 
       scan.nextLine(); 
      } 

      int j; 
      for(j=0;j<count;j++){ 

       if (temp.get_name().compareTo(review[j].get_name())<0){ // Putting records in ascending order 
        break; 
       } 
      } 

      count=count+1; 
      for (int k=count;k>j;k--){ 
       review[k]=review[k-1]; 
      } 

      review[j]= temp; 

      if (count>=30){ 
       System.out.println("The number of records read has exceeded the limit and it will stop reading now"); 
       break; 
      } 

     } 

     return count; 
    } 
} 

package lab4; 

import java.io.*; 
import java.util.*; 
/** 
* 
* @author dawnoflife 
*/ 
public class Account1 extends Customer { 

    public Account1(){ // Savings Account 
     super(); 
    } 

    public void update(double rate){ // Savings account interest calc 
     double updateBal = (this.get_balance()*(Math.pow((1+rate),31))); // Interest calculated for month of march 
     this.set_balance(updateBal); 
    } 


} 
+0

也許總結一下這段代碼,因爲我懷疑它的所有內容都與你的錯誤直接相關。 – julkiewicz 2011-03-27 12:45:44

+0

編輯問題 – dawnoflife 2011-03-27 12:49:37

+0

並通過總結我的意思是省略與上述問題無關的部分。很可能任何人都不會閱讀300多行代碼來回答你的問題。 – julkiewicz 2011-03-27 12:59:35

回答

3

因此,您正在讀取變量first中的Customer對象的信息,然後將其與first = first1一起扔掉。 (或first2first3)。 (或temp2temp3)。

我想你誤會了=操作符的含義。它不會將現有的first對象的類更改爲first1的類,但會將該變量中的指針從現有對象切換到另一個對象。

前:

   .------------. 
first -----> | Customer | 
      '------------' 

      .------------. 
first1 ----> | Account1 | 
      |   | 
      '------------' 

後:

   .------------. 
first  | Customer | 
    \  '------------' 
    \ 
     \  .------------. 
     '---> | Account1 | 
first1 ----> |   | 
      '------------' 

Customer對象中的所有信息,現在是離開。 (這同樣適用於其他的帳戶類型,併爲temp更高版本。)

它看起來像你必須做的其中之一:

  • 決定哪些賬戶類型使用時,您可以複製將數據發送到您的帳戶。

    你可以使用這個複製構造函數。

  • 更改文件格式以首先包含帳戶類型,並在開始時創建正確類型的對象。


順便說一句,想想設計 - 這是爲什麼Account1Customer一個子類?客戶非帳號,他有一個

因此,最好在此使用委派,並考慮哪些部分信息是帳戶的一部分,哪些是客戶的一部分。然後,一個客戶甚至可以擁有多個賬戶(甚至不同類型)。

+0

該問題說,使用Account1作爲子類,不幸的是我不能編輯輸入文件。我想在這裏實現正確的繼承/多態性功能,所以我可以得到功勞的功勞。你認爲我可以解決使用複製構造函數的情況嗎? – dawnoflife 2011-03-27 14:26:23

+0

是的,您可以 - 僅在您閱讀信息後才創建對象。你瞭解你的問題的原因了嗎? – 2011-03-27 14:55:19

+0

是的,我得到了什麼問題。感謝您的幫助 – dawnoflife 2011-03-27 15:31:37

0

你的代碼非常難讀。 您遇到的問題之一是因爲您丟失了使用對象方法設置的信息,因此當您重新爲該對象重新分配不同的值時

請參閱我對inheritance and polymorphism java的回答,我相信這些回答將幫助您解決此問題。

你要做的是建立類型爲Account1,Account2,Account3的客戶對象,根據你掃描文件時分析的某些值。 首先要做的是構造正確類型的對象。 然後在特定於該類型的方法中設置值。 然後在超類Customer中的方法中設置值。

+0

問題是一個小時前,然後前兩個答案在3秒內出現... – 2011-03-27 14:07:08