this is in Java (Subclasses of Account) In Programming Exercise 9.7, the Account
ID: 3840051 • Letter: T
Question
this is in Java
(Subclasses of Account) In Programming Exercise 9.7, 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 with- draw funds. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn Draw the UML diagram for the classes and then implement them. Write a test program that creates objects of Account, SavingsAccount, and Checking Account and invokes their tostring() methods.Explanation / Answer
//Test class
package sample1;
import java.util.Random;
import java.util.Scanner;
import java.util.Date;
public class test
{
public static void main(String args[]){
SavingsAccount sa=new SavingsAccount(1,1000.0,10.0,new Date(),0.0);
CheckingAccount ca=new CheckingAccount(1,1000.0,10.0,new Date(),100.0);
System.out.println("savings details:"+sa.toString());
System.out.println("checking acc details :"+ca.toString());
sa.deposit(100.0);
ca.withdraw(1100.0);
System.out.println("savings details:"+sa.toString());
System.out.println("checking acc details :"+ca.toString());
ca.withdraw(100.0);
}
}
//Account class
package sample1;
import java.util.Date;
public class Account {
private int accountNo;
private double balance;
private double rate;
private Date createdDate;
private double overdraftLimit;
public double getOverdraftLimit() {
return overdraftLimit;
}
public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
public Account(int accountNo, double balance, double rate, Date createdDate, double overdraftLimit) {
super();
this.accountNo = accountNo;
this.balance = balance;
this.rate = rate;
this.createdDate = createdDate;
this.overdraftLimit = overdraftLimit;
}
public Account(int accNo){
this.accountNo=accNo;
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [accountNo=" + accountNo + ", balance=" + balance + ", rate=" + rate + ", createdDate="
+ createdDate + "]";
}
public Account(int accountNo, double balance, double rate, Date createdDate) {
super();
this.accountNo = accountNo;
this.balance = balance;
this.rate = rate;
this.createdDate = createdDate;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public void withdraw(double amount){
if(-this.overdraftLimit<=this.balance-amount)
this.balance-=amount;
else
System.out.println("withdraw failed : Overdraft limit crossed");
}
public void deposit(double amount){
this.balance+=amount;
}
}
//Savings account
package sample1;
import java.util.Date;
public class SavingsAccount extends Account{
public SavingsAccount(int accountNo, double balance, double rate, Date createdDate, double overdraftLimit) {
super(accountNo, balance, rate, createdDate, overdraftLimit);
// TODO Auto-generated constructor stub
}
public SavingsAccount(int accNo) {
super(accNo);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "SavingsAccount []"+super.toString();
}
}
// Checking account
package sample1;
import java.util.Date;
public class CheckingAccount extends Account{
public CheckingAccount(int accountNo, double balance, double rate, Date createdDate, double overdraftLimit) {
super(accountNo, balance, rate, createdDate, overdraftLimit);
// TODO Auto-generated constructor stub
}
public CheckingAccount(int accNo) {
super(accNo);
// TODO Auto-generated constructor stub
}
public String toString(){
return super.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.