0% found this document useful (0 votes)
6 views11 pages

Java Bank Account Management System

The document contains a Java implementation of a simple bank account system, featuring classes for managing accounts and a bank. Users can create accounts, deposit and withdraw money, and check their account balances through a console interface. The program utilizes an ArrayList to store multiple accounts and provides basic validation for transactions.

Uploaded by

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

Java Bank Account Management System

The document contains a Java implementation of a simple bank account system, featuring classes for managing accounts and a bank. Users can create accounts, deposit and withdraw money, and check their account balances through a console interface. The program utilizes an ArrayList to store multiple accounts and provides basic validation for transactions.

Uploaded by

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

import [Link].

ArrayList;

import [Link];

// Account class representing a bank account

class Account {

private String accountNumber;

private String accountHolder;

private double balance;

public Account(String accountNumber, String accountHolder, double balance) {

[Link] = accountNumber;

[Link] = accountHolder;

[Link] = balance;

public String getAccountNumber() {


return accountNumber;

public String getAccountHolder() {

return accountHolder;

public double getBalance() {

return balance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

[Link]("Successfully deposited. Deposit Amount=" + amount);


} else {

[Link]("Deposit amount must be greater than zero.");

public void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

[Link]("Successfully withdrew. Withdraw Amount= " + amount);

} else {

[Link]("Insufficient balance or invalid amount.");

}
// Bank class to manage multiple accounts

class Bank {

private ArrayList<Account> accounts;

public Bank() {

accounts = new ArrayList<>();

public void createAccount(String accountNumber, String accountHolder, double initialDeposit) {

if (findAccount(accountNumber) != null) {

[Link]("Account with this number already exists.");

return;

Account newAccount = new Account(accountNumber, accountHolder, initialDeposit);

[Link](newAccount);
[Link]("Account successfully created.");

public Account findAccount(String accountNumber) {

for (Account account : accounts) {

if ([Link]().equals(accountNumber)) {

return account;

return null;

public void deposit(String accountNumber, double amount) {

Account account = findAccount(accountNumber);


if (account != null) {

[Link](amount);

} else {

[Link]("Account not found.");

public void withdraw(String accountNumber, double amount) {

Account account = findAccount(accountNumber);

if (account != null) {[Link](amount);

} else {

[Link]("Account not found.");

}
public void checkBalance(String accountNumber) {

Account account = findAccount(accountNumber);

if (account != null) {

[Link]("Account Holder: " + [Link]());

[Link]("Balance: " + [Link]());

} else {

[Link]("Account not found.");

// Main class to run the program

public class BankAccountSystem {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

Bank bank = new Bank();

while (true) {

[Link]("\n--- Bank Account System ---");

[Link]("1. Create Account");

[Link]("2. Deposit Money");

[Link]("3. Withdraw Money");

[Link]("4. Check Balance");[Link]("5. Exit");

[Link]("Choose an option: ");

int choice = [Link]();

[Link](); // Consume newline

switch (choice) {
case 1:

[Link]("Enter Account Number: ");

String accountNumber = [Link]();

[Link]("Enter Account Holder Name: ");

String accountHolder = [Link]();

[Link]("Enter Initial Deposit: ");

double initialDeposit = [Link]();

[Link](accountNumber, accountHolder, initialDeposit);

break;

case 2:

[Link]("Enter Account Number: ");

accountNumber = [Link]();
[Link]("Enter Deposit Amount: ");

double depositAmount = [Link]();

[Link](accountNumber, depositAmount);

break;

case 3:

[Link]("Enter Account Number: ");

accountNumber = [Link]();

[Link]("Enter Withdrawal Amount: ");

double withdrawalAmount = [Link]();

[Link](accountNumber, withdrawalAmount);

break;case 4:

[Link]("Enter Account Number: ");

accountNumber = [Link]();
[Link](accountNumber);

break;

case 5:

[Link]("Exiting the program. Goodbye!");

[Link]();

return;

default:

[Link]("Invalid choice. Please try again.");

Common questions

Powered by AI

To improve efficiency in managing a large number of bank accounts, the Bank class could use a HashMap instead of an ArrayList to store accounts. The HashMap can map account numbers to account objects, enabling quicker retrieval, especially when searching or accessing accounts, due to constant-time complexity for put and get operations. This would significantly enhance performance compared to the linear search required in the current implementation.

The deposit method in the Account class handles errors by checking if the deposit amount is greater than zero before adding it to the balance. If the amount is not positive, it prints a message stating that "Deposit amount must be greater than zero." This prevents any zero or negative amounts from being mistakenly added to the account balance.

The Bank class uses the findAccount method to check if an account with the given account number already exists before performing deposit or withdrawal operations. The findAccount method iterates through the list of accounts and returns the account object if a match is found. If the account is not found, the deposit and withdraw methods print "Account not found." to notify the user that the operation cannot be performed.

The import statements for java.util.ArrayList and java.util.Scanner are necessary because they allow the use of these classes defined in Java's standard library. ArrayList is used for storing and managing multiple Account objects in the Bank class, and Scanner is utilized for user input in the BankAccountSystem class. Without these imports, the classes cannot be used, leading to compilation errors.

The main method in BankAccountSystem uses a while(true) loop to continuously display a menu and accept user inputs for different operations like creating an account or depositing money. The loop only exits when the case 5 is selected, which prints a goodbye message and calls return, breaking the loop's execution, thus stopping further input handling.

Not closing the Scanner object in the BankAccountSystem class can lead to resource leaks as the underlying resource (System.in input stream) remains open. This can prevent efficient usage of system resources and can eventually lead to performance degradation in longer running programs. The program does close the Scanner when the user chooses to exit, ensuring resources are freed correctly.

Trying to withdraw an amount greater than the balance can lead to an overdraft issue. The Account class addresses this by checking if the amount is greater than zero and less than or equal to the balance before performing the withdrawal. If the condition is not met, it prints "Insufficient balance or invalid amount." to prevent the withdrawal of unauthorized funds.

Encapsulation in the Account class is achieved by using private access modifiers for its fields: accountNumber, accountHolder, and balance. Accessor methods (getters) for the account number, account holder, and balance provide controlled access to these fields. Additionally, public methods like deposit and withdraw allow controlled modification of the balance, preventing unauthorized direct access or changes.

Allowing multiple accounts with the same account number can lead to confusion and incorrect transactions, as users may inadvertently deposit or withdraw from the wrong account. The Bank class prevents this by using the findAccount method in the createAccount process to check if an account number already exists before adding a new account. If an account with the same number is found, the method prints "Account with this number already exists." and does not create a new account.

To add an overdraft feature to the Account class, new fields for overdraftLimit and overdraftAmount could be introduced. The withdraw method would then need to adjust its conditional checks to allow withdrawals that exceed the balance by the overdraft limit. This could involve deducting withdrawal amounts from both balance and overdraft, considering both as a cumulative available amount. Care must be taken to handle interest on overdrafts and proper error messaging if the overdraft limit is exceeded.

You might also like