Account class is public class Account { private int id; protected double balance
ID: 3791333 • Letter: A
Question
Account class is
public class Account {
private int id;
protected double balance;
private double annualInterestRate;
private Date dateCreated;
Account(){
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
Account(int ID, double Balance){
id = ID;
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() {
return annualInterestRate/12.0;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public void deposit(double amount) {
balance = balance + amount;
}
}
tester class is
public class Prob1_Tester {
public static void main (String[] args) {
CheckingAccount account = new CheckingAccount(1122, 1000.0, 5000.0);
printCheckingInfo(account);
account.withdraw(2000.0);
printCheckingInfo(account);
account.withdraw(5000.0);
printCheckingInfo(account);
account.setOverdraftLimit(2000.0);
printCheckingInfo(account);
account.withdraw(1001.0);
printCheckingInfo(account);
account.withdraw(999.0);
printCheckingInfo(account);
SavingsAccount sAccount = new SavingsAccount(1122, 1000.0);
printSavingsInfo(sAccount);
sAccount.withdraw(1001.0);
printSavingsInfo(sAccount);
sAccount.withdraw(999.0);
printSavingsInfo(sAccount);
}
public static void printCheckingInfo( CheckingAccount acnt) {
System.out.printf("Bal: $%4.2f, overdraft limit: $%4.2f ", acnt.getBalance(),
acnt.getOverdraftLimit());
}
public static void printSavingsInfo( SavingsAccount acnt) {
System.out.printf("Bal: $%4.2f ", acnt.getBalance());
}
}
****************** print out********
Bal: $-1000.00, overdraft limit: $5000.00
Bal: $-1000.00, overdraft limit: $5000.00
Bal: $-1000.00, overdraft limit: $2000.00
Bal: $-1000.00, overdraft limit: $2000.00
Bal: $-1999.00, overdraft limit: $2000.00
Bal: $1000.00
Bal: $1000.00
Bal: $1.00
Name the two subclasses: CheckingAccount and SavingsAccount. The CheckingAccount class should have at least 3 constructors to be consistent with the superclass. However, you will only provide one. This one constructor should accept: id, balance, and overdraft limit. It should utilize a superclass constructor. Similar for SavingsAccount, supply one constructor that accepts id and balance and utilizes a superclass constructor. Make sure the checking account subclass has methods to get and set the overdraft limit The problem says, "a checking account has an overdraft limit." This is the way this should be handled a. Override the withdraw method. Only allow a withdrawal if the balance does not go below the negative of the overdraft limit. Example: Balance $1000, OverdraftLimit- $5000, Cannot withdraw $7000 (Balance would be -6000 which is less than -5000). Can withdraw S5900 (Balance would be -4900 which is greater than -5000) b. Technically, I guess you should also override the setBalance method to work in the same way to be consistent, but you do not need to do that. I don't think the class should even have this method. The balance should be read-only, and modified only through the methods withdraw and deposit. The problem says, "a savings account cannot be overdrawn." This simply means that the balance can never be negative. Similar to the checking account except that we don't need the overdraft limit. Write a test class AccountTester in package prob1. which is exactly as shown belo (copy/paste). Your code w should work perfectly with this test class:Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class SavingsAccount extends Account {
public SavingsAccount(int ID, double Balance) {
super(ID, Balance);
}
public void withdraw(double amount) {
if(balance - amount >= 0)
balance = balance - amount;
}
}
public class CheckingAccount extends Account{
private double overdraftLimit;
public CheckingAccount(int ID, double Balance, double limit){
super(ID, Balance);
overdraftLimit = limit;
}
public double getOverdraftLimit() {
return overdraftLimit;
}
public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
public void withdraw(double amount) {
if((getBalance() - amount) >= -overdraftLimit){
balance = balance - amount;
}
}
}
public class Prob1_Tester {
public static void main (String[] args) {
CheckingAccount account = new CheckingAccount(1122, 1000.0, 5000.0);
printCheckingInfo(account);
account.withdraw(2000.0);
printCheckingInfo(account);
account.withdraw(5000.0);
printCheckingInfo(account);
account.setOverdraftLimit(2000.0);
printCheckingInfo(account);
account.withdraw(1001.0);
printCheckingInfo(account);
account.withdraw(999.0);
printCheckingInfo(account);
SavingsAccount sAccount = new SavingsAccount(1122, 1000.0);
printSavingsInfo(sAccount);
sAccount.withdraw(1001.0);
printSavingsInfo(sAccount);
sAccount.withdraw(999.0);
printSavingsInfo(sAccount);
}
public static void printCheckingInfo( CheckingAccount acnt) {
System.out.printf("Bal: $%4.2f, overdraft limit: $%4.2f ", acnt.getBalance(),
acnt.getOverdraftLimit());
}
public static void printSavingsInfo( SavingsAccount acnt) {
System.out.printf("Bal: $%4.2f ", acnt.getBalance());
}
}
/*
Sample run:
Bal: $1000.00, overdraft limit: $5000.00
Bal: $-1000.00, overdraft limit: $5000.00
Bal: $-1000.00, overdraft limit: $5000.00
Bal: $-1000.00, overdraft limit: $2000.00
Bal: $-1000.00, overdraft limit: $2000.00
Bal: $-1999.00, overdraft limit: $2000.00
Bal: $1000.00
Bal: $1000.00
Bal: $1.00
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.