Problem with inheritance??
Codes:
Code :
public class Customers
{
int accID;
String name;
String address;
String dob;
String phone;
double accBalance;
public Customers(int accID, String name, String address, String dob, String phone, double accBalance)
{
this.accID=accID;
this.name=name;
this.address=address;
this.dob=dob;
this.phone=phone;
this.accBalance=accBalance;
}
public Customers()
{
}
public void set_accID(int new_accID)
{
accID=new_accID;
}
public int get_accID()
{
return accID;
}
public void set_name(String new_name)
{
name=new_name;
}
public String get_name()
{
return name;
}
public void set_addr(String new_addr)
{
address=new_addr;
}
public String get_addr()
{
return address;
}
public void set_dob(String new_dob)
{
name=new_dob;
}
public String get_dob()
{
return dob;
}
public void set_phone(String new_phone)
{
phone=new_phone;
}
public String get_phone()
{
return phone;
}
public void set_accBal(double new_bal)
{
accBalance=new_bal;
}
public double get_accBal()
{
return accBalance;
}
public void print()
{
System.out.println("Account no: " + accID);
System.out.println("Customer Name: "+name);
System.out.println("Address: "+address);
System.out.println("Date of Birth: "+dob);
System.out.println("Phone: " +phone);
System.out.print("Avaliable Balance: ");
System.out.printf("%1$.2f", accBalance);
System.out.println("");
}
}
Code :
public class Account1 extends Customers //Saving account
{
private String accType;
private double rate = 0.0;
public Account1(int accID, String name, String address, String dob, String phone, double accBalance,String accType)
{
super(accID,name,address,dob, phone, accBalance);
this.accType=accType;
}
public void set_accType(String new_type)
{
accType=new_type;
}
public String get_accType()
{
return accType;
}
public void setRate(double new_rate)
{
rate=new_rate;
}
public double get_rate()
{
return rate;
}
public void print()
{
super.print();
System.out.println("Account Type: "+accType);
System.out.println("Interest Rate: "+rate);
}
}
Code :
Customers[] custTxt = new Customers[30];
custTxt[index]=new Account1(accID,name.toUpperCase(),address,dob,phone,accBal,accType);
when i do a custTxt[i].get_accType();
it show an error;
Code :
java:212: cannot find symbol
symbol : method get_accType()
location: class Customers
smallBuff.write("Account type: "+custTxt[i].get_accType());
need help on how to show this error...
Re: Problem with inheritance??
Inheritance works down, not up.
get_accType() is a member of Account1, therefore cannot be access from a Customer because not all customers necessarily have a method called get_accType(). There are two options:
1. Cast the object to Account1. You should only use this if you know you're going to have an Account1 object.
2. Move get_accType() into the Customers class (either as a fully defined method, or as an abstract method).
Re: Problem with inheritance??
sorry to post this question here...
Codes for BankDB
Code :
public Customers[] readFile(String filename)throws FileNotFoundException
{
scStream = new Scanner(new File(filename));
int record =0;
boolean flag=true;
String bData="";
String name="",accID1="",accBal1="",phone="",address="",dob="",accType="",rate1="";
double accBal=0.0, rate=0.0;
int accID=0;
try
{
if (scStream == null )
{
System.out.println("NOT FILE OPENED");
return null;
}
else
{
while (scStream.hasNext())
{
bData=scStream.nextLine();
if(bData.startsWith("Account Id"))
{
accID1=bData.substring((2+ (bData.indexOf("= "))));
accID=Integer.parseInt(accID1);
flag = checkAcc(accID);
if (flag==false)
{
builder.append("Acc ID:"+accID+" has already being created\n");
}
}
else if(bData.startsWith("Name"))
{
name=bData.substring((2+ bData.indexOf("= ")));
if (name.length()>20)
{
flag =false;
builder.append("The name: "+name+" exceed the 20 character words limit\n");
}
}
else if(bData.startsWith("Address"))
{
address=bData.substring((2+ bData.indexOf("= ")));
if (address.length()>80)
{
flag =false;
builder.append("the address: "+address+" exceeed the 80 characters limit\n");
}
}
else if(bData.startsWith("DOB"))
{
dob=bData.substring((2+ bData.indexOf("= ")));
if (dob.length()>10)
{
flag =false;
builder.append("the DOB: "+dob+"exceed the 10 characters limit\n");
}
}
else if(bData.startsWith("Phone Number"))
{
phone=bData.substring((2+ bData.indexOf("= ")));
if (phone.length()>8)
{
flag =false;
builder.append("the phone: "+phone+"exceed the 8 characters limit\n");
}
}
else if(bData.startsWith("Account Balance"))
{
accBal1=bData.substring((2+ bData.indexOf("= ")));
accBal=Double.parseDouble(accBal1);
}
else if(bData.startsWith("Account Type"))
{
accType=bData.substring((2+bData.indexOf("= ")));
if (accType.equalsIgnoreCase("Saving"))
{
if (flag==true)
{
custTemp[index]=new Account1(accID,name.toUpperCase(),address,dob,phone,accBal,accType);
index++;
record++;
}
else
{
System.out.println(builder.toString());
}
}
else if (accType.equalsIgnoreCase("checking"))
{
if (flag==true)
{
custTemp [index]= new Account2(accID, name.toUpperCase(),address, dob,phone, accBal,accType);
index++;
record++;
}
else
{
System.out.println(builder.toString());
}
}
}
else if (bData.startsWith("Fixed Daily Interest"))
{
rate1=bData.substring((2+bData.indexOf("= ")));
rate =Double.parseDouble(rate1);
if (flag==true)
{
custTemp [index]= new Account3(accID, name.toUpperCase(),address, dob,phone, accBal,accType,rate);
index++;
record++;
}
else
{
System.out.println(builder.toString());
}
}
}
}
}
catch (Exception e)
{
System.out.println("Error reading file...." +e.getMessage());
}
System.out.println(record + " records read");
return custTemp;
}
Main class codes frag
Code :
public static void main(String[] args)
{
double new_interest=0.0;
BankDB custData = new BankDB();
Scanner input = new Scanner (System.in);
int accID=0;
int index=0;
String fileName="";
char userInput='0';
while (true)
{
System.out.println("First Bank Customer Query System..");
System.out.println("==================================");
System.out.println("(1) Input Data from text file");
System.out.println("(2) Print out Customer Info onto console");
System.out.println("(3) Write Customer Info to a text file");
System.out.println("(4) Delete data");
System.out.println("(5) Update account");
System.out.println("(Q) Quit the system");
System.out.println("Your choice:");
userInput=input.next().charAt(0);
try
{
switch(userInput)
{
case '1':
String[] tempNames=new String [30];
System.out.println("Please enter filename:");
fileName= input.next();
custTemp=custData.readFile(fileName);
for(int i=0; i<custTemp.length;i++)
{
tempNames[i]=custTemp[i].get_name();
}
Arrays.sort(tempNames);
custTxt.clear();
for(int j=0;j<tempNames.length;j++)
{
for (int i=0; i<custTemp.length;i++)
{
if(tempNames[j].equalsIgnoreCase(custTemp[i].get_name()))
{
custTxt.add(j,custTemp[i]);
}
}
}
break;
what these codes is supposed to do is get some data from a text file and put them into an user define type array, sort them into alphabetical order and lastly insert them into an ArrayList...
my logic error faced is that when i do
Code :
System.out.println(custTxt.get(1).print());
the contain is empty.. but if i do this instead:
Code :
System.out.println(custTemp[1].print());
there are values printed out.. need help on how to solve this...
thx