我有基类,客户类和子类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);
}
}
也许总结一下这段代码,因为我怀疑它的所有内容都与你的错误直接相关。 – julkiewicz 2011-03-27 12:45:44
编辑问题 – dawnoflife 2011-03-27 12:49:37
并通过总结我的意思是省略与上述问题无关的部分。很可能任何人都不会阅读300多行代码来回答你的问题。 – julkiewicz 2011-03-27 12:59:35