patternjavaMinor
Simple bank account
Viewed 0 times
banksimpleaccount
Problem
I believe it is very much possible to make this code more efficient, possibly adding interfaces, inheritance or possibly divide the main class into more classes. I will improve some of the code such as blocking withdraws into negative amounts and so forth.
Main class:
```
import java.util.Scanner;
public class Main
{
public static void main(String [] args){
System.out.println("Welcome to the ATM: Press any key to continue");
try
{
System.in.read();
}
catch(Exception e)
{}
Main obj = new Main();
obj.mainMenu();
}
public void menuSavChe(){
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Logout");
System.out.print("Enter Selection: ");
}
public void menuDepWith(){
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. View Account Balance");
System.out.println("4. Logout");
System.out.print("Enter Selection: ");
}
public void mainMenu(){
menuSavChe();
int selection = input.nextInt();
switch(selection){
case 1:
optionChecking();
break;
case 2:
optionSavings();
break;
case 3:
logOut();
break;
}
}
public void optionChecking(){
menuDepWith();
int inputOption = input.nextInt();
switch(inputOption){
case 1:
depositChecking();
break;
case 2:
withdrawChecking();
break;
case 3:
accountInfoChecking();
break;
case 4:
logOut();
break;
}
}
public void depositChecking(){
System.out.print("Enter deposit amount: ");
double amount = input.nextDouble();
withDep.deposit(amount
Main class:
```
import java.util.Scanner;
public class Main
{
public static void main(String [] args){
System.out.println("Welcome to the ATM: Press any key to continue");
try
{
System.in.read();
}
catch(Exception e)
{}
Main obj = new Main();
obj.mainMenu();
}
public void menuSavChe(){
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Logout");
System.out.print("Enter Selection: ");
}
public void menuDepWith(){
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. View Account Balance");
System.out.println("4. Logout");
System.out.print("Enter Selection: ");
}
public void mainMenu(){
menuSavChe();
int selection = input.nextInt();
switch(selection){
case 1:
optionChecking();
break;
case 2:
optionSavings();
break;
case 3:
logOut();
break;
}
}
public void optionChecking(){
menuDepWith();
int inputOption = input.nextInt();
switch(inputOption){
case 1:
depositChecking();
break;
case 2:
withdrawChecking();
break;
case 3:
accountInfoChecking();
break;
case 4:
logOut();
break;
}
}
public void depositChecking(){
System.out.print("Enter deposit amount: ");
double amount = input.nextDouble();
withDep.deposit(amount
Solution
I think a MenuOption should be a separate class. A MenuOption has a name, a pick function, and a way to call functions on your Main class where you have all your business logic. Right now you have duplication in the numbers for menu options and in the handling of menus.
This is where you're duplicating the numbers for menu options. If you could make a class which contains a method to call - anonymous classes, perhaps, like so:
then you'd be able to get rid of part of this duplication. To get rid of the numbers, you'll probably want to use a List.
Like so, you can then print a menu:
With something like that, you can get rid of the duplication of numbers. You can also use this to combine the menu printing functions into one function which takes a menu to print and then prints it. Like that, adding a new menu would be easier.
As for your Savings class, you can make use of
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Logout");
//...
switch(selection){
case 1:
optionChecking();
break;
case 2:
optionSavings();
break;
case 3:
logOut();
break;
}This is where you're duplicating the numbers for menu options. If you could make a class which contains a method to call - anonymous classes, perhaps, like so:
new MenuOption(this, "Deposit"){
@Override
public void pick(){
main.depositChecking();
}
};then you'd be able to get rid of part of this duplication. To get rid of the numbers, you'll probably want to use a List.
List mainMenu = new ArrayList<>();
mainMenu.add(new MenuOption(this, "Checking"){...});
mainMenu.add(new MenuOption(this, "Savings"){...});Like so, you can then print a menu:
for(int i = 0; i < menu.size(); i++){
MenuOption option = menu.get(i);
System.out.println((i+1)+". "+option.getName());
}With something like that, you can get rid of the duplication of numbers. You can also use this to combine the menu printing functions into one function which takes a menu to print and then prints it. Like that, adding a new menu would be easier.
As for your Savings class, you can make use of
+= and -= to shorten balance = balance + amount to balance += amount.Code Snippets
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Logout");
//...
switch(selection){
case 1:
optionChecking();
break;
case 2:
optionSavings();
break;
case 3:
logOut();
break;
}new MenuOption(this, "Deposit"){
@Override
public void pick(){
main.depositChecking();
}
};List<MenuOption> mainMenu = new ArrayList<>();
mainMenu.add(new MenuOption(this, "Checking"){...});
mainMenu.add(new MenuOption(this, "Savings"){...});for(int i = 0; i < menu.size(); i++){
MenuOption option = menu.get(i);
System.out.println((i+1)+". "+option.getName());
}Context
StackExchange Code Review Q#147948, answer score: 2
Revisions (0)
No revisions yet.