My problem says to create an account class with account number, balance, annual interest rate, and date created, and methods to deposit and withdraw. (which i have done) Then create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Implement the classes, write a test program that creates objects of account, savings account, and checking account, and invokes their toString() methods.
Ok so i have done the account class, created the subclasses and made toString for each savings and checking. What i don't understand is basically how to use the account methods for a checking/saving type account when the checking/saving are subclasses.
public class ex9_3 {
public static void main(String[] args) {
//create new account
Account account3 = new Checkings();
Account account2 = new Savings();
Account account1 = new Account(1122, 20000, 4.5);
account1.deposit(3000);
account1.withdraw(2500);
//Print out required
//Account
System.out.println("\nAccount ID: " + account1.getID());
System.out.println("Balance: " + account1.getBalance());
System.out.println("Monthly interest rate: " + account1.getMonthlyInterestRate());
System.out.println("Date created: " + account1.getdate());
System.out.println("toString: " + account1.toString());
//Savings
System.out.println("toString: " + account2.toString());
//Checking
System.out.println("toString: " + account3.toString());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated = new java.util.Date();
// constructors
public Account () {
java.util.Date dateCreated = new java.util.Date();
}
//all variables included
public Account (int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
//setdate
public void setdate(java.util.Date dateCreated) {
this.dateCreated = dateCreated;
}
//return the date
public java.util.Date getdate(){
return dateCreated;
}
//returning and setting each vars
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//deposit method
public double deposit (double deposit){
balance += deposit;
return(deposit);
}
//withdraw method
public double withdraw ( double withdraw) {
this.balance -= withdraw;
return(withdraw);
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestrate) {
this.annualInterestRate = annualInterestRate;
}
//monthly interest rate and formula
public double getMonthlyInterestRate() {
double monthlyInterest = (annualInterestRate / 1200) * balance;
return monthlyInterest;
}
}
//Checkings class
class Checkings extends Account {
int overdraft = 400;
public String toString() {
return "Checkings Account " + "overdraft limit " + overdraft;
}
}
//Savings Class
class Savings extends Account {
int overdraft = 0;
public String toString() {
return "Savings Account" + "overdraft limit " + overdraft;
}
}