Ryan
Ryan

Reputation: 59

Constructors for subclasses

Okay guys, im having trouble with these constructors for this code and some logic. I did most of it, just confused on how to finish this off.

Here is the main code i have. (it might be a little sloppy but its "good enough")

public class Accountdrv {
  public static void main (String[] args) {
    Account account = new Account(1122, 20000, 4.5);

    account.withdraw(2500);
    account.deposit(3000);
    System.out.println("Balance is " + account.getBalance());
    System.out.println("Monthly interest is " +
      account.getMonthlyInterest());
    System.out.println("This account was created at " +
      account.getDateCreated());
  }
}

class Account {
  private int id;
  private double balance;
  private double annualInterestRate;
  private java.util.Date dateCreated;

  public Account() {
    dateCreated = new java.util.Date();
  }

  public Account(int id, double balance, double annualInterestRate) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    dateCreated = new java.util.Date();
  }

  public int getId() {
    return this.id;
  }

  public double getBalance() {
    return balance;
  }

  public double getAnnualInterestRate() {
    return annualInterestRate;
  }

  public void setId(int id) {
    this.id =id;
  }

  public void setBalance(double balance) {
    this.balance = balance;
  }

  public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
  }

  public double getMonthlyInterest() {
    return balance * (annualInterestRate / 1200);
  }

  public java.util.Date getDateCreated() {
    return dateCreated;
  }

  public void withdraw(double amount) {
    balance -= amount;
  }

  public void deposit(double amount) {
    balance += amount;
  }
}

Now, i wanted to create a Savings, and Checking account. I need help with what i need to add for constructors, i put in the comments the parts i know im missing but confused on how to do them.

Savings:

class Savings extends Account{
//need to create a constructor
  public Savings(int id, double balance, double annualInterestRate) {
//need to invoke the constructor for Account
  super(id, balance, annualInterestRate);
 }

// need to override the withdraw method in Account
public void withdraw(double amount) {
    // place logic to prevent the account from being overdrawn
    // that is do not allow a negative balance
  }
}

Upvotes: 0

Views: 139

Answers (2)

PlasmaPower
PlasmaPower

Reputation: 1878

super() invokes the constructor for the super-class, in the Savings case, Account.

Upvotes: 1

Kakarot
Kakarot

Reputation: 4252

You just need to check id the amount < balance Or whatever logic you have (eg: you want some threshold amount to be present in the account).

Also I would recommend to look into synchronized methods as you should synchronize access to account of a given user to ensure that when a withdraw function is invoked...another withdraw on the same user account has to wait...or else it will lead to problems...I will leave that to you to figure out.

public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance

     if(balance < amount)
     {
        // print error message or do something
     }
     else
     {
        // withdraw the money
        balance -= amount;
        // print message or do something
     }
}

Upvotes: 1

Related Questions