Account.java
import java.util.*;
import java.io.*;
public class Account implements Serializable{
private int accountId;
private String holderName;
private double balance;
private Date creationDate;
public static final String TRA_DEPOSIT = "deposit";
public static final String TRA_WITHDRAW = "withdraw";
ArrayList<Transaction> passbook = new ArrayList<Transaction>();
Account(String holderName, int accountId, double balance){
this.holderName = holderName;
this.accountId = accountId;
this.balance = balance;
this.creationDate = new Date();
}
Account(String holderName, int accountId){
this(holderName, accountId, 0);
}
public void setAccountId(int id){
this.accountId = id;
}
public int getAccountId(){
return this.accountId;
}
public void setName(String name){
this.holderName = name;
}
public String getName(){
return this.holderName;
}
public double getBalance(){
return this.balance;
}
public String toString(){
return "\nAccount No. :"+this.accountId+"\nHolder Name: "+this.holderName+"\nCurrent Balance: "+this.balance+"\n";
}
public boolean deposit(double amt){
if(amt<=0){
return false;
}
else{
this.balance += amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_DEPOSIT);
this.passbook.add(t1);
return true;
}
}
public boolean withdraw(double amt){
if(amt<=0){
return false;
}
else if(amt>balance){
return false;
}
else{
this.balance -= amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_WITHDRAW);
this.passbook.add(t1);
return true;
}
}
public class Transaction{
double amt;
String type;
Date transactionDate;
public Transaction(double amt,String type){
this.amt = amt;
this.type = type;
this.transactionDate = new Date();
}
public String toString(){
return "Amount: "+ ((this.type.equals(Account.TRA_DEPOSIT))?"+":"-") + this.amt+" \nTransaction Date: "+ transactionDate;
}
}
boolean printPassbook(){
System.out.println(this+"\n----------------------------------\n");
for(Transaction t : this.passbook){
System.out.println(t+"\n\n");
}
return true;
}
}
Bank.java
import java.util.*;
public class Bank{
String name;
int nextAccountId = 1001;
ArrayList<Account> accounts = new ArrayList<Account>();
Bank(String name){
this.name = name;
}
Account createAccount(String holderName, double balance){
Account a1 = new Account(holderName, nextAccountId++, balance);
this.accounts.add(a1);
return a1;
}
double getAccountBalance(int accountId){
Account a = findAccount(accountId);
if(a==null){
return -1;
}
else{
return a.getBalance();
}
}
boolean removeAccount(int accountId){
for(int i = 0; i < accounts.size(); i++){
if(accounts.get(i).getAccountId() == accountId){
accounts.remove(i);
return true;
}
}
return false;
}
boolean deposit(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.deposit(amt);
}
}
boolean withdraw(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.withdraw(amt);
}
}
Account findAccount(int accountId){
for(Account a: accounts){
if(a.getAccountId() == accountId){
return a;
}
}
return null;
}
boolean printPassbook(int accountId){
Account a = findAccount(accountId);
if(a==null){
return false;
}
return a.printPassbook();
}
}
TestBank.java
import java.util.*;
class TestBank{
static Bank b1;
static Scanner sc;
public static void main(String[] args){
if(!findAndLoadBank()){
createBank();
}
sc = new Scanner(System.in);
while(true){
int selection = showMenu();
if(selection==6){
System.out.println("Thank you !!");
saveBank();
break;
}
if(selection >0 && selection<6){
handleSelection(selection);
}
}
}
public static int showMenu(){
System.out.println("Select an option:\n-------------------------\n1. Create Accoun\n2. Deposit\n3. Withdraw\n4. Check Balance\n5. Print Passbook\n6. QUIT\n");
try{
int selection = Integer.parseInt(sc.nextLine());
return selection;
}catch(Exception e){
System.out.println("Invalid Selection\n");
return 0;
}
}
public static void handleSelection(int selection){
System.out.println("I am currently handling : "+ selection);
switch(selection){
case 1:
{
System.out.println("Enter your name: ");
String name = sc.nextLine();
System.out.println("Enter initial balance: ");
String balance = sc.nextLine();
try{
Account a = b1.createAccount(name, Double.parseDouble(balance));
System.out.println(a);
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 2:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter deposit amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.deposit(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Deposit Successful":"Deposit Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 3:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter withdraw amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.withdraw(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Withdraw Successful":"Withdraw Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 4:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
double balance = b1.getAccountBalance(Integer.parseInt(acno));
System.out.println("Balance is: "+ balance);
}
break;
case 5:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
b1.printPassbook(Integer.parseInt(acno));
}
break;
default:
}
}
public static void createBank(){
b1 = new Bank("SBI Alkapuri");
}
}
import java.util.*;
import java.io.*;
public class Account implements Serializable{
private int accountId;
private String holderName;
private double balance;
private Date creationDate;
public static final String TRA_DEPOSIT = "deposit";
public static final String TRA_WITHDRAW = "withdraw";
ArrayList<Transaction> passbook = new ArrayList<Transaction>();
Account(String holderName, int accountId, double balance){
this.holderName = holderName;
this.accountId = accountId;
this.balance = balance;
this.creationDate = new Date();
}
Account(String holderName, int accountId){
this(holderName, accountId, 0);
}
public void setAccountId(int id){
this.accountId = id;
}
public int getAccountId(){
return this.accountId;
}
public void setName(String name){
this.holderName = name;
}
public String getName(){
return this.holderName;
}
public double getBalance(){
return this.balance;
}
public String toString(){
return "\nAccount No. :"+this.accountId+"\nHolder Name: "+this.holderName+"\nCurrent Balance: "+this.balance+"\n";
}
public boolean deposit(double amt){
if(amt<=0){
return false;
}
else{
this.balance += amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_DEPOSIT);
this.passbook.add(t1);
return true;
}
}
public boolean withdraw(double amt){
if(amt<=0){
return false;
}
else if(amt>balance){
return false;
}
else{
this.balance -= amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_WITHDRAW);
this.passbook.add(t1);
return true;
}
}
public class Transaction{
double amt;
String type;
Date transactionDate;
public Transaction(double amt,String type){
this.amt = amt;
this.type = type;
this.transactionDate = new Date();
}
public String toString(){
return "Amount: "+ ((this.type.equals(Account.TRA_DEPOSIT))?"+":"-") + this.amt+" \nTransaction Date: "+ transactionDate;
}
}
boolean printPassbook(){
System.out.println(this+"\n----------------------------------\n");
for(Transaction t : this.passbook){
System.out.println(t+"\n\n");
}
return true;
}
}
Bank.java
import java.util.*;
public class Bank{
String name;
int nextAccountId = 1001;
ArrayList<Account> accounts = new ArrayList<Account>();
Bank(String name){
this.name = name;
}
Account createAccount(String holderName, double balance){
Account a1 = new Account(holderName, nextAccountId++, balance);
this.accounts.add(a1);
return a1;
}
double getAccountBalance(int accountId){
Account a = findAccount(accountId);
if(a==null){
return -1;
}
else{
return a.getBalance();
}
}
boolean removeAccount(int accountId){
for(int i = 0; i < accounts.size(); i++){
if(accounts.get(i).getAccountId() == accountId){
accounts.remove(i);
return true;
}
}
return false;
}
boolean deposit(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.deposit(amt);
}
}
boolean withdraw(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.withdraw(amt);
}
}
Account findAccount(int accountId){
for(Account a: accounts){
if(a.getAccountId() == accountId){
return a;
}
}
return null;
}
boolean printPassbook(int accountId){
Account a = findAccount(accountId);
if(a==null){
return false;
}
return a.printPassbook();
}
}
TestBank.java
import java.util.*;
class TestBank{
static Bank b1;
static Scanner sc;
public static void main(String[] args){
if(!findAndLoadBank()){
createBank();
}
sc = new Scanner(System.in);
while(true){
int selection = showMenu();
if(selection==6){
System.out.println("Thank you !!");
saveBank();
break;
}
if(selection >0 && selection<6){
handleSelection(selection);
}
}
}
public static int showMenu(){
System.out.println("Select an option:\n-------------------------\n1. Create Accoun\n2. Deposit\n3. Withdraw\n4. Check Balance\n5. Print Passbook\n6. QUIT\n");
try{
int selection = Integer.parseInt(sc.nextLine());
return selection;
}catch(Exception e){
System.out.println("Invalid Selection\n");
return 0;
}
}
public static void handleSelection(int selection){
System.out.println("I am currently handling : "+ selection);
switch(selection){
case 1:
{
System.out.println("Enter your name: ");
String name = sc.nextLine();
System.out.println("Enter initial balance: ");
String balance = sc.nextLine();
try{
Account a = b1.createAccount(name, Double.parseDouble(balance));
System.out.println(a);
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 2:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter deposit amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.deposit(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Deposit Successful":"Deposit Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 3:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter withdraw amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.withdraw(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Withdraw Successful":"Withdraw Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}
break;
case 4:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
double balance = b1.getAccountBalance(Integer.parseInt(acno));
System.out.println("Balance is: "+ balance);
}
break;
case 5:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
b1.printPassbook(Integer.parseInt(acno));
}
break;
default:
}
}
public static void createBank(){
b1 = new Bank("SBI Alkapuri");
}
}