This is an application created using Java which allows the user to deposit, withdraw or check the bank balance.
This project uses the basic concepts of the Java programming language. It has two classes, namely BankAccount
and BankingApplication
. The data members of the BankAccount class are as follows:
int balance; int previousTransaction; String customerName; String customerId;
A constructor is created to initialize the data members as follows:
BankAccount(String cname, String cid) { customerName = cname; customerId = cid; }
A function to deposit a certain amount is created as follows:
void deposit(int amount) { if(amount != 0) { balance = balance + amount; previousTransaction = amount; } }
A function to withdraw a certain amount is created as follows:
void withdraw(int amount) { if(amount != 0) { balance = balance - amount; previousTransaction = -amount; } }
A function that lets the user check the previous transaction is created as follows:
void getPreviousTransaction() { if(previousTransaction > 0) System.out.println("Deposited: " + previousTransaction); else if(previousTransaction < 0) System.out.println("Withdrawn: " + Math.abs(previousTransaction)); else System.out.println("No transaction ocurred."); }
A function is created which shows the menu to the user using a switch-case and a while()
which runs infinitely unless the user wants to exit. It is as follows:
void showMenu() { char option = '\0'; System.out.println("Welcome " + customerName); System.out.println("Your ID is " + customerId); System.out.println(); System.out.println("A. Check Balance"); System.out.println("B. Deposit"); System.out.println("C. Withdraw"); System.out.println("D. Check Previous Transaction"); System.out.println("E. Exit"); System.out.println(); System.out.println("======================================================================================================================="); System.out.println(); do { System.out.println("Enter an option:"); option = sc.next().charAt(0); switch(option) { case 'A': System.out.println(); System.out.println("--------------------------------------------------"); System.out.println(); System.out.println("Balance = " + balance); System.out.println(); System.out.println("--------------------------------------------------"); break; case 'B': System.out.println(); System.out.println("--------------------------------------------------"); System.out.println("Enter an amount to deposit:"); int amount = sc.nextInt(); deposit(amount); System.out.println(); System.out.println("--------------------------------------------------"); break; case 'C': System.out.println(); System.out.println("--------------------------------------------------"); System.out.println("Enter an amount to withdraw:"); int amount2 = sc.nextInt(); withdraw(amount2); System.out.println(); System.out.println("--------------------------------------------------"); break; case 'D': System.out.println(); System.out.println("--------------------------------------------------"); getPreviousTransaction(); System.out.println(); System.out.println("--------------------------------------------------"); break; case 'E': System.out.println("***************************************************************************************"); break; default: System.out.println("Invalid Option!! Please enter again."); } } while(option != 'E'); System.out.println(); System.out.println("Thank you for using our services!"); System.out.println(); System.out.println("======================================================================================================================="); }
That ends the BankAcount
class. Next, we start with the BankingApplication
class. We start with the main()
method. We create an object of the Scanner
class as well as the BankAcount
class. We input the name and ID of the user using the Scanner
object. Next, we call the showMenu()
function using the object of the BankAcount
class.
import java.util.Scanner; public class BankingApplication { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the name: "); String name = sc.nextLine(); System.out.print("Enter the ID: "); String id = sc.nextLine(); BankAccount obj = new BankAccount(name, id); obj.showMenu(); } }
Submitted by Arya Bhattacharyya (xxEasterGrymm)
Download packets of source code on Coders Packet
Comments