0% found this document useful (0 votes)
23 views6 pages

Simple Java Bank Account Program

This Java code defines a Bank class with methods to open an account, display account details, deposit money, withdraw money, and search for an account. It also defines an ExBank class with a main method that allows a user to input account details for multiple customers, and interactively select options to display all accounts, search by account number, deposit to an account, withdraw from an account, and exit the program.

Uploaded by

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

Simple Java Bank Account Program

This Java code defines a Bank class with methods to open an account, display account details, deposit money, withdraw money, and search for an account. It also defines an ExBank class with a main method that allows a user to input account details for multiple customers, and interactively select options to display all accounts, search by account number, deposit to an account, withdraw from an account, and exit the program.

Uploaded by

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

import [Link].

Scanner;

class Bank

private String accno;

private String name;

private long balance;

Scanner KB = new Scanner([Link]);

//method to open an account

void openAccount() {

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

accno = [Link]();

[Link]("Enter Name: ");

name = [Link]();

[Link]("Enter Balance: ");

balance = [Link]();

//method to display account details

void showAccount() {

[Link](accno + "," + name + "," + balance);

//method to deposit money


void deposit() {

long amt;

[Link]("Enter Amount U Want to Deposit : ");

amt = [Link]();

balance = balance + amt;

//method to withdraw money

void withdrawal() {

long amt;

[Link]("Enter Amount U Want to withdraw : ");

amt = [Link]();

if (balance >= amt) {

balance = balance - amt;

} else {

[Link]("Less Balance..Transaction Failed..");

//method to search an account number

boolean search(String acn) {

if ([Link](acn)) {

showAccount();

return (true);

}
return (false);

public class ExBank extends Bank

public static void main(String arg[]) {

Scanner KB = new Scanner([Link]);

//create initial accounts

[Link]("How Many Customer U Want to Input : ");

int n = [Link]();

Bank C[] = new Bank[n];

for (int i = 0; i < [Link]; i++) {

C[i] = new Bank();

C[i].openAccount();

//run loop until menu 5 is not pressed

int ch;

do {

[Link]("Main Menu\n1. Display All\n 2. Search By Account\n 3. Deposit\n 4.


Withdrawal\n 5.E xit ");

[Link]("Ur Choice :"); ch = [Link]();

switch (ch) {

case 1:
for (int i = 0; i < [Link]; i++) {

C[i].showAccount();

break;

case 2:

[Link]("Enter Account No U Want to Search...: ");

String acn = [Link]();

boolean found = false;

for (int i = 0; i < [Link]; i++) {

found = C[i].search(acn);

if (found) {

break;

if (!found) {

[Link]("Search Failed..Account Not Exist..");

break;

case 3:

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

acn = [Link]();

found = false;

for (int i = 0; i < [Link]; i++) {


found = C[i].search(acn);

if (found) {

C[i].deposit();

break;

if (!found) {

[Link]("Search Failed..Account Not Exist..");

break;

case 4:

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

acn = [Link]();

found = false;

for (int i = 0; i < [Link]; i++) {

found = C[i].search(acn);

if (found) {

C[i].withdrawal();

break;

if (!found) {

[Link]("Search Failed..Account Not Exist..");

}
break;

case 5:

[Link]("Good Bye..");

break;

while (ch != 5);

Common questions

Powered by AI

Transforming this banking program into a web-based application would necessitate architectural changes such as implementing a client-server model for handling requests over HTTP. Introducing a database management system would replace the array with persistent storage, allowing for scalable account management. Additionally, input validation, security mechanisms, and a user interface framework would be crucial to support reliable, secure, and user-friendly access across the web .

The program employs a do-while loop for continuous user interaction with a menu that offers options for displaying accounts, searching by account number, depositing, withdrawing, and exiting. The loop condition runs until the user selects option 5 (exit). When exit is selected, the program outputs 'Good Bye..', ensuring a graceful termination of the loop and program .

The main purpose of the 'Bank' class is to manage bank account operations such as opening an account, displaying account details, depositing money, withdrawing money, and searching for an account by its number. It achieves this by defining private variables 'accno', 'name', and 'balance' to store respective account details. Methods like 'openAccount', 'showAccount', 'deposit', 'withdrawal', and 'search' encapsulate the functionality needed to perform these operations .

The code handles a failed withdrawal attempt by checking if the balance is greater than or equal to the withdrawal amount before performing the operation. If the balance is insufficient, it prints 'Less Balance..Transaction Failed..' and does not alter the balance, effectively aborting the withdrawal operation .

The 'search' method is significant as it allows checking if an account exists by comparing the entered account number with stored account numbers. In the 'ExBank' class, this method is used in several cases—to display account details, to deposit money, and to enable withdrawals—by iterating over the accounts array and invoking 'search'. If the method returns true, indicating the account is found, further actions like deposit or withdrawal are allowed .

The 'openAccount' method facilitates account creation by prompting the user for account details—account number, name, and initial balance—via the Scanner. Input values are stored in the class variables, initializing the new account. The purpose is to gather and encapsulate new account data within an entity, enabling future transactions and management through implemented methods .

Encapsulation in the 'Bank' class is achieved by declaring the account details ('accno', 'name', and 'balance') as private. This access restriction prevents direct modification of these fields from outside the class, preserving data integrity. Methods like 'deposit' and 'withdrawal' provide controlled means to alter balance, ensuring that changes are valid and consistent with expected account behavior .

Using the Scanner for input without validation poses risks such as crashes or exceptions if users enter invalid data types or unexpected inputs. For example, entering a string instead of a number for balance or deposit can result in InputMismatchException. Additionally, without validation, input errors or malicious data can compromise account operation integrity, leading to incorrect data processing .

Using an array to store bank accounts provides a straightforward mechanism for managing a fixed number of accounts. The benefit is simplicity in iteration and fixed memory allocation. However, this design choice limits scalability, as arrays have a fixed size, which is not suitable for an indefinite number of accounts. Additionally, array-based operations like search can be inefficient, as they may require scanning the entire array (O(n) time complexity).

Before executing operations like deposit and withdrawal, the program utilizes the 'search' method to verify account existence. If 'search' returns true, indicating the account number is valid, the operation proceeds. This check ensures that attempts to deposit or withdraw involve only legitimate accounts, reducing errors and maintaining data coherence .

You might also like