Need to modify a java program that has the class \"BankAccount\" This program sh
ID: 3607015 • Letter: N
Question
Need to modify a java program that has the class "BankAccount"
This program should have "BankAccount" class with the extensions of Checking, Savings, and Retirement classes.
OUTPUT OF PROGRAM:
This program should ask the user for their balance in their Checking, Savings, And Retirement accounts.
Once the user enters the amounts, the program should ask if the user wants to withdraw, deposit, or transfer money from a specific account.
When the user enters what they want to do(if transfer, have program ask from what account to what account), then the program should ask for what amount.
Once the user enters the amount, the program will display all 3 accounts with the adjusted balance.
HERE IS THE ORIGINAL CODE I NEED MODIFIED:
import java.util.Scanner;
public class BankAccount {
private double currentBal,previousBal;
private static char type;
public BankAccount(char type){
BankAccount.type = type;
}
public void deposit(double amount) {
previousBal = currentBal;
currentBal += amount;
}
public void withdraw(double amount) {
previousBal = currentBal;
currentBal -= amount;
}
public void transfer(BankAccount otherAccount,double amount){
otherAccount.deposit(amount);
this.withdraw(amount);
}
public double getCurrentBal(){
return currentBal;
}
public void setCurrentBal(double currentBal) {
this.currentBal = currentBal;
}
public double getPreviousBal() {
return previousBal;
}
public void setPreviousBal(double previousBal) {
this.previousBal = previousBal;}
public char getType() {
return type;
}
public void setType(char type) {
BankAccount.type = type;
}
public void bankFunctions(){
BankAccount savingsAcc = new BankAccount('S');
BankAccount checkingAcc = new BankAccount('C');
BankAccount retirementAcc = new BankAccount('R');
BankAccount[] accsArr = {savingsAcc,checkingAcc,retirementAcc};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the balance of your Savings account: ");
savingsAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Enter the balance of your Checking account: ");
checkingAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Enter the balance of your Retirement account: ");
retirementAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Please enter: 1 for Withdrawal 2 for Deposit 3 for Transfer");
int choice = scanner.nextInt();
int accChoice = 0;
double amount = 0;
switch(choice) {
case 1:
System.out.println("Please select account to withdraw from: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Enter amount to withdraw: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].withdraw(amount);
break;
case 2:
System.out.println("Please select account to deposit into: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Enter amount to deposit: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].deposit(amount);
break;
case 3:
System.out.println("Please select from which account to transfer: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Please select to which account to transfer: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
int secondChoice = scanner.nextInt();
System.out.println("Enter amount to transfer: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].transfer(accsArr[secondChoice-1], amount);
break;
}
System.out.println("Savings Account: "+savingsAcc.getCurrentBal());
System.out.println("Checking Account: "+checkingAcc.getCurrentBal());
System.out.println("Retirement Account: "+retirementAcc.getCurrentBal());
scanner.close();
}
public static void main(String[] args) {
BankAccount myBankAccount = new BankAccount(type);
myBankAccount.bankFunctions();
}
}
Explanation / Answer
Answer: Below is the modified program, along with the subclasses added. Please refer to the comments which explain the modifications done. The concept of Inheritance has been used, and three child classes, SavingsAccount, CheckingAccount and RetirementAccount have been created as subclasses of BankAccount.
Modified program:
import java.util.Scanner;
public class BankAccount {
private double currentBal,previousBal;
private static char type;
public BankAccount(char type){
BankAccount.type = type;
}
public void deposit(double amount) {
previousBal = currentBal;
currentBal += amount;
}
public void withdraw(double amount) {
previousBal = currentBal;
currentBal -= amount;
}
public void transfer(BankAccount otherAccount,double amount){
otherAccount.deposit(amount);
this.withdraw(amount);
}
public double getCurrentBal(){
return currentBal;
}
public void setCurrentBal(double currentBal) {
this.currentBal = currentBal;
}
public double getPreviousBal() {
return previousBal;
}
public void setPreviousBal(double previousBal) {
this.previousBal = previousBal;
}
public char getType() {
return type;
}
public void setType(char type) {
BankAccount.type = type;
}
public void bankFunctions(){
/*
The accounts are instantiated to be objects of the specific subclasses.
accChoice will access the specific object (account) according to index
specified of accsArr array.
*/
BankAccount savingsAcc = new SavingsAccount();
BankAccount checkingAcc = new CheckingAccount();
BankAccount retirementAcc = new RetirementAccount();
BankAccount[] accsArr = {savingsAcc,checkingAcc,retirementAcc};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the balance of your Savings account: ");
savingsAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Enter the balance of your Checking account: ");
checkingAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Enter the balance of your Retirement account: ");
retirementAcc.setCurrentBal(scanner.nextDouble());
System.out.println("Please enter: 1 for Withdrawal 2 for Deposit 3 for Transfer");
int choice = scanner.nextInt();
int accChoice = 0;
double amount = 0;
switch(choice) {
case 1:
System.out.println("Please select account to withdraw from: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Enter amount to withdraw: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].withdraw(amount);
break;
case 2:
System.out.println("Please select account to deposit into: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Enter amount to deposit: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].deposit(amount);
break;
case 3:
System.out.println("Please select from which account to transfer: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
accChoice = scanner.nextInt();
System.out.println("Please select to which account to transfer: 1 for "
+ "Savings Account 2 for Checking Account 3 for Retirement Account");
int secondChoice = scanner.nextInt();
System.out.println("Enter amount to transfer: ");
amount = scanner.nextDouble();
accsArr[accChoice-1].transfer(accsArr[secondChoice-1], amount);
break;
}
System.out.println("Savings Account: "+savingsAcc.getCurrentBal());
System.out.println("Checking Account: "+checkingAcc.getCurrentBal());
System.out.println("Retirement Account: "+retirementAcc.getCurrentBal());
scanner.close();
}
public static void main(String[] args) {
BankAccount myBankAccount = new BankAccount(type);
myBankAccount.bankFunctions();
}
}
/* The child classes of BankAccount are SavingsAccount, CheckingAccount and RetirementAccount, which inherit
all fields and methods from parent class, with same functionality logic being implemented in them */
//SavingsAccount child class
public class SavingsAccount extends BankAccount{
SavingsAccount(){
super('S');
// calls parent class BankAccount, setting category to 'S'
}
}
//CheckingAccount child class
public class CheckingAccount extends BankAccount{
CheckingAccount(){
super('C');
// calls parent class BankAccount, setting category to 'C'
}
}
//RetirementAccount child class
public class RetirementAccount extends BankAccount{
RetirementAccount(){
super('R');
// calls parent class BankAccount, setting category to 'R'
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.