Search code examples
javainheritanceconstructorsuperclass

3 cheque accounts 2 constructors for cust without overdraft but cust who does not want overdraft is getting one


ChequeAccount class extends Account class. Account class holds ID, name and balance attributes. ChequesAccount class has overdraftLimit, amtOverdrawn and transactionNo plus super(ID, name, balance) from Account. I have created 3 chqAccount objects in an array and printed their details. Ringo has opted out of overdraft facility in his chq account so separate constructor for him. However when i print the details for all cheque accounts he is the only one who gets the overdraft facility. Its doing my head in, please help

public class TestAccounts6
{
   private static ChequeAccount[] chqAccount = new ChequeAccount[5];
   private static int indexNo = 0;

   public static void main(String[] args)
   {
      ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 0, 0, 0);
      ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);
      ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 0, 0, 0);
      chqAccount[0] = c1;
      chqAccount[1] = c2;
      chqAccount[2] = c3;
      indexNo = 3;
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "ID", "Name", "Balance",
                        "Overdraft", "Amount", "No of");
      System.out.printf("%-10s%-10s%-10s%-10s%-10s%-10s%n", "", "", "",
                        "Limit", "Overdrawn", "Transactions\n");
      for (int i = 0; i < indexNo; i++)
      {
         chqAccount[i].print();
      }

   }

}
public class ChequeAccount extends Account
{
   protected double overdraftLimit = 10000;
   protected double amtOverdrawn = 0;
   protected int transactionNo = 0;

   // constructor
   public ChequeAccount(String ID, String name, double balance,
                        double overdraftLimit, double amtOverdrawn,
                        int transactionNo)
   {
      super(ID, name, balance);
      this.overdraftLimit = overdraftLimit;
      this.amtOverdrawn = amtOverdrawn;
      this.transactionNo = transactionNo;
   }

   public ChequeAccount(String ID, String name, double balance)
   {
      super(ID, name, balance);
   }

   public void print()
   {

      System.out.printf("%-10s%-10s$%-9.2f$%-9.2f$%-9.2f%-10d%n", ID, name,
                        balance, overdraftLimit, amtOverdrawn, transactionNo);
   }

}
public class Account
{
   protected String ID;
   protected String name;
   protected double balance;

   // Constructor
   public Account(String ID, String name, double balance)
   {
      this.ID = ID;
      this.name = name;
      this.balance = balance;
   }

Expected ringo would not get overdraft facility and John and Paul would. Opposite to expectations


Solution

  • it is behaving correctly. Because

    1. you have created 3 chqAccount objects

    ChequeAccount c1 = new ChequeAccount("S1111", "Paul", 1245.00, 0, 0, 0);

    ChequeAccount c2 = new ChequeAccount("S2222", "Ringo", 2500.00);

    ChequeAccount c3 = new ChequeAccount("S3333", "John", 1575.00, 0, 0, 0);

    1. when you try to print the values of this objects. it is printing the following way.

    ID Name Balance Overdraft Amount No of
    Limit Overdrawn Transactions

    S1111 Paul $1245.00 $0.00 $0.00 0 S2222
    Ringo $2500.00 $10000.00 $0.00 0 S3333 John
    $1575.00 $0.00 $0.00 0

    when we observe the above and check the Ringo's details. it has Balance as : $10000.00 because you have not assigned any values to Ringo . But in ChequeAccount as following.

      protected double overdraftLimit = 10000;
      protected double amtOverdrawn = 0;
      protected int transactionNo = 0;
    

    it has default value . so that while printing it is taking default values.