Structure of the Problem Requirements
In this project we will implement the code of Banking system in Java. The project fulfill all the necessary requirements for banking system and provide a good practice for beginners. The project consist of 5 classes include one main class. Here is the source code of this project which help you in better understanding.
Source Code
Balance.Java
package LEP.uzair.bms.bank;
import java.util.Date;
import java.util.Scanner;
public class Balance {
private double balance;
private Date date;
private long accountNum;
Scanner sc = new Scanner(System.in);
public Balance(long aNo, double money, Date aDate) {
accountNum = aNo;
balance = money;
date = (Date) aDate.clone();
System.out.println("New account created with account no :" + accountNum);
System.out.println("Opening balance rs. " + balance);
System.out.println("Account created on " + date.toString());
}
public Balance(Balance b) {
balance = b.balance;
date = b.date;
accountNum = b.accountNum;
}
public Balance() {
}
public long getAccountNum() {
return accountNum;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String toString() {
return "A/C no.: " + accountNum + "\nCurrent balance: " + balance
+ "\nLast date of update: " + date;
}
}
Bank.Java
package LEP.uzair.bms.bank;
import java.util.ArrayList;
import java.util.Iterator;
public class Bank {
private ArrayList<Balance> balanceList;
Iterator<Balance> itr;
public Bank() {
balanceList = new ArrayList<Balance>();
}
public void newAccount(Balance e) {
balanceList.add(e);
}
public Balance searchAccount(long accountNum) {
itr = balanceList.iterator();
while (itr.hasNext()) {
Balance b = new Balance(itr.next());
if (b.getAccountNum() == accountNum)
return b;
}
return null;
}
public long getBalanceListSize() {
return balanceList.size();
}
}
Transaction.Java
package LEP.uzair.bms.bank;
import java.util.Date;
import java.util.Scanner;
public class Transaction {
private String transactionType;
private double amount;
private long accountNum;
private Date date;
private Bank b;
private Balance balance = new Balance();
Scanner sc = new Scanner(System.in);
public Transaction() {
b = new Bank();
}
public void transaction(long accountNum, String transactionType,
double amount) {
this.accountNum= accountNum;
this.transactionType = transactionType;
this.amount = amount;
date = new Date();
operation();
}
private void operation() {
if (transactionType.equalsIgnoreCase("Opening")) {
if (amount < 0) {
System.out.println("Opening balance cannot be less than zero.");
return;
}
balance = new Balance(b.getBalanceListSize() + 1, amount, date);
b.newAccount(balance);
}
else if (transactionType.equalsIgnoreCase("withdraw")) {
balance = b.searchAccount(accountNum);
if (balance == null) {
System.out.println("Account not found");
return;
}
if (balance.getBalance() < amount) {
System.out.println("Insufficient Balance.");
return;
}
System.out.println("Balance before transaction:");
System.out.println(balance.toString());
balance.setBalance(balance.getBalance() - amount);
System.out.println("Balance after transaction:\n"
+ balance.toString());
}
else if (transactionType.equalsIgnoreCase("deposit")) {
balance = b.searchAccount(accountNum);
if (balance == null) {
System.out.println("Account not found");
return;
}
System.out.println("Balance before transaction:");
System.out.println(balance.toString());
balance.setBalance(balance.getBalance() + amount);
System.out.println("Balance after transaction:\n"
+ balance.toString());
}
else if (transactionType.equalsIgnoreCase("showInfo")) {
balance = b.searchAccount(accountNum);
if (balance == null) {
System.out.println("Account not found");
return;
}
System.out.println(balance.toString());
}
else {
System.out.println("Invalid option");
return;
}
}
}
Start.Java
package LEP.uzair.bms.main;
import java.util.Scanner;
import LEP.uzair.bms.bank.Transaction;
public class Start {
int count=0;
public void StartGame(){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
char mainYesOrNo = 'Y';
while (mainYesOrNo =='Y'){
System.out.print("\t\t ================================\n");
System.out.print("\t\t Welcome To Bank Managment System \n");
System.out.print("\t\t \n");
if(count<1){
count=count+1;
}else{
System.out.print("\t\t Again \n");
}
System.out.print("\t\t ================================\n");
System.out.print("\tQ:What do you want to do?\n\n");
if(count<2){
System.out.print("\t\t1 : Start\n\n");
count=count+1;
}else{
System.out.print("\t\t1 : Start Again\n\n");
}
System.out.print("\t\t2 : Exit\n\n");
System.out.println("\t\t\tYou Select : ");
int switchChoice = input.nextInt();
switch (switchChoice)
{
case 1: {
start();
break;
}
case 2: {
return;
}
default: {
System.out.println("Invalid Selection");
error();
break;
}
}
System.out.println("\n\tDo u want to run your Program Again \n\t\t\tY = yes\n\t\t\tN = No\n");
System.out.println("You Select : ");
mainYesOrNo =(input.next()).charAt(0);
if(Character.isLowerCase(mainYesOrNo )){
mainYesOrNo =Character.toUpperCase(mainYesOrNo );
}//End If
}//End While
}
private void start() {
String choice, ch, operation;
Transaction transac = new Transaction();
Scanner sc = new Scanner(System.in);
double amount;
long accountNo;
do {
System.out.println("1. New account");
System.out.println("2. Transaction");
System.out.println("3. View Account Information");
System.out.println("q. Exit");
System.out.println("choice:");
choice = sc.next();
switch (choice) {
case "1":
double openingBalance;
System.out.println("Enter the opening balance :");
openingBalance = sc.nextDouble();
transac.transaction(0, "Opening", openingBalance);
break;
case "2":
System.out.println("a. Deposit");
System.out.println("b. Withdraw");
ch = sc.next();
if (ch.equalsIgnoreCase("a"))
operation = "Deposit";
else if (ch.equalsIgnoreCase("b"))
operation = "Withdraw";
else {
operation = "Invalid option";
}
System.out.println("Account Number:");
accountNo = sc.nextLong();
System.out.println("Amount:");
amount = sc.nextDouble();
transac.transaction(accountNo, operation, amount);
break;
case "3":
System.out.println("Account Number:");
accountNo = sc.nextLong();
operation = "showInfo";
transac.transaction(accountNo, operation, 0);
break;
case "q":
System.out.println("Thank you!");
return;
// choice="q";
default:
error();
}
} while (choice != "q");
sc.close();
}
public static void error() {
System.out.print("\t\t **************************\n");
System.out.print("\t\t |--------> ERROR <--------| \n");
System.out.print("\t\t **************************\n");
System.out.print("\t\t You Select some thin wrong\n");
System.out.print("\t\t OR\n");
System.out.print("\t\t There may be some othere Problem\n");
System.out.print("\t\t It is better for you to try again...!\n");
System.out.print("\t\t **************************\n");
}}
Main.Java
package LEP.uzair.bms.main;
public class BankMain {
public static void main(String[] args) {
Start objectStart = new Start();
objectStart.StartGame();
}
}
Output of the Program
Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.
0 comments:
Post a Comment