Hello fellow daoist,
Yesterday, I’m learning java by doing simple project named Banking Applications, it is not using any database, just a simple console that emulates the banking work flow.
Now, I want to share on how you can make one too. I assume that you already at level where you can create new project for java. So I will bypass any formality and just explain on the code.
First, you may want to create new project for java application ( I’m using Intellij IDEA IDE ), I named my project BankingApplications.
When you created the new project, you will get a new blank Main.java that looks like this.
1 2 3 4 5 6 7 8 |
package com.company; public class Main { public static void main(String[] args) { // write your code here } } |
Next, add a new class after the public class Main, let’s call it BankAccount, in the class you need variable like : balance, previous transaction, account name, and account id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.company; public class Main { public static void main(String[] args) { // write your code here } } class BankAccount { int balance; int previousTransaction; String customerName; String customerId; // will add method down here } |
Of course in a bank application there’s always work like deposit and withdraw money from the bank, that is why inside the BankAccount class insert the deposit and withdraw method.
Don’t forget that deposit will increase your balance, while withdraw will decrease your balance. Also that for the previous transaction to know whether you deposit or withdraw too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.company; public class Main { public static void main(String[] args) { // write your code here } } class BankAccount { int balance; int previousTransaction; String accountName; String accountId; BankAccount(String cname, String cid) { accountName = cname; accountId = cid; } public void deposit(int amount) { balance = balance + amount; previousTransaction = amount; } public void withdraw(int amount) { balance = balance - amount; previousTransaction = -amount; } public void getPreviousTransaction() { if (previousTransaction > 0) { System.out.println("You deposit: " + previousTransaction); } else if (previousTransaction < 0) { System.out.println("You withdraw: " + previousTransaction); } else { System.out.println("There is no transaction record."); } } } |
In the above code, using the BankAccount method means that when I declared on new BankAccount class, I will need to insert parameters “name” and “id”. As you can see too, the parameter will be copied to variable accountName and accountId.
The 3 method beside BankAccount was “deposit”, “withdraw”, and “getPreviousTransaction” and they will be used to deposit money, withdraw money, and see what kind of transaction before.
Like I just said, when deposit money, your balance will increase with the amount of money deposited, and decrease with the amount withdrawn.
In the getPreviousTransaction method that we used, to difference on whether deposit or withdraw were executed, we just need to fill the variable previousTransaction with either a positif, a negatif, or just 0.
Positif means deposit, Negatif means withdraw, and 0 means there is no transaction ever occur.
Next, we will need to bring up the menu of the banking application, in banking application you will need (for now) 5 menus.
Menus:
a. check balance
b. deposit
c. withdraw
d. check previous transaction
e. exit program
Before we make menus, you will need input from player, the input can be obtained by using Scanner in java. The Scanner will process the input, and you can get it to check what input the user type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here } } class BankAccount { int balance; int previousTransaction; String accountName; String accountId; BankAccount(String cname, String cid) { accountName = cname; accountId = cid; } public void deposit(int amount) { balance = balance + amount; previousTransaction = amount; } public void withdraw(int amount) { balance = balance - amount; previousTransaction = -amount; } public void getPreviousTransaction() { if (previousTransaction > 0) { System.out.println("You deposit: " + previousTransaction); } else if (previousTransaction < 0) { System.out.println("You withdraw: " + previousTransaction); } else { System.out.println("There is no transaction record."); } } public void showMenu() { char option = '\0'; Scanner scanner = new Scanner(System.in); System.out.println("======================================================================================="); System.out.println("Select your choice"); System.out.println("A. Check Balance"); System.out.println("B. Deposit"); System.out.println("C. Withdraw"); System.out.println("D. Previous Transaction"); System.out.println("E. Exit"); System.out.println("======================================================================================="); do { System.out.println("-----------------------------------------------------------------------------"); System.out.println("Please enter an option:"); System.out.println("-----------------------------------------------------------------------------"); option = scanner.next().charAt(0); switch(option) { case 'a': System.out.println("----------------------------------------------------------------------"); System.out.println("Balance: " + String.format("%,d", balance)); System.out.println("----------------------------------------------------------------------"); break; case 'b': System.out.println("----------------------------------------------------------------------"); System.out.println("Please enter amount of deposit:"); System.out.println("----------------------------------------------------------------------"); int amount = scanner.nextInt(); deposit(amount); break; case 'c': System.out.println("----------------------------------------------------------------------"); System.out.println("Please enter amount of withdraw:"); System.out.println("----------------------------------------------------------------------"); int amount2 = scanner.nextInt(); withdraw(amount2); break; case 'd': System.out.println("-----------------------------------------------------------------------"); getPreviousTransaction(); System.out.println("-----------------------------------------------------------------------"); break; case 'e': System.out.println("**********************************************************************"); break; default: System.out.println("Invalid Option!Please choose again."); break; } } while(option != 'e'); System.out.println("Thank you for using our services"); } } |
As you see on the showMenu method, we used a loop with while and switch to filter what kind of response needed to show to the user.
To make this banking application to run and show it menus all you need is to create new object of BankAccount and call the showMenu method like below in the public static void main.
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { // write your code here BankAccount obj1 = new BankAccount("XYZ", "BA0001"); obj1.showMenu(); } } |
Of course, now you can run the code and simply test the banking application is working or not. But this code is still prone of errors and that is why we will fix some of the errors.
The 2 main errors in this code is, first the option input from user to choose which menu will be run. Char is different to string, therefor ‘A’ is not the same as ‘a’, so if the user inputs an ‘A’ , then it will not run the ‘a’.
The fix is using the Character function to lower case the Char variable.
1 2 |
option = scanner.next().charAt(0); option = Character.toLowerCase(option); //Lower case the char |
The second one is when you’re require to insert the amount to deposit or withdraw, any words beside number will be causing errors, that is why we will use a little loop on to check if the input is number (integer) or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// before System.out.println("----------------------------------------------------------------------"); System.out.println("Please enter amount of deposit:"); System.out.println("----------------------------------------------------------------------"); int amount = scanner.nextInt(); // after System.out.println("----------------------------------------------------------------------"); System.out.println("Please enter amount of deposit:"); System.out.println("----------------------------------------------------------------------"); while(!scanner.hasNextInt()) { System.out.println("Please enter a number:"); scanner.next(); } int amount = scanner.nextInt(); |
The “hasNextInt” in java will check whether the input is number (integer) or not, and while it is not, then it will return to before and request another input from the user.
Ps: The same thing will be needed to apply on the withdraw.
So, how about it? Do you understand a little about this simple banking application? If you had any questions about this please leave it in the comment sections.
Note: Full code will be up to github when I had the time, cheers 🙂