Need to write a java program based on the class \"BankAccount\" *MUST HAVE A SMA
ID: 3585584 • Letter: N
Question
Need to write a java program based on the class "BankAccount"
*MUST HAVE A SMALL MAIN METHOD AT THE END OF THE PROGRAM THAT CALLS THE CLASS*
This program should only have "BankAccount" class with no extensions.
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.
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.
Create a minimum of three private data attributes for your class. These can all be primitive variables (int, char, long, double etc..), or they can be reference variables (another class*).
Remember, do not put functionality in your main method that is better done in a class method. *MUST HAVE A SMALL MAIN METHOD*
Explanation / Answer
//BankAccount.java
import java.util.Scanner;
public class BankAccount {
//Private static Variables
private static double checkingAccount = 54241.78;
private static double savingsAccount = 65341.89;
private static double retirementAccount = 844567.34;
//Withdraw Amount
public static void withdrawAmount(double amount,String accType){
if(amount>checkingAccount){
System.out.println("Low Balance...");
}else{
if(accType.equals("Checkings")){
checkingAccount = checkingAccount - amount;
}else if(accType.equalsIgnoreCase("Savings")){
savingsAccount = savingsAccount - amount;
}else{
retirementAccount = retirementAccount - amount;
}
System.out.println("Amount withdrawn is:"+amount);
}
accountInfo();
}
//Deposit Amount method
public static void depositAmount(double amount,String accType){
if(accType.equals("Checkings")){
checkingAccount = checkingAccount + amount;
}else if(accType.equalsIgnoreCase("Savings")){
savingsAccount = savingsAccount + amount;
}else{
retirementAccount = retirementAccount + amount;
}
System.out.println("Amount deposited is:"+amount);
accountInfo();
}
//Transfer Amount method
public static void transferAmount(double amount,String fromAccount, String toAccount){
if(fromAccount.equalsIgnoreCase("checkings")){
if(toAccount.equalsIgnoreCase("savings")){
checkingAccount = checkingAccount - amount;
savingsAccount = savingsAccount +amount;
}else{
checkingAccount = checkingAccount - amount;
retirementAccount = retirementAccount + amount;
}
}else if(fromAccount.equalsIgnoreCase("savings")){
if(toAccount.equalsIgnoreCase("checkings")){
savingsAccount = savingsAccount - amount;
checkingAccount = checkingAccount + amount;
}else{
savingsAccount = savingsAccount - amount;
retirementAccount = retirementAccount + amount;
}
}else{
if(toAccount.equalsIgnoreCase("checkings")){
retirementAccount = retirementAccount - amount;
checkingAccount = checkingAccount + amount;
}else{
retirementAccount = retirementAccount - amount;
savingsAccount = savingsAccount + amount;
}
}
System.out.println("Amount "+amount+" is transferred from "+fromAccount+" to "+toAccount);
accountInfo();
}
//Account information
public static void accountInfo(){
System.out.println("Checking Account: "+checkingAccount);
System.out.println("Savings Account: "+savingsAccount);
System.out.println("Retirement Account: "+retirementAccount);
}
//Main Method
public static void main(String[] args) {
System.out.println("*****Account Information****");
BankAccount.accountInfo();
System.out.println("****************************");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the transaction type:(| withdraw | deposit | transfer |)");
String tType = scanner.next();
double amount;
if(tType.equalsIgnoreCase("withdraw")){
System.out.println("Enter the amount to withdraw:");
amount = scanner.nextDouble();
System.out.println("Enter the Account type: (Checkings | Savings | Retirement)");
String accType = scanner.next();
withdrawAmount(amount,accType);
}else if(tType.equalsIgnoreCase("deposit")){
System.out.println("Enter the amount to deposit:");
amount = scanner.nextDouble();
System.out.println("Enter the Account type: (Checkings | Savings | Retirement)");
String accType = scanner.next();
depositAmount(amount,accType);
}else if(tType.equalsIgnoreCase("transfer")){
System.out.println("Enter the amount to transfer:");
amount = scanner.nextDouble();
System.out.println("Enter the from Account: (Checkings | Savings | Retirement)");
String fromAccount = scanner.next();
System.out.println("Enter the to Account: (Checkings | Savings | Retirement)");
String toAccount = scanner.next();
transferAmount(amount, fromAccount, toAccount);
}else{
System.out.println("Invalid Transaction Type selected...");
}
scanner.close();
}
}
/* Sample Outputs:
Output-1:
*****Account Information****
Checking Account: 54241.78
Savings Account: 65341.89
Retirement Account: 844567.34
****************************
Enter the transaction type:(| withdraw | deposit | transfer |)
transfer
Enter the amount to transfer:
20000
Enter the from Account: (Checkings | Savings | Retirement)
retirement
Enter the to Account: (Checkings | Savings | Retirement)
savings
Amount 20000.0 is transferred from retirement to savings
Checking Account: 54241.78
Savings Account: 85341.89
Retirement Account: 824567.34
Output-2:
*****Account Information****
Checking Account: 54241.78
Savings Account: 65341.89
Retirement Account: 844567.34
****************************
Enter the transaction type:(| withdraw | deposit | transfer |)
deposit
Enter the amount to deposit:
20000
Enter the Account type: (Checkings | Savings | Retirement)
Savings
Amount deposited is:20000.0
Checking Account: 54241.78
Savings Account: 85341.89
Retirement Account: 844567.34
Output-3:
*****Account Information****
Checking Account: 54241.78
Savings Account: 65341.89
Retirement Account: 844567.34
****************************
Enter the transaction type:(| withdraw | deposit | transfer |)
withdraw
Enter the amount to withdraw:
30000
Enter the Account type: (Checkings | Savings | Retirement)
retirement
Amount withdrawn is:30000.0
Checking Account: 54241.78
Savings Account: 65341.89
Retirement Account: 814567.34
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.