Note that there are some explanatory texts on larger screens.

plurals
  1. POinheritance and polymorphism java
    text
    copied!<p>I have base class, Customer and sub-classes Acc1 up till 3.</p> <p>When I am reading a file to initialize my array of customer objects, they all just show up as empty. As I find out what account type the customer has I assign the variable to the respective object(temp1-3, first1-3). Not quite sure why they remain empty.</p> <p>I am pasting my class definitions for the base and one of the derived classes. The error occurs in the readFile() method when I assign first and temp (Customer class objects_ to first(1/2/3) and temp(1/2/3) (sub classes, Account1, Account2 and Account3).</p> <p>Not sure if the class definitions are wrong as I simply call the super constructor in all of them. tried debugging it but with no success. Help will be appreciated.</p> <pre><code>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&lt;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()&amp;&amp; count&gt;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&lt;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&lt;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&lt;count;j++){ if (temp.get_name().compareTo(review[j].get_name())&lt;0){ // Putting records in ascending order break; } } count=count+1; for (int k=count;k&gt;j;k--){ review[k]=review[k-1]; } review[j]= temp; if (count&gt;=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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload