-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
179 lines (171 loc) · 6.67 KB
/
Main.java
File metadata and controls
179 lines (171 loc) · 6.67 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
169
170
171
172
173
174
175
176
177
178
179
import java.util.Scanner; //abhinavgautam08
import java.util.InputMismatchException;
//abhinavgautam08
class Account {
private int accountNumber;
private String accountHolderName;
private double balance;
private String email;
private String phoneNumber;
public Account(int accountNumber, String accountHolderName, double initialDeposit,
String email, String phoneNumber) {
this.accountNumber = accountNumber;
this.accountHolderName = accountHolderName;
this.balance = initialDeposit;
this.email = email;
this.phoneNumber = phoneNumber;
}
public int getAccountNumber() {
return accountNumber;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit successful! Current balance: " + balance);
} else {
System.out.println("Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Withdrawal amount must be positive.");//abhinavgautam08
} else if (amount > balance) {
System.out.println("Insufficient balance.");
} else {
balance -= amount;
System.out.println("Withdrawal successful! Current balance: " + balance);//abhinavgautam08
}
}//abhinavgautam08
public void displayAccountDetails() {
System.out.println("\n=== Account Details ===");
System.out.println("AC Number: " + accountNumber);
System.out.println("Holder Name: " + accountHolderName);
System.out.println("Balance: " + balance);
System.out.println("Email: " + email);
System.out.println("Phone: " + phoneNumber);
}
public void updateContactDetails(String email, String phoneNumber) {
if (isValidEmail(email) && isValidPhone(phoneNumber)) {
this.email = email;
this.phoneNumber = phoneNumber;
System.out.println("Contact details updated successfully.");
} else {
System.out.println("Invalid email or phone number format.");
}
}
private boolean isValidEmail(String email) {
return email.contains("@") && email.contains(".");
}
private boolean isValidPhone(String phone) {
return phone.matches("\\d{10}");
}
}
class Main {
// Data storage
private static Account[] accounts = new Account[100];
private static int accountCount = 0;
private static int nextAccountNumber = 1001;
private static Scanner sc = new Scanner(System.in);
// Entry point
public static void main(String[] args) {
System.out.println("Welcome to the Banking Application! CLI");
mainMenu();//abhinavgautam08
}
private static void mainMenu() {
while (true) {
try {
System.out.println("\n=== Main Menu ===");
System.out.println("1. Create a new account");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4. View account details");
System.out.println("5. Update contact details");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine();
// Switch menu options
switch (choice) {
case 1 -> createAccount();
case 2 -> performDeposit();
case 3 -> performWithdrawal();
case 4 -> showAccountDetails();
case 5 -> updateContact();
case 6 -> {
System.out.println("Exiting... Thank you for using the Banking Application!");
return;//abhinavgautam08
}
default -> System.out.println("Invalid choice. Try again.");//abhinavgautam08
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter numbers only.");
sc.nextLine();//abhinavgautam08
}
}
}
private static void createAccount() {
System.out.print("Enter account holder name: ");//abhinavgautam08
String name = sc.nextLine();
System.out.print("Enter initial deposit amount: ");
double initialDeposit = sc.nextDouble();
sc.nextLine();
System.out.print("Enter email address: ");
String email = sc.nextLine();
System.out.print("Enter phone number (10 digits): ");
String phone = sc.nextLine();
if (initialDeposit > 0 && email.contains("@") && phone.matches("\\d{10}")) {
accounts[accountCount] = new Account(nextAccountNumber, name, initialDeposit, email, phone);
System.out.println("Account created successfully with Account Number: " + nextAccountNumber);
nextAccountNumber++;
accountCount++;
} else {
System.out.println("Invalid input. Account creation failed.");
}
}
private static void performDeposit() {
Account acc = findAccount();
if (acc != null) {
System.out.print("Enter amount to deposit: ");
double amount = sc.nextDouble();
sc.nextLine();
acc.deposit(amount);
}
}
private static void performWithdrawal() {
Account acc = findAccount();
if (acc != null) {
System.out.print("Enter amount to withdraw: ");
double amount = sc.nextDouble();
sc.nextLine();
acc.withdraw(amount);
}
}
private static void showAccountDetails() {
Account acc = findAccount();
if (acc != null) {
acc.displayAccountDetails();//abhinavgautam08
}
}
private static void updateContact() {
Account acc = findAccount();
if (acc != null) {
System.out.print("Enter new email: ");
String email = sc.nextLine();
System.out.print("Enter new phone number: ");
String phone = sc.nextLine();
acc.updateContactDetails(email, phone);//abhinavgautam08
}
}
private static Account findAccount() {
System.out.print("Enter account number: ");
int accNum = sc.nextInt();
sc.nextLine();//abhinavgautam08
for (int i = 0; i < accountCount; i++) {
if (accounts[i].getAccountNumber() == accNum) {
return accounts[i];//abhinavgautam08
}
}
System.out.println("Account not found.");
return null;
}//abhinavgautam08
}