(Subclasses of Account) In Programming Exercise 9.7, the Account class was defin
ID: 3912949 • Letter: #
Question
(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 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.
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.
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. You withdraw method in each class needs to check that the rule is enforced prior to adjusting the balance. If an illegal balance is attempted, display a message to the user that they are in overdraft.
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
// This is my Main
import java.util.Scanner;
public class AccountTest {
private static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
Account[] accounts = new Account[10];
for (int i = 0; i <= 9; i++) {
accounts[i] = new Account(i, 100.0);
}
System.out.print("Enter an id (1 - 10): ");
int id = in.nextInt();
if (id < 1 || id > 10) {
id = incorrectId(id);
}
while (true) {
menuDisplay();
System.out.print("Enter a choice: ");
int choice = in.nextInt();
if (choice == 4) {
System.out.printf("Enter an id (1 - 10): ");
id = in.nextInt();
if (id < 1 || id > 10) {
id = incorrectId(id);
}
}
userInput(id, choice, accounts);
}
}
public static int incorrectId(int id) {
while (id < 1 || id > 10) {
System.out.print("Please enter a valid id: ");
id = in.nextInt();
System.out.println();
}
return id;
}
public static void userInput(int id, int choice, Account[] accounts) {
switch (choice) {
case 1:
System.out.printf("The balance is " + accounts[id].getBalance());
break;
case 2:
System.out.print("Enter the amount to withdraw: ");
accounts[id].withdraw(in.nextDouble());
break;
case 3:
System.out.print("Enter the amount to deposit: ");
accounts[id].deposit(in.nextDouble());
break;
default:
break;
}
}
public static void menuDisplay() {
System.out.printf(" Main menu ");
System.out.println("1: check balance");
System.out.println("2: withdraw");
System.out.println("3: deposit");
System.out.println("4: exit");
}
}
//
/
import java.util.Date;
public class Account {
private int id;
private double balance;
private static double annualInterestRate;
private Date dateCreated;
Account() {
this(0, 0);
}
Account(int id, double balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 0;
dateCreated = new Date();
}
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 int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public String getDateCreated() {
return dateCreated.toString();
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return balance * (getMonthlyInterestRate() / 100);
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
public String toString() {
return " Account ID: " + id + " Date created: " + getDateCreated()
+ " Balance: $" + balance +
" Monthly interest: $" + getMonthlyInterest();
}
}
Explanation / Answer
// Saving Account Subclass
// Test Class For above mentioned Task
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.