4
\$\begingroup\$

I programmed in Java before, but I feel that I lack the "true way" of programming (OOP concepts, design, algorithm), so I started to learn all of these but I need your opinions and suggestions so I can improve my skills and to be sure that I got all the concepts right. I know that there are multiple ways of doing some things, but I am trying to learn the most efficient and the best practices ways.

Application description:

  1. A user is prompted to insert his login credentials (account number and account password).
  2. If an account number is found in the database then the password is verified (in this case I have a HashMap with the key holding the account number and the value holding the account to whom the account number belongs).
  3. After a successful login, the user has two choices:

    1. Check account balance
    2. Deposit money

InternetBankingApp.java class:

package main;

import java.util.HashMap;

import accounts.Account;
import operations.AccountOperations;
import operations.OperationFactory;
import utils.InputScanner;

public class InternetBankingApp {

    private static boolean successLogin;

    private static InputScanner input;
    private static HashMap<Integer, Account> accounts;
    private static Account userAccount;

    public static void main(String[] args) {

        accounts = new HashMap<Integer, Account>();
        successLogin = false;

        Account account1 = new Account(1, "password1", 1000.00);
        Account account2 = new Account(2, "password2", 2000.00);
        Account account3 = new Account(3, "password3", 3000.00);

        accounts.put(account1.getAccountNo(), account1);
        accounts.put(account2.getAccountNo(), account2);
        accounts.put(account3.getAccountNo(), account3);

        login();

        if(successLogin) {
            performOperation();
        }

    }

    private static void login() {
        int loginTries = 0;

        input = InputScanner.getInstance();

        System.out.println("Enter Account no.: ");      
        String userInput = input.readInput();

        System.out.println("Searching for account no. " + userInput);
        userAccount = accounts.get(Integer.parseInt(userInput));

        if(userAccount == null) {
            System.out.println("Account no. is invalid!");
        } else {
            while(true) {
                System.out.println("Please enter password: ");
                userInput = input.readInput();

                if(userAccount.getPassword().equals(userInput)) {
                    System.out.println("Login was performed successfully!");
                    successLogin = true;
                    break;
                } else {
                    System.out.println("Invalid password, please try again!");
                }

                loginTries ++;
                if(loginTries == 3) {
                    System.out.println("Maximum number of tries reached!");
                    break;
                }
            }
        }
    }

    private static void performOperation() {
        OperationFactory factory = new OperationFactory();

        boolean stop = false;

        while(!stop) {
            System.out.println("Please select one of the following posibilities: ");
            System.out.println("1. Check Account Summary");
            System.out.println("2. Deposit money in account");

            System.out.println("Your choice: ");
            String userInput = input.readInput();

            AccountOperations operation = factory.getOperation(userInput);
            operation.performOperation(userAccount);

            while(true) {
                System.out.println("Do you wish to make another operation? (Y/N)");
                userInput = input.readInput();

                if(userInput.equalsIgnoreCase("n")) {
                    stop = true;
                    break;
                } else if(userInput.equalsIgnoreCase("y")) {
                    break;
                } else {
                    System.out.println("Invalid option!");
                }
            }
        }
    }

}

Account.java class:

package accounts;

public class Account {

    private int accountNo;
    private String password;
    private double balance;

    public Account(int accountNo, String password, double balance) {
        this.accountNo = accountNo;
        this.password = password;
        this.balance = balance;
    }

    /**
     * @return the accountNo
     */
    public int getAccountNo() {
        return accountNo;
    }

    /**
     * @param accountNo the accountNo to set
     */
    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * @param balance the balance to set
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }   

}

OperationFactory.java class:

package operations;

public class OperationFactory {

    public AccountOperations getOperation(String operation) {

        if(operation.equals("1")) {
            return new AccountSummary();
        } else if(operation.equals("2")) {
            return new AccountDeposit();
        }

        return null;

    }

}

AccountOperations.java interface:

package operations;

import accounts.Account;

public interface AccountOperations {

    public void performOperation(Account account);

}

AccountSummary.java class:

package operations;

import accounts.Account;

public class AccountSummary implements AccountOperations {

    @Override
    public void performOperation(Account account) {
        System.out.println("Account current balance: " + account.getBalance());
    }

}

AccountDeposit.java class:

package operations;

import accounts.Account;
import utils.InputScanner;

public class AccountDeposit implements AccountOperations {

    @Override
    public void performOperation(Account account) {
        double balance = account.getBalance();

        System.out.println("Current balance: " + balance);
        System.out.println("Please insert the sum of money you wish to deposit in account: ");

        balance = balance + Double.parseDouble(InputScanner.getInstance().readInput());

        account.setBalance(balance);

        System.out.println("New account balance: " + account.getBalance());
    }

}

InputScanner.java class:

package utils;

import java.util.Scanner;

public class InputScanner {

    private static Scanner readInput;
    private static InputScanner input;

    private InputScanner() {
        readInput = new Scanner(System.in);
    }

    public static InputScanner getInstance() {
        if(input == null) {
            input = new InputScanner();
        }

        return input;
    }

    public String readInput() {
        String inputString = readInput.nextLine();

        return inputString;
    }

}
\$\endgroup\$
2
\$\begingroup\$

Dessign Issue

In your daily codding life just remember one thing: The Best Code is Not Code At All

You have an additional wrapper for Scanner. You will need to refactor, maintain this code in the future.

InputScanner are used in 2 places in the code, we can replace it with builtin Scanner:

Example for AccountDeposit

Scanner sc = new Scanner(System.in);
double userInputBalance = sc.NextDouble(); // this line is more shortly than initial

The same you can do in main method.

Possible Error Issue

I want to check if user input is correct, because user can write any number and this number can be negative or equal to zero.

double userInputBalance = 
            Double.parseDouble(InputScanner.getInstance().readInput());

boolean isBalanceCorrect = checkInputBalance(userInputBalance);

if (isBalanceCorrect) {
  balance += userInputBalance;  
  account.setBalance(balance);

  System.out.println("New account balance: " + account.getBalance());
} // else you can call this function again or to do something

Additional function for checking user input.

private boolean checkInputBalance(double inputBalance) {
  if (inputBalance < 0) {
    System.out.println("You can't insert negative values on your deposit");
    return false;
  } else if (inputBalance == 0) {
    System.out.println("You current balance will not change");
    return false;        
  }
}
\$\endgroup\$
  • \$\begingroup\$ Thank you very much for your comment, it really helped me! \$\endgroup\$ – Alexandru Cosmin Jun 27 '16 at 19:55

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.