the Account class was defined to model a bank account. An account has the proper
ID: 3863369 • Letter: T
Question
the Account class was defined to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Do not to alter your Account class, but create two new subclasses that make use of class Account. If you need an Account class, please contact me.
Write a test program that creates objects of Account, SavingsAccount, and CheckingAccount and invokes their toString() methods. Include a toString method in SavingsAccount and in CheckingAccount. CheckingAccount constructor will receive three values, id, initial balance and overdraft limit. You will want to have a property overdraftAmt in your class CheckingAccount. Your diagram needs to reflect all inheritance.
Clarification - A checking account balance may go below $0 but not below -$100. Overdraft means you can withdraw up to $100 before the “bank” cuts you off. A savings account balance may not go below $0. Your withdraw method in each subclass will need to check to make sure the balance rule is met before doing the withdrawal.
Make a main that will show that your classes are working correctly. Create a checking account and a savings account. Make deposits and withdrawals from each, attempt to go into overdraft. Make sure your main will print output indicating what is going on along the way.
UML Diagram
Account
-id: int
-balance: double
-annualInterestRate : double
-dateCreated : Date
+Account( )
+Account(someId : int, someBalance : double)
+getId() : int
+setId(newId : int) : void
+getBalance() : double
+setBalance(newBalance : double) : void
+getAnnualInterestRate( ) : double
+setAnnualInterestRate(newRate : double) : void
+getDateCreated( ) : Date
+getMonthlyInterestRate( ) : double
+getMonthlyInterest( ) : double
+withdraw(amt : double) : void
+deposit(amt : double) : void
Subclasses of Account
Checking
-overDraft
+Checking(id : int, balance : double, overdraft : double)
+withdraw(amount : double) : void
+toString( ) : String
Savings
+Savings(id : int, balance : double)
+withdraw(amount : double) : void
+toString( ) : String
Here is the Account class I created for Account:
import java.util.Date;
public class Account {
private int id;
private double balance;
static private double annualInterestRate = 0;
private Date dateCreated;
public Account() {
dateCreated = new Date();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
dateCreated = new Date();
}
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;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
double monthlyInterestRate = getAnnualInterestRate() / 1200;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
double monthlyInterest= getBalance() * getMonthlyInterestRate();
return monthlyInterest;
}
public void withdraw(double amount) {
balance = getBalance() - amount;
}
public void deposit(double amount) {
balance = getBalance() + amount;
}
}
Explanation / Answer
Account.java
import java.util.Date;
public class Account {
private int id;
private double balance;
private double annualInterestRate = 0;
private Date dateCreated;
public Account() {
dateCreated = new Date();
}
public Account(int id, double balance) {
super();
this.id = id;
this.balance = balance;
dateCreated = new Date();
}
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;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
double monthlyInterestRate = getAnnualInterestRate() / 1200;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
double monthlyInterest= getBalance() * getMonthlyInterestRate();
return monthlyInterest;
}
public void withdraw(double amount) {
balance = getBalance() - amount;
}
public void deposit(double amount) {
balance = getBalance() + amount;
}
@Override
public String toString() {
return "id=" + id + ", balance=" + balance
+ ", annualInterestRate=" + annualInterestRate
+ ", dateCreated=" + dateCreated ;
}
}
____________________
Checking.java
public class Checking extends Account {
private double overDraft;
public Checking(int id, double balance,double overDraft) {
super(id, balance);
this.overDraft=overDraft;
}
public void withdraw(double amount)
{
if((getBalance()-amount)<overDraft)
{
System.out.println("*** Exceded Overgrat limit.Could'nt Withdraw **");
}
else
super.setBalance(getBalance()-amount);
}
@Override
public String toString() {
return super.toString()+"Checking overDraft=" + overDraft+" ";
}
}
___________________
Savings.java
public class Savings extends Account {
public Savings(int id,double balance)
{
super(id, balance);
}
public void withdraw(double amount )
{
if(getBalance()-amount>0)
{
super.setBalance(getBalance()-amount);
}
else
{
System.out.println("** Account Do not have enough balance **");
}
}
@Override
public String toString() {
return super.toString()+" ";
}
}
_____________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating the Checking Account class Object
Checking check=new Checking(111,50000, -100);
//Displaying the Checking Account info
System.out.println("Checking Account Info# "+check.toString());
//Displaying the checking account balance
System.out.println("Checking Account balance before withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 5000");
//Performing withdraw operation
check.withdraw(5000);
//Displaying the checking account balance
System.out.println("Checking Account balance after withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 45100");
//Performing withdraw operation
check.withdraw(45100);
//Displaying the checking account balance
System.out.println("Checking Account balance after withdrawl:"+check.getBalance());
System.out.println("Performing Withdrawl of 1000");
check.withdraw(1000);
System.out.println("Checking Account balance :"+check.getBalance());
//Creating the Savings class Object
Savings saving=new Savings(1234,30000);
//Displaying the Savings Account info
System.out.println(" Savings Account Info # "+saving.toString());
//Displaying the Savings account balance
System.out.println("Savings Account balance before withdrawl:"+saving.getBalance());
System.out.println("Performing Withdrawl of 7000");
//Performing withdraw operation
saving.withdraw(7000);
//Displaying the checking account balance
System.out.println("Savings Account balance before withdrawl:"+saving.getBalance());
System.out.println("Performing Withdrawl of 31000");
//Performing withdraw operation
saving.withdraw(31000);
//Displaying the checking account balance
System.out.println("Savings Account balance:"+saving.getBalance());
}
}
____________________
Checking Account Info#
id=111, balance=50000.0, annualInterestRate=0.0, dateCreated=Sat Mar 18 02:40:25 IST 2017Checking
overDraft=-100.0
Checking Account balance before withdrawl:50000.0
Performing Withdrawl of 5000
Checking Account balance after withdrawl:45000.0
Performing Withdrawl of 45100
Checking Account balance after withdrawl:-100.0
Performing Withdrawl of 1000
*** Exceded Overgrat limit.Could'nt Withdraw **
Savings Account Info #
id=1234, balance=30000.0, annualInterestRate=0.0, dateCreated=Sat Mar 18 02:40:25 IST 2017
Savings Account balance before withdrawl:30000.0
Performing Withdrawl of 7000
Savings Account balance before withdrawl:23000.0
Performing Withdrawl of 31000
** Account Do not have enough balance **
____________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.