forked from IamBisrutPyne/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccountManagement.java
More file actions
168 lines (146 loc) · 6.07 KB
/
BankAccountManagement.java
File metadata and controls
168 lines (146 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.Scanner;
/**
* Program Title: Bank Account Management System (OOP)
* Author: [Sreenivasulu-03]
* Date: 2025-10-05
*
* Description: Demonstrates core OOP concepts (encapsulation, methods, constructor)
* by simulating a basic bank account with deposit, withdrawal, and balance checking
* functionality using a simple menu-driven interface.
* Time Complexity: O(1) for all operations.
* Space Complexity: O(1) for storing account details.
*/
class BankAccount {
private String accountNumber;
private String accountHolderName;
private double balance;
/**
* Constructor to initialize account details.
* @param accountNumber The unique account number.
* @param accountHolderName The name of the account holder.
* @param initialBalance The starting balance.
*/
public BankAccount(String accountNumber, String accountHolderName, double initialBalance) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = initialBalance;
}
/**
* Deposits the specified amount into the account.
* @param amount The amount to deposit.
*/
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Successfully deposited: ₹" + amount);
}
else {
System.out.println("Deposit amount must be positive!");
}
}
/**
* Withdraws the specified amount from the account.
* @param amount The amount to withdraw.
*/
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Successfully withdrew: ₹" + amount);
}
else if (amount > balance) {
System.out.println("Insufficient balance! Current balance: ₹" + balance);
}
else {
System.out.println("Invalid withdrawal amount!");
}
}
/**
* Prints the current balance to the console.
*/
public void checkBalance() {
System.out.println("Current balance: ₹" + balance);
}
/**
* Prints all account details to the console.
*/
public void displayAccountDetails() {
System.out.println("\n--- Account Details ---");
System.out.println("Account Holder: " + accountHolderName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: ₹" + balance);
System.out.println("-----------------------");
}
}
public class BankAccountManagement {
public static void main(String[] args) {
// Use try-with-resources to ensure the Scanner is safely closed.
try (Scanner sc = new Scanner(System.in)) {
// Take initial account details from user
System.out.print("Enter Account Holder Name: ");
String name = sc.nextLine();
System.out.print("Enter Account Number: ");
String number = sc.nextLine();
System.out.print("Enter Initial Balance: ₹");
// Check for valid number input
while (!sc.hasNextDouble()) {
System.out.print("Invalid input. Enter a numeric balance: ₹");
sc.next();
}
double balance = sc.nextDouble();
BankAccount account = new BankAccount(number, name, balance);
int choice = 0;
// Menu-driven system
do {
System.out.println("\n===== Bank Menu =====");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Check Balance");
System.out.println("4. Display Account Details");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
if (sc.hasNextInt()) {
choice = sc.nextInt();
} else {
System.out.println("Invalid input! Please enter a number.");
sc.next(); // Consume the invalid token
continue; // Skip the rest of the loop and show menu again
}
switch (choice) {
case 1:
System.out.print("Enter amount to deposit: ₹");
if (sc.hasNextDouble()) {
double depositAmount = sc.nextDouble();
account.deposit(depositAmount);
} else {
System.out.println("Invalid input. Deposit failed.");
sc.next(); // Consume invalid input
}
break;
case 2:
System.out.print("Enter amount to withdraw: ₹");
if (sc.hasNextDouble()) {
double withdrawAmount = sc.nextDouble();
account.withdraw(withdrawAmount);
} else {
System.out.println("Invalid input. Withdrawal failed.");
sc.next(); // Consume invalid input
}
break;
case 3:
account.checkBalance();
break;
case 4:
account.displayAccountDetails();
break;
case 5:
System.out.println("Thank you for using the Bank Account Management System! Exiting...");
break;
default:
System.out.println("Invalid choice! Please try again (1-5).");
}
} while (choice != 5);
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
}