Skip to main content
2 of 2
Made the review less personal and fixed some linguistic mistakes.

Don't use floats or doubles for money/currency.

https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

Naming

As Timothy pointed out, methods starting with is are expected to return a boolean value.

A more readable name for isAccountExist would be isExistingAccount.

For depositing depositMoney is used, but for withdrawing withdrawel is used. For consistency it would be better to rename withdrawel to withdrawMoney.

Concurrency

The code does things that have to do with money. When writing software that relates to money (or anything with transactions), it is important to think about things like thread-safety. Java has things in it's standard library to help with this. A few examples of this are atomic variables and synchronization.

Exception handling

As you mentioned you didn't have time to implement exception handling. From a quick look at the code two types of functions which can throw an exception are used. These are the next and parse functions.

When sc.nextInt() is called and no input is given it will throw a NoSuchElementException. If the input cannot be parsed as an integer it will throw an InputMismatchException. A try/catch block can be used to handle these exceptions and prevent them from crashing your application.

Without exception handling:

int accountID = sc.nextInt();

With exception handling:

int accountID = 0;

while (true) {
  try {
    accountID = sc.nextInt();
    break;
  } catch (NoSuchElementException e) {
    sc.nextLine();
  }
}

In this example the code will try to read the input and parse it as an integer. If the input is an integer accountID will be set and the code will break from the while loop. If a NoSuchElementException or an InputMismatchException occurs the code will go to the next line for input and try again. Only the NoSuchElementException has to be caught because InputMismatchException is a subclass of NoSuchElementException.

The Integer.parseInt() function will throw a NumberFormatException if the input cannot be parsed. In a few instances Integer.parseInt() can actually be avoided by using the sc.nextInt() function like in other parts of the code.

int choice = Integer.parseInt(sc.next());

Could be replaced to avoid extra exception handling by:

int choice = sc.nextInt();
Lau G
  • 218
  • 1
  • 6