Menu
Question Index
...


Write a program to implement the bank and savings account framework. The interfaces which need to be implemented are given and the methods have to be defined such that the required functionality works.

Errors to be thrown if any of the operation fails.

1. Invalid Aadhar Id, should be 16 characters - if the aadhar id passed is not 16 characters.
2. Duplicate Aadhar Id - if the same aadhar id is used for two accounts.
3. Invalid Account Number - if there is not account with the given account number.
4. Invalid Account Name - if the account number is there, but the passed name is not valid.
5. Insufficient Funds - if the amount withdrawn is greater than the balance.

class SavingsAccount

{    public static void main(String s[])
    {
        System.out.println("----------CREATING BANK----------------");
        IBank hdfcBank = createBank("HDFC220013", "SB1230001");
        System.out.println("Creating a bank with IFSC Code as HDFC220013 and Account Number Prefix as SB1230001");
        System.out.println("Created Bank IFSC Code is : " + hdfcBank.getIFSCCode());
        System.out.println("----------CREATING NARAYANA ACCOUNT----------------");
        IAccount narayana = hdfcBank.createAccount("Narayana", "9921131310812134");
        System.out.println("Creating account for Narayana with Aadhar Id 992113131081213");
        System.out.println("Account Number is : " + narayana.getAccountNumber());
        System.out.println("Account Name is : " + narayana.getAccountName());
        System.out.println("---------DEPOSITING 3000 INTO NARAYANA ACCOUNT -----------------");
        ITransaction narayanaDeposit1 = hdfcBank.deposit(narayana.getAccountNumber(), narayana.getAccountName(), 3000);
        System.out.println("Depositing 3000 into Narayana's account.");
        System.out.println("Balance before : " + narayanaDeposit1.getBalanceBefore());
        System.out.println("Deposit amount : " + narayanaDeposit1.getAmount());
        System.out.println("Balance after : " + narayanaDeposit1.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------DEPOSITING 1575 MORE INTO NARAYANA ACCOUNT -----------------");
        ITransaction narayanaDeposit2 = hdfcBank.deposit(narayana.getAccountNumber(), narayana.getAccountName(), 1575);
        System.out.println("Depositing 1575 into Narayana's account.");
        System.out.println("Balance before : " + narayanaDeposit2.getBalanceBefore());
        System.out.println("Deposit amount : " + narayanaDeposit2.getAmount());
        System.out.println("Balance after : " + narayanaDeposit2.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------DEPOSITING 2000 AMOUNT INTO NARAYANA ACCOUNT, ACCOUNT NUMBER AS SB23001134 TO SEE INVALID ACCOUNT NUMBER ERROR -----------------");
        ITransaction narayanaDeposit3 = hdfcBank.deposit("SB23001134", narayana.getAccountName(), 2000);
        System.out.println("Transaction is null : " + narayanaDeposit3);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Account Number is : " + narayana.getAccountNumber());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------DEPOSITING 4000 AMOUNT INTO NARAYANA ACCOUNT,ACCOUNT NAME AS Narasimha TO SEE INVALID ACCOUNT NAME ERROR -----------------");
        ITransaction narayanaDeposit4 = hdfcBank.deposit(narayana.getAccountNumber(), "Narasimha", 400);
        System.out.println("Transaction is null : " + narayanaDeposit4);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW 275 FROM NARAYANA ACCOUNT -----------------");
        ITransaction narayanaWithdraw1 = hdfcBank.withdraw(narayana.getAccountNumber(), narayana.getAccountName(), 275);
        System.out.println("Withdraw 275 from Narayana's account.");
        System.out.println("Balance before : " + narayanaWithdraw1.getBalanceBefore());
        System.out.println("Withdraw amount : " + narayanaWithdraw1.getAmount());
        System.out.println("Balance after : " + narayanaWithdraw1.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW EXCESS AMOUNT 50000 FROM NARAYANA ACCOUNT TO SEE INSUFFICIENT FUNDS ERROR -----------------");
        ITransaction narayanaWithdraw2 = hdfcBank.withdraw(narayana.getAccountNumber(), narayana.getAccountName(), 50000);
        System.out.println("Transaction is null : " + narayanaWithdraw2);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Available Balance is : " + narayana.getBalance());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW 6000 AMOUNT FROM NARAYANA ACCOUNT,ACCOUNT NUMBER AS SB23001100 TO SEE INVALID ACCOUNT NUMBER ERROR -----------------");
        ITransaction narayanaWithdraw3 = hdfcBank.withdraw("SB23001100", narayana.getAccountName(), 2000);
        System.out.println("Transaction is null : " + narayanaWithdraw3);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Account Number is : " + narayana.getAccountNumber());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW 9000 AMOUNT INTO NARAYANA ACCOUNT, ACCOUNT NAME AS Rakesh TO SEE INVALID ACCOUNT NAME ERROR -----------------");
        ITransaction narayanaWithdraw4 = hdfcBank.withdraw(narayana.getAccountNumber(), "Rakesh", 9000);
        System.out.println("Transaction is null : " + narayanaWithdraw4);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Account Name is : " + narayana.getAccountName());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("----------CREATING KIRAN ACCOUNT----------------");
        IAccount kiran = hdfcBank.createAccount("Kiran Kumar", "1234678681612418");
        System.out.println("Creating account for Kiran with Aadhar Id 1234678681612");
        System.out.println("Account Number is : " + kiran.getAccountNumber());
        System.out.println("Account Name is : " + kiran.getAccountName());
        System.out.println("---------DEPOSITING 2510 INTO KIRAN ACCOUNT -----------------");
        ITransaction kiranDeposit1 = hdfcBank.deposit(kiran.getAccountNumber(), kiran.getAccountName(), 2510);
        System.out.println("Depositing 2510 into Kiran account.");
        System.out.println("Balance before : " + kiranDeposit1.getBalanceBefore());
        System.out.println("Deposit amount : " + kiranDeposit1.getAmount());
        System.out.println("Balance after : " + kiranDeposit1.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------DEPOSITING 1000 MORE INTO KIRAN ACCOUNT -----------------");
        ITransaction kiranDeposit2 = hdfcBank.deposit(kiran.getAccountNumber(), kiran.getAccountName(), 1000);
        System.out.println("Depositing 1000 into Kiran's account.");
        System.out.println("Balance before : " + kiranDeposit2.getBalanceBefore());
        System.out.println("Deposit amount : " + kiranDeposit2.getAmount());
        System.out.println("Balance after : " + kiranDeposit2.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW 400 FROM KIRAN ACCOUNT -----------------");
        ITransaction kiranWithdraw1 = hdfcBank.withdraw(kiran.getAccountNumber(), kiran.getAccountName(), 400);
        System.out.println("Withdraw 400 from Kiran's account.");
        System.out.println("Balance before : " + kiranWithdraw1.getBalanceBefore());
        System.out.println("Withdraw amount : " + kiranWithdraw1.getAmount());
        System.out.println("Balance after : " + kiranWithdraw1.getBalanceAfter());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW EXCESS AMOUNT 38000 FROM KIRAN ACCOUNT TO SEE INSUFFICIENT FUNDS ERROR -----------------");
        ITransaction kiranWithdraw2 = hdfcBank.withdraw(kiran.getAccountNumber(), kiran.getAccountName(), 38000);
        System.out.println("Transaction is null : " + kiranWithdraw2);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("---------WITHDRAW 2500 AMOUNT FROM KIRAN ACCOUNT, ACCOUNT NUMBER AS SB200012334 TO SEE INVALID ACCOUNT NUMBER ERROR -----------------");
        ITransaction kiranWithdraw3 = hdfcBank.withdraw("SB200012334", narayana.getAccountName(), 2500);
        System.out.println("Transaction is null : " + kiranWithdraw3);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Account Number is : " + kiran.getAccountNumber());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("---------WITHDRAW 500 AMOUNT FROM KIRAN ACCOUNT, ACCOUNT NUMBER AS Krishna TO SEE INVALID ACCOUNT NAME ERROR -----------------");
        ITransaction kiranWithdraw4 = hdfcBank.withdraw(narayana.getAccountNumber(), "Krishna", 500);
        System.out.println("Transaction is null : " + kiranWithdraw4);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("Account Name is : " + kiran.getAccountName());
        System.out.println("Total HDFC Bank Balance : " + hdfcBank.getAllAccountsTotalBalance());
        System.out.println("----------CREATING MAHESH ACCOUNT WITH INVALID AADHAR ID AS 01044537467 TO SEE INVALID AADHAR Id, SHOULD BE 16 CHARACTERS ERROR----------------");
        IAccount mahesh1 = hdfcBank.createAccount("Mahesh", "01044537467");
        System.out.println("Account is null : " + mahesh1);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("----------CREATING MAHESH ACCOUNT WITH DUPLICATE AADHAR ID AS 1234678681612418 TO SEE DUPLICATE AADHAR ID ERROR----------------");
        IAccount mahesh2 = hdfcBank.createAccount("Mahesh", "1234678681612418");
        System.out.println("Account is null : " + mahesh2);
        System.out.println("Last Error is : " + hdfcBank.getLastError());
        System.out.println("------------NARAYANA TRANSACTIONS---------------");
        for (ITransaction transaction : narayana.getTransactions()) {
        System.out.println(transaction);
        }
        System.out.println("------------KIRAN TRANSACTIONS---------------");
        for (ITransaction transaction : kiran.getTransactions()) {
        System.out.println(transaction);
        }

    }


public static IBank createBank(String ifscCode, String accountNumberPrefix) {
//Write code here to create bank.
}
}
interface IBank {

IAccount createAccount(String name, String aadharId);

String getIFSCCode();

ITransaction deposit(String accountNumber, String accountName, double amount);

ITransaction withdraw(String accountNumber, String accountName, double amount);

double getBalance(String accountNumber);

double getAllAccountsTotalBalance();

List<? extends IAccount> getAccounts();

String getLastError();
}

interface IAccount {

String getAccountName();

String getAccountNumber();

double getBalance();

List<? extends ITransaction> getTransactions();
}

interface ITransaction {

boolean isCredit();

double getAmount();

double getBalanceBefore();

double getBalanceAfter();
}

//Define the classes to implement the interfaces IBank, IAccount and ITransaction

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App