*JAVA CODE* does not need an account number or anything. This a general ATM prog
ID: 3606122 • Letter: #
Question
*JAVA CODE*
does not need an account number or anything. This a general ATM program.
take the following code (Bank Account class) and extend the class to Checking, Savings, and Retirement instead of it all being in the main class.
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
Hi... I have seperated code for savings, checkings and retirements classes. Please check once.
BankMain.java
import java.util.Scanner;
public class BankMain {
public BankMain(){
}
public void bankFunctions(){
Savings savingsAcc = new Savings();
Checking checkingAcc = new Checking();
Retirement retirementAcc = new Retirement();
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();
if(accChoice == 1){
savingsAcc.withdraw(amount);
}else if(accChoice == 2){
checkingAcc.withdraw(amount);
}else if(accChoice == 3){
retirementAcc.withdraw(amount);
}else{
System.out.println("Invalid choice!");
}
//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();
if(accChoice == 1){
savingsAcc.deposit(amount);
}else if(accChoice == 2){
checkingAcc.deposit(amount);
}else if(accChoice == 3){
retirementAcc.deposit(amount);
}else{
System.out.println("Invalid choice!");
}
//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();
if(secondChoice>3){
System.out.println("Invalid choice!");
break;
}
if(accChoice == 1){
savingsAcc.transfer(secondChoice,amount);
}else if(accChoice == 2){
checkingAcc.transfer(secondChoice,amount);
}else if(accChoice == 3){
retirementAcc.transfer(secondChoice,amount);
}else{
System.out.println("Invalid choice!");
}
//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) {
BankMain myBankAccount = new BankMain();
myBankAccount.bankFunctions();
}
}
Savings.java
public class Savings {
private double currentBal,previousBal;
public Savings(){
//currentBal = 0.0;
//previousBal = 0.0;
}
public void deposit(double amount) {
previousBal = currentBal;
currentBal += amount;
}
public void withdraw(double amount) {
previousBal = currentBal;
currentBal -= amount;
}
public void transfer(int secondChoice,double amount){
if(secondChoice == 1){
Savings s = new Savings();
s.deposit(amount);
}else if(secondChoice == 2){
Checking s = new Checking();
s.deposit(amount);
}else if(secondChoice == 3){
Retirement s = new Retirement();
s.deposit(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;}
}
Checking.java
public class Checking {
private double currentBal,previousBal;
public Checking(){
//currentBal = 0.0;
//previousBal = 0.0;
}
public void deposit(double amount) {
previousBal = currentBal;
currentBal += amount;
}
public void withdraw(double amount) {
previousBal = currentBal;
currentBal -= amount;
}
public void transfer(int secondChoice,double amount){
if(secondChoice == 1){
Savings s = new Savings();
s.deposit(amount);
}else if(secondChoice == 2){
Checking s = new Checking();
s.deposit(amount);
}else if(secondChoice == 3){
Retirement s = new Retirement();
s.deposit(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;}
}
Retirement.java
public class Retirement {
private double currentBal,previousBal;
public Retirement(){
//currentBal = 0.0;
//previousBal = 0.0;
}
public void deposit(double amount) {
previousBal = currentBal;
currentBal += amount;
}
public void withdraw(double amount) {
previousBal = currentBal;
currentBal -= amount;
}
public void transfer(int secondChoice,double amount){
if(secondChoice == 1){
Savings s = new Savings();
s.deposit(amount);
}else if(secondChoice == 2){
Checking s = new Checking();
s.deposit(amount);
}else if(secondChoice == 3){
Retirement s = new Retirement();
s.deposit(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;}
}
Output:
Enter the balance of your Savings account:
20
Enter the balance of your Checking account:
30
Enter the balance of your Retirement account:
40
Please enter:
1 for Withdrawal
2 for Deposit
3 for Transfer
3
Please select from which account to transfer:
1 for Savings Account
2 for Checking Account
3 for Retirement Account
1
Please select to which account to transfer:
1 for Savings Account
2 for Checking Account
3 for Retirement Account
2
Enter amount to transfer:
10
Savings Account: 10.0
Checking Account: 30.0
Retirement Account: 40.0
Please check the code and let me know any querries. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.