Skip to content

Instantly share code, notes, and snippets.

@subratastack
Created June 25, 2021 10:59
Show Gist options
  • Save subratastack/f1c6a8f675e8a71c19f88c4df3e9720d to your computer and use it in GitHub Desktop.
Save subratastack/f1c6a8f675e8a71c19f88c4df3e9720d to your computer and use it in GitHub Desktop.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class BankApp {
static HashMap<String, Customer> customerMap = new HashMap<String, Customer>();
static Customer existingCustomer;
static Scanner in = new Scanner(System.in);
static String getMainMenu() {
String menuChoice;
System.out.println("\n\n-----------*************-----------");
System.out.println("Choose an option:");
System.out.println("1. Add New Customer");
System.out.println("2. Search Existing Customer");
System.out.println("3. Exit");
System.out.println("-----------*************-----------\n\n");
System.out.print("Enter your choice: ");
menuChoice = in.next();
return menuChoice;
}
static Customer register() {
String id;
String name;
String gender;
String location;
int accountType;
int accountNo;
System.out.println("-----------*************-----------");
System.out.print("Enter ID No.: ");
id = in.next();
System.out.print("Enter Name: ");
name = in.next();
System.out.print("Enter Gender(M/F): ");
gender = in.next();
System.out.print("Enter Location: ");
location = in.next();
System.out.print("Enter A/C Type(0 = Savings, 1 = Current): ");
accountType = in.nextInt();
System.out.print("Enter A/C No: ");
accountNo = in.nextInt();
System.out.println("-----------*************-----------");
return new Customer(id, name, gender, location, accountType, accountNo);
}
static void findCustomer() {
System.out.println("-----------*************-----------");
System.out.print("Enter Customer ID: ");
String customerID = in.next();
BankApp.existingCustomer = BankApp.customerMap.get(customerID);
if (existingCustomer != null) {
String existingCustomerMenuChoice = BankApp.existingCustomerMenu();
existingCustomerChoiceSelector(existingCustomerMenuChoice);
BankApp.mainChoiceSelector(BankApp.getMainMenu());
} else {
System.out.println("No Customer Found!!");
BankApp.mainChoiceSelector(BankApp.getMainMenu());
}
}
static void mainChoiceSelector(String mainMenuChoice) {
switch(mainMenuChoice) {
case "1":
Customer newCustomer = BankApp.register();
BankApp.customerMap.put(newCustomer.id, newCustomer);
BankApp.mainChoiceSelector(BankApp.getMainMenu());
break;
case "2":
BankApp.findCustomer();
break;
case "3":
System.exit(0);
break;
default:
break;
}
}
static String existingCustomerMenu() {
String menuChoice;
System.out.println("\n");
System.out.println("Choose an option:");
System.out.println("1. Debit");
System.out.println("2. Credit");
System.out.println("3. Account Summary");
System.out.println("3. Edit Customer");
System.out.println("4. Delete Customer");
System.out.println("5. Back to Main Menu");
System.out.println("6. Exit");
System.out.println("-----------*************-----------\n\n");
System.out.print("Enter your choice: ");
menuChoice = in.next();
return menuChoice;
}
static void existingCustomerChoiceSelector(String existingCustomerMenuChoice) {
switch (existingCustomerMenuChoice) {
case "1":
BankApp.customerDebit();
existingCustomerChoiceSelector(BankApp.existingCustomerMenu());
break;
case "2":
BankApp.customerCredit();
existingCustomerChoiceSelector(BankApp.existingCustomerMenu());
break;
case "3":
BankApp.customerAccountSummary();
existingCustomerChoiceSelector(BankApp.existingCustomerMenu());
break;
case "5":
BankApp.mainChoiceSelector(BankApp.getMainMenu());
break;
case "6":
System.exit(0);
break;
default:
break;
}
}
static void customerDebit() {
System.out.print("Enter amount to be debited: ");
Double amount = in.nextDouble();
existingCustomer.debit(amount);
System.out.println("Amount debited successfully.");
System.out.println("-----------*************-----------");
}
static void customerCredit() {
System.out.print("Enter amount to be credited: ");
Double amount = in.nextDouble();
existingCustomer.credit(amount);
System.out.println("Amount credited successfully.");
System.out.println("-----------*************-----------");
}
static void customerAccountSummary() {
existingCustomer.accountSummary();
}
public static void main(String ...args) {
BankApp.mainChoiceSelector(BankApp.getMainMenu());
}
}
class Customer {
String id;
String name;
String gender;
String location;
int accountType;
int accountNo;
ArrayList<AccountBookeeping> book;
Customer(String id, String name, String gender, String location, int accountType, int accountNo) {
this.id = id;
this.name = name;
this.gender = gender;
this.location = location;
this.accountType = accountType;
this.accountNo = accountNo;
this.book = new ArrayList<AccountBookeeping>();
}
public void print() {
System.out.println("-----------*************-----------");
System.out.println("ID: \t" + this.id);
System.out.println("Name: \t" + this.name);
System.out.println("Gender: \t" + this.gender);
System.out.println("Location: \t" + this.location);
System.out.format("A/C Type: \t%d\n", this.accountType);
System.out.format("A/C No.: \t%d\n", this.accountNo);
System.out.println("-----------*************-----------");
}
private static String createDate() {
String pattern = "dd/MM/yyyy HH:mm:ss";
DateFormat df = new SimpleDateFormat(pattern);
Date today = Calendar.getInstance().getTime();
String todayAsString = df.format(today);
return todayAsString;
}
private Double latestBalance() {
if (!this.book.isEmpty()) {
AccountBookeeping lastRecord = this.book.get(this.book.size() - 1);
return lastRecord.balance;
} else {
return 0.0;
}
}
public void debit(Double amount) {
if (latestBalance() - amount >= 0) {
this.book.add(new AccountBookeeping(createDate(), amount, 0.0, latestBalance() - amount));
} else {
System.out.println("Insufficient Balance!!");
}
}
public void credit(Double amount) {
this.book.add(new AccountBookeeping(createDate(), 0.0, amount, latestBalance() + amount));
}
public void accountSummary() {
Iterator<AccountBookeeping> bookIterator = this.book.iterator();
System.out.println("-----------*************-----------");
System.out.printf("%-20.20s %-10.10s %-10.10s %-10.10s\n", "Date", "Debit", "Credit", "Balance");
while (bookIterator.hasNext()) {
AccountBookeeping record = bookIterator.next();
System.out.printf("%-20.20s %-10.2f %-10.2f %-10.2f\n", record.date, record.debit, record.credit, record.balance);
}
System.out.println("-----------*************-----------");
}
}
class AccountBookeeping {
String date;
Double debit;
Double credit;
Double balance;
AccountBookeeping(String date, Double debit, Double credit, Double balance) {
this.date = date;
this.debit = debit;
this.credit = credit;
this.balance = balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment