[go: up one dir, main page]

0% found this document useful (0 votes)
46 views7 pages

Simple Banking Application Using CORE

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views7 pages

Simple Banking Application Using CORE

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Simple Banking System

# Task Description
Create a simple banking system program in Java. The program should perform the
following operations:

1. Display account information.

2. Deposit money.

3. Withdraw money.

# Task Steps
1. **Define the BankAccount Class:**

- Create a class `BankAccount` with the following attributes:

- `String accountNumber`

- `String accountHolderName`

- `double balance`

2. **Add Methods to the BankAccount Class:**

- `void displayAccountInfo()`: Print the account number, account holder name, and balance.

- `void deposit(double amount)`: Add the deposit amount to the balance.

- `void withdraw(double amount)`: Subtract the withdrawal amount from the balance if
sufficient funds are available.

3. **Main Method Implementation:**

- Create an instance of `BankAccount`.

- Set initial values for the account.

- Display account information.

- Perform deposit and withdrawal operations.

- Display updated account information.

1
Simple Banking System

# Sample Code Structure


public class BankAccount {
// Attributes
String accountNumber;
String accountHolderName;
double balance;

// Method to display account information


void displayAccountInfo() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder Name: " + accountHolderName);
System.out.println("Balance: $" + balance);
}

// Method to deposit money


void deposit(double amount) {
balance += amount;
System.out.println("Deposited: $" + amount);
}

// Method to withdraw money


void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient funds");
}
}

// Main method
public static void main(String[] args) {
// Creating an instance of BankAccount
BankAccount account = new BankAccount();

// Initializing account details


account.accountNumber = "123456789";
account.accountHolderName = "John Doe";
account.balance = 1000.00;

// Display initial account information


account.displayAccountInfo();

// Deposit money
account.deposit(500.00);

// Withdraw money
account.withdraw(200.00);

// Display updated account information


account.displayAccountInfo();
}
}

2
Simple Banking System

# Assignment Instructions :

1. Implement the `BankAccount` class with the specified attributes and methods.

2. In the `main` method, create an instance of `BankAccount`, initialize it with sample data,
and perform deposit and withdrawal operations.

3. Print the account details before and after the transactions to verify that the methods work
correctly.

4. Ensure that the program handles invalid withdrawal attempts gracefully by checking for
sufficient funds.

3
Simple Banking System

PART - 2
• Topics Covered
• Conditional statements
• Control statements
• Patterns
• Constructors
• Arrays
• OOP Principles (Encapsulation, Inheritance, Polymorphism)
• Multithreading
• Exception Handling

Task Steps
1. Extend the BankAccount Class:
• Add a constructor to initialize the account.
• Add methods to handle multiple transactions using arrays.
• Implement basic exception handling for invalid operations.
2. Add an Enhanced User Interface:
• Use control statements to build a menu-driven program.
• Implement conditional statements to navigate through the menu options.
3. Implement Multithreading:
• Simulate concurrent transactions using threads.
4. Pattern Generation:
• Add a method to display a simple pattern

SYNTAX:

import java.util.Scanner;

// Exception class for handling insufficient funds


class InsufficientFundsException extends Exception {
public InsufficientFundsException(String message) {
super(message);
}
}

// BankAccount class with added features


class BankAccount {
// Attributes
private String accountNumber;
private String accountHolderName;
private double balance;
private double[] transactions; // Array to store transactions
private int transactionCount;

// Constructor
public BankAccount(String accountNumber, String accountHolderName, double initialBalance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = initialBalance;
this.transactions = new double[100]; // Initialize transaction array
this.transactionCount = 0;
}

4
Simple Banking System

// Method to display account information


public void displayAccountInfo() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Holder Name: " + accountHolderName);
System.out.println("Balance: $" + balance);
}

// Method to deposit money


public void deposit(double amount) {
balance += amount;
transactions[transactionCount++] = amount; // Record transaction
System.out.println("Deposited: $" + amount);
}

// Method to withdraw money with exception handling


public void withdraw(double amount) throws InsufficientFundsException {
if (amount <= balance) {
balance -= amount;
transactions[transactionCount++] = -amount; // Record transaction
System.out.println("Withdrawn: $" + amount);
} else {
throw new InsufficientFundsException("Insufficient funds to withdraw: $" + amount);
}
}

// Method to display transactions


public void displayTransactions() {
System.out.println("Transactions:");
for (int i = 0; i < transactionCount; i++) {
System.out.println(transactions[i]);
}
}

// Method to display a pattern


public void displayPattern() {
System.out.println("Pattern:");
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

// Runnable class for multithreading example


class TransactionRunnable implements Runnable {
private BankAccount account;
private double amount;
private boolean deposit;

public TransactionRunnable(BankAccount account, double amount, boolean deposit) {


this.account = account;
this.amount = amount;
this.deposit = deposit;
}

@Override
public void run() {
try {
if (deposit) {
account.deposit(amount);
} else {
account.withdraw(amount);
}
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}

5
Simple Banking System
}
}

// Main class to drive the program


public class BankingSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Creating an instance of BankAccount using constructor


BankAccount account = new BankAccount("123456789", "John Doe", 1000.00);

// Menu-driven program
while (true) {
System.out.println("\nBanking System Menu:");
System.out.println("1. Display Account Information");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Display Transactions");
System.out.println("5. Display Pattern");
System.out.println("6. Simulate Concurrent Transactions");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
account.displayAccountInfo();
break;
case 2:
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 3:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
try {
account.withdraw(withdrawAmount);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
break;
case 4:
account.displayTransactions();
break;
case 5:
account.displayPattern();
break;
case 6:
// Simulate concurrent transactions using multithreading
Thread t1 = new Thread(new TransactionRunnable(account, 200, true));
Thread t2 = new Thread(new TransactionRunnable(account, 300, false));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 7:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}

6
Simple Banking System
}
}

Assignment Instructions

• Implement and Extend the BankAccount Class:

Add a constructor to initialize the account with account number, holder name, and initial
balance.

Implement methods for deposit, withdrawal (with exception handling), and displaying
transactions.

Add a method to display a simple pattern.

• Enhance the Main Method:

Create a menu-driven program using control statements (if-else, switch-case).

Use conditional statements to handle user input and navigate through different operations.

• Multithreading:

Implement a Runnable class for simulating concurrent transactions.

Create threads to perform deposit and withdrawal operations simultaneously.

• Exception Handling:

Implement custom exception handling for insufficient funds during withdrawal.

• Pattern Generation:

Add a method in the BankAccount class to display a simple star pattern.


Testing:

Test the program with various inputs to ensure all features work correctly.

Simulate concurrent transactions to observe multithreading behavior.

You might also like