Simple Banking System Using Core Java


Introduction


The Simple Banking System will manage customer bank accounts, including features to create accounts, deposit money, withdraw money, and view account details. This project covers Java fundamentals like classes, inheritance, and exception handling.


Part 1: Requirement Gathering and Feature Discussion


Requirements:

  1. Create Account: Create a new bank account with customer details and initial deposit.
  2. Deposit Money: Deposit money into an existing account.
  3. Withdraw Money: Withdraw money from an existing account.
  4. View Account: Display details of an account.


Part 2: Design Discussion


Design Components:

  1. Account Class: Represents a bank account.
  2. SavingsAccount and CurrentAccount Classes: Extend Account class for specific account types.
  3. Bank Class: Manages the collection of accounts.
  4. BankingSystem Class: Main class to interact with the user.


Part 3: Code Implementation


1. Account Class

public class Account {
    private String accountNumber;
    private String customerName;
    protected double balance;

    public Account(String accountNumber, String customerName, double balance) {
        this.accountNumber = accountNumber;
        this.customerName = customerName;
        this.balance = balance;
    }

    // Getters and Setters
    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        } else {
            throw new IllegalArgumentException("Deposit amount must be positive.");
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        } else {
            throw new IllegalArgumentException("Invalid withdraw amount.");
        }
    }

    @Override
    public String toString() {
        return "Account [AccountNumber=" + accountNumber + ", CustomerName=" + customerName + ", Balance=" + balance + "]";
    }
}

2. SavingsAccount and CurrentAccount Classes

public class SavingsAccount extends Account {
    private double interestRate;

    public SavingsAccount(String accountNumber, String customerName, double balance, double interestRate) {
        super(accountNumber, customerName, balance);
        this.interestRate = interestRate;
    }

    public double getInterestRate() {
        return interestRate;
    }

    public void setInterestRate(double interestRate) {
        this.interestRate = interestRate;
    }

    public void addInterest() {
        balance += balance * (interestRate / 100);
    }

    @Override
    public String toString() {
        return "SavingsAccount [AccountNumber=" + getAccountNumber() + ", CustomerName=" + getCustomerName() + ", Balance=" + getBalance() + ", InterestRate=" + interestRate + "]";
    }
}

public class CurrentAccount extends Account {
    private double overdraftLimit;

    public CurrentAccount(String accountNumber, String customerName, double balance, double overdraftLimit) {
        super(accountNumber, customerName, balance);
        this.overdraftLimit = overdraftLimit;
    }

    public double getOverdraftLimit() {
        return overdraftLimit;
    }

    public void setOverdraftLimit(double overdraftLimit) {
        this.overdraftLimit = overdraftLimit;
    }

    @Override
    public void withdraw(double amount) {
        if (amount > 0 && amount <= (balance + overdraftLimit)) {
            balance -= amount;
        } else {
            throw new IllegalArgumentException("Invalid withdraw amount.");
        }
    }

    @Override
    public String toString() {
        return "CurrentAccount [AccountNumber=" + getAccountNumber() + ", CustomerName=" + getCustomerName() + ", Balance=" + getBalance() + ", OverdraftLimit=" + overdraftLimit + "]";
    }
}

3. Bank Class

import java.util.ArrayList;
import java.util.List;

public class Bank {
    private List<Account> accounts = new ArrayList<>();

    public void createSavingsAccount(String accountNumber, String customerName, double initialDeposit, double interestRate) {
        Account account = new SavingsAccount(accountNumber, customerName, initialDeposit, interestRate);
        accounts.add(account);
    }

    public void createCurrentAccount(String accountNumber, String customerName, double initialDeposit, double overdraftLimit) {
        Account account = new CurrentAccount(accountNumber, customerName, initialDeposit, overdraftLimit);
        accounts.add(account);
    }

    public Account findAccount(String accountNumber) {
        for (Account account : accounts) {
            if (account.getAccountNumber().equals(accountNumber)) {
                return account;
            }
        }
        return null;
    }

    public List<Account> viewAccounts() {
        return new ArrayList<>(accounts);
    }
}

4. BankingSystem Class

import java.util.Scanner;

public class BankingSystem {
    private static Bank bank = new Bank();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            displayMenu();
            handleUserInput();
        }
    }

    private static void displayMenu() {
        System.out.println("\nBanking System");
        System.out.println("1. Create Savings Account");
        System.out.println("2. Create Current Account");
        System.out.println("3. Deposit Money");
        System.out.println("4. Withdraw Money");
        System.out.println("5. View Account");
        System.out.println("6. Exit");
        System.out.print("Enter your choice: ");
    }

    private static void handleUserInput() {
        int choice = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        switch (choice) {
            case 1:
                createSavingsAccount();
                break;
            case 2:
                createCurrentAccount();
                break;
            case 3:
                depositMoney();
                break;
            case 4:
                withdrawMoney();
                break;
            case 5:
                viewAccount();
                break;
            case 6:
                System.exit(0);
                break;
            default:
                System.out.println("Invalid choice. Please try again.");
        }
    }

    private static void createSavingsAccount() {
        System.out.print("Enter Account Number: ");
        String accountNumber = scanner.nextLine();
        System.out.print("Enter Customer Name: ");
        String customerName = scanner.nextLine();
        System.out.print("Enter Initial Deposit: ");
        double initialDeposit = scanner.nextDouble();
        System.out.print("Enter Interest Rate: ");
        double interestRate = scanner.nextDouble();

        bank.createSavingsAccount(accountNumber, customerName, initialDeposit, interestRate);
        System.out.println("Savings account created successfully.");
    }

    private static void createCurrentAccount() {
        System.out.print("Enter Account Number: ");
        String accountNumber = scanner.nextLine();
        System.out.print("Enter Customer Name: ");
        String customerName = scanner.nextLine();
        System.out.print("Enter Initial Deposit: ");
        double initialDeposit = scanner.nextDouble();
        System.out.print("Enter Overdraft Limit: ");
        double overdraftLimit = scanner.nextDouble();

        bank.createCurrentAccount(accountNumber, customerName, initialDeposit, overdraftLimit);
        System.out.println("Current account created successfully.");
    }

    private static void depositMoney() {
        System.out.print("Enter Account Number: ");
        String accountNumber = scanner.nextLine();
        System.out.print("Enter Amount to Deposit: ");
        double amount = scanner.nextDouble();

        Account account = bank.findAccount(accountNumber);
        if (account != null) {
            account.deposit(amount);
            System.out.println("Deposit successful. New balance: " + account.getBalance());
        } else {
            System.out.println("Account not found.");
        }
    }

    private static void withdrawMoney() {
        System.out.print("Enter Account Number: ");
        String accountNumber = scanner.nextLine();
        System.out.print("Enter Amount to Withdraw: ");
        double amount = scanner.nextDouble();

        Account account = bank.findAccount(accountNumber);
        if (account != null) {
            try {
                account.withdraw(amount);
                System.out.println("Withdraw successful. New balance: " + account.getBalance());
            } catch (IllegalArgumentException e) {
                System.out.println(e.getMessage());
            }
        } else {
            System.out.println("Account not found.");
        }
    }

    private static void viewAccount() {
        System.out.print("Enter Account Number: ");
        String accountNumber = scanner.nextLine();

        Account account = bank.findAccount(accountNumber);
        if (account != null) {
            System.out.println(account);
        } else {
            System.out.println("Account not found.");
        }
    }
}


Part 4: Testing Plan


  1. Manual Testing:

    • Run the program and manually test each feature (create accounts, deposit, withdraw, view accounts).
    • Verify that the system behaves as expected and handles edge cases.

  2. Unit Testing:

    • Write unit tests for the Bank class to ensure each method works correctly.
    • Use a testing framework like JUnit for structured testing.


Example Unit Test


import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class BankTest {
    private Bank bank;

    @BeforeEach
    public void setUp() {
        bank = new Bank();
    }

    @Test
    public void testCreateAndFindAccount() {
        bank.createSavingsAccount("123", "Customer1", 1000, 1.5);
        Account account = bank.findAccount("123");
        assertNotNull(account);
        assertEquals("Customer1", account.getCustomerName());
    }

    @Test
    public void testDepositMoney() {
        bank.createSavingsAccount("123", "Customer1", 1000, 1.5);
        Account account = bank.findAccount("123");
        account.deposit(500);
        assertEquals(1500, account.getBalance());
    }

    @Test
    public void testWithdrawMoney() {
        bank.createSavingsAccount("123", "Customer1", 1000, 1.5);
        Account account = bank.findAccount("123");
        account.withdraw(500);
        assertEquals(500, account.getBalance());
    }

    @Test
    public void testViewAccounts() {
        bank.createSavingsAccount("123", "Customer1", 1000, 1.5);
        bank.createCurrentAccount("456", "Customer2", 2000, 500);
        assertEquals(2, bank.viewAccounts().size());
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *