2011-03-27 96 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