Create a Banking System Console Application In Java

Chelariu Andrei
The Fresh Writes
Published in
7 min readFeb 13, 2023

Improve your OOP knowledge through a practical project in Java.

Photo by Tima Miroshnichenko from Pexels

Today we will develop a simple java application for the banking system. We will spend more time on understanding Object Oriented Programming concepts.

To begin with, we will need the java environment installed on the computer, preferably java 11. If you have not installed yet java on your local environment, please follow those steps to get ready.

Next, we will start by detailing the functionalities of the console application.

Functionalities:

  • Account creation,
  • Login, logout,
  • Displaying the last 5 transactions,
  • Deposit money,
  • Display current user information.

Concepts of Object Oriented Programming used:

  • Inheritance
  • Polymorphism
  • Encapsulation

If you want to understand more about these concepts, follow this post from my OOP basics tutorial.

Start developing your application

Create a new java project in Eclipse or Intellij.

Let’s define a new interface named as Savings Account.

public interface SavingsAccount {

void deposit(double amount, Date date);
}

I have implemented the following interface that hosts the “deposit” method, I practically call this method every time I add money to the current account. The OOP concept used is polymorphism (methods in an interface have no body).

Therefore, the implementation of the method can be found in the Customer class by overriding the method with the same name and parameters. When you override a method from the parent interface in the child class, as in the given example. The context is given by the fact that, in order to add money to the current account, we need a customer first.

So, let’s define our Customer class.

public class Customer extends Person implements SavingsAccount {

private String username;

private String password;

private double balance;

private ArrayList<String> transactions = new ArrayList<>(5);

public Customer(String firstName, String lastName, String address, String phone, String username, String password, double balance, ArrayList<String> transactions, Date date) {
super(firstName, lastName, address, phone);
this.username = username;
this.password = password;
this.balance = balance;
addTransaction(String.format("Initial deposit - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
}

private void addTransaction(String message) {

transactions.add(0, message);
if (transactions.size() > 5) {
transactions.remove(5);
transactions.trimToSize();
}
}

//Getter Setter

public ArrayList<String> getTransactions() {
return transactions;
}

@Override
public void deposit(double amount, Date date) {

balance += amount;
addTransaction(String.format(NumberFormat.getCurrencyInstance().format(amount) + " credited to your account. Balance - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
}

@Override
public String toString() {
return "Customer{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", balance=" + balance +
", transactions=" + transactions +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Customer customer = (Customer) o;

if (Double.compare(customer.getBalance(), getBalance()) != 0) return false;
if (getUsername() != null ? !getUsername().equals(customer.getUsername()) : customer.getUsername() != null)
return false;
if (getPassword() != null ? !getPassword().equals(customer.getPassword()) : customer.getPassword() != null)
return false;
return getTransactions() != null ? getTransactions().equals(customer.getTransactions()) : customer.getTransactions() == null;
}

@Override
public int hashCode() {
int result;
long temp;
result = getUsername() != null ? getUsername().hashCode() : 0;
result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
temp = Double.doubleToLongBits(getBalance());
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (getTransactions() != null ? getTransactions().hashCode() : 0);
return result;
}

The OOP concept used is inheritance because the “Customer” class receives the properties from the “Person” class. Practically, all attributes of the person class are inherited. Relationship Person-(Parent)-Customer-(Child). We need a constructor with all the attributes from the two classes and the addition of the super constructor keyword to specify the inherited attributes.

By implementing the SavingsAccount interface, we must override the deposit method in the Customer class because we will write the implementation of the method in that class.

Also, the transaction list is initialized to display the last 5 transactions. The addTransaction method is called in the constructor which the date of the changes and the transactions performed are displayed.

private void addTransaction(String message) {

transactions.add(0, message);
if (transactions.size() > 5) {
transactions.remove(5);
transactions.trimToSize();
}
}

Now let’s talk about the Person parent class.

public class Person {

private String firstName;

private String lastName;

private String address;

private String phone;

public Person() {}

public Person(String firstName, String lastName, String address, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}

//Getters Setters

@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Person person = (Person) o;

if (getFirstName() != null ? !getFirstName().equals(person.getFirstName()) : person.getFirstName() != null)
return false;
if (getLastName() != null ? !getLastName().equals(person.getLastName()) : person.getLastName() != null)
return false;
if (getAddress() != null ? !getAddress().equals(person.getAddress()) : person.getAddress() != null)
return false;
return getPhone() != null ? getPhone().equals(person.getPhone()) : person.getPhone() == null;
}

@Override
public int hashCode() {
int result = getFirstName() != null ? getFirstName().hashCode() : 0;
result = 31 * result + (getLastName() != null ? getLastName().hashCode() : 0);
result = 31 * result + (getAddress() != null ? getAddress().hashCode() : 0);
result = 31 * result + (getPhone() != null ? getPhone().hashCode() : 0);
return result;
}

In the Person class we used the concept of encapsulation, by using the “private” access modifier for each attribute. Encapsulation in Java can be defined as a mechanism by which methods that work with that data are wrapped to form a single unit. Basically, the data from the Person class is available only in that class. Not in other classes or packages.

Finally our main class, named as Bank class.

This is the main class, from where we start the application and interact with the functionality of all classes.

public class Bank {

private static double amount = 0;
Map<String, Customer> customerMap;

Bank() {
customerMap = new HashMap<String, Customer>();
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

Customer customer;
Bank bank = new Bank();
int choice;
outer:
while (true) {

System.out.println("\n-------------------");
System.out.println("BANK OF JAVA");
System.out.println("-------------------\n");
System.out.println("1. Registrar cont.");
System.out.println("2. Login.");
System.out.println("3. Exit.");
System.out.print("\nEnter your choice : ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter First Name : ");
String firstName = sc.nextLine();
System.out.print("Enter Last Name : ");
String lastName = sc.nextLine();
System.out.print("Enter Address : ");
String address = sc.nextLine();
System.out.print("Enter contact number : ");
String phone = sc.nextLine();
System.out.println("Set Username : ");
String username = sc.next();
while (bank.customerMap.containsKey(username)) {
System.out.println("Username already exists. Set again : ");
username = sc.next();
}
System.out.println("Set a password:");
String password = sc.next();
sc.nextLine();

customer = new Customer(firstName, lastName, address, phone, username, password, new Date());
bank.customerMap.put(username, customer);
break;

case 2:
System.out.println("Enter username : ");
username = sc.next();
sc.nextLine();
System.out.println("Enter password : ");
password = sc.next();
sc.nextLine();
if (bank.customerMap.containsKey(username)) {
customer = bank.customerMap.get(username);
if (customer.getPassword().equals(password)) {
while (true) {
System.out.println("\n-------------------");
System.out.println("W E L C O M E");
System.out.println("-------------------\n");
System.out.println("1. Deposit.");
System.out.println("2. Transfer.");
System.out.println("3. Last 5 transactions.");
System.out.println("4. User information.");
System.out.println("5. Log out.");
System.out.print("\nEnter your choice : ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter amount : ");
while (!sc.hasNextDouble()) {
System.out.println("Invalid amount. Enter again :");
sc.nextLine();
}
amount = sc.nextDouble();
sc.nextLine();
customer.deposit(amount, new Date());
break;

case 2:
System.out.print("Enter beneficiary username : ");
username = sc.next();
sc.nextLine();
System.out.println("Enter amount : ");
while (!sc.hasNextDouble()) {
System.out.println("Invalid amount. Enter again :");
sc.nextLine();
}
amount = sc.nextDouble();
sc.nextLine();
if (amount > 300) {
System.out.println("Transfer limit exceeded. Contact bank manager.");
break;
}
if (bank.customerMap.containsKey(username)) {
Customer payee = bank.customerMap.get(username); //Todo: check
payee.deposit(amount, new Date());
customer.withdraw(amount, new Date());
} else {
System.out.println("Username doesn't exist.");
}
break;

case 3:
for (String transactions : customer.getTransactions()) {
System.out.println(transactions);
}
break;

case 4:
System.out.println("Titularul de cont cu numele: " + customer.getFirstName());
System.out.println("Titularul de cont cu prenumele : " + customer.getLastName());
System.out.println("Titularul de cont cu numele de utilizator : " + customer.getUsername());
System.out.println("Titularul de cont cu addresa : " + customer.getAddress());
System.out.println("Titularul de cont cu numarul de telefon : " + customer.getPhone());
break;
case 5:
continue outer;
default:
System.out.println("Wrong choice !");
}
}
} else {
System.out.println("Wrong username/password.");
}
} else {
System.out.println("Wrong username/password.");
}
break;

case 3:
System.out.println("\nThank you for choosing Bank Of Java.");
System.exit(1);
break;
default:
System.out.println("Wrong choice !");
}}}}

Using the java.util library we call Scanner to read data from the keyboard. We bring the Customer object through its definition as well as Bank, you create a new object of type Bank(). Through a while we display a start menu. By using nextLine, the number added from the keyboard is read.

Below we have a new constructor that saves our map, customer data. Map.put is used to save or update customer data.

customer = new Customer(firstName, lastName, address, phone, username, password, new Date());
bank.customerMap.put(username, customer);

When connection exists we have a new menu with options. The same approach using while and the switch to call the functionality of the application.

Case 1: We add money to the current account.

Case 2: We display the last 5 transactions.

Case 3: We display the client’s data from the map in the console.

Case 4: We close the menu.

Source code here.

Final Thoughts

I hope this exemple is useful to help you get familiar with the use of OOP concepts in Java. Hence, practicing exercises is important to help you think through the logic.

Stay tuned for more about the topic!

Thanks for reading!Happy learning 😄

Do support our publication by following it

--

--