3
\$\begingroup\$

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);
         System.out.println("Savings Balance is: " + withDep.getBalance());
         mainMenu();
    }
    public void withdrawChecking(){
        System.out.print("Enter withdraw amount: ");
        double amount = input.nextDouble();
        withDep.withdraw(amount);
        System.out.println("Savings Balance is: " + withDep.getBalance());
        mainMenu();
    }
    public void accountInfoChecking(){
        System.out.println("Checking balance is: " + withDep.getBalance());
        mainMenu();

    }
    public void optionSavings(){
        menuDepWith();
        int inputoption = input.nextInt();
        switch(inputoption){
            case 1:
            savingsDeosit();
            break;

            case 2:
            savingsWithdraw();
            break;

            case 3:
            accountInfoSavings();
            break;

            case 4:
            logOut();
            break;
        }
    }
     public void savingsDeosit(){
         System.out.print("Enter deposit amount: ");
         double amount = input.nextDouble();
         withdrawDeposit.deposit(amount);
         System.out.println("Savings Balance is: " + withdrawDeposit.getBalance());
         mainMenu();
    }
    public void savingsWithdraw(){
        System.out.print("Enter withdraw amount: ");
        double amount = input.nextDouble();
        withdrawDeposit.withdraw(amount);
        System.out.println("Savings Balance is: " + withdrawDeposit.getBalance());
        mainMenu();
    }
    public void accountInfoSavings(){
        System.out.println("Savings Balance is: " + withdrawDeposit.getBalance());
        mainMenu();
    }
    public void logOut(){
        System.exit(0);
    }
    Savings withdrawDeposit = new Savings();
    Checking withDep= new Checking();
    Scanner input = new Scanner(System.in);
}

Savings class (same as checking class):

public class Savings
  {
   double balance =0;
     public void deposit(double amount){
     balance=balance+amount;
   }
     public void withdraw(double amount){
     balance=balance-amount;
   }
     public double getBalance(){
     return balance;
   }
   }
\$\endgroup\$
2
\$\begingroup\$

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.

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<MenuOption> 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.