-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccountManagementSystem.java
More file actions
217 lines (186 loc) · 8.56 KB
/
BankAccountManagementSystem.java
File metadata and controls
217 lines (186 loc) · 8.56 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
abstract class BankAccount {
private static int accountCounter = 1000;
private int accountNumber;
private String accountHolder;
protected double balance;
public BankAccount(String accountHolder, double balance) {
this.accountNumber = ++accountCounter;
this.accountHolder = accountHolder;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountHolder() {
return accountHolder;
}
public double getBalance() {
return balance;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
class SavingsAccount extends BankAccount {
private static final double INTEREST_RATE = 0.04;
private static final double MIN_BALANCE = 500.0;
private List<String> transactions = new ArrayList<>();
public SavingsAccount(String accountHolder, double balance) {
super(accountHolder, balance);
transactions.add("Account opened with initial balance: " + balance);
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
transactions.add("Deposited: " + amount + ", New Balance: " + balance);
System.out.println("Successfully deposited " + amount);
} else {
System.out.println("Invalid deposit amount!");
}
}
public void withdraw(double amount) {
if (amount > 0 && (balance - amount) >= MIN_BALANCE) {
balance -= amount;
transactions.add("Withdrew: " + amount + ", New Balance: " + balance);
System.out.println("Successfully withdrew " + amount);
} else {
System.out.println("Insufficient balance or minimum balance requirement not met!");
}
}
public void calculateInterest() {
double interest = balance * INTEREST_RATE;
balance += interest;
transactions.add("Interest added: " + interest + ", New Balance: " + balance);
System.out.println("Interest of " + interest + " added to your balance.");
}
public void printTransactions() {
System.out.println("\nTransaction History for Account No. " + getAccountNumber() + ":");
for (String transaction : transactions) {
System.out.println(transaction);
}
}
}
class CheckingAccount extends BankAccount {
private static final double OVERDRAFT_LIMIT = 1000.0;
private List<String> transactions = new ArrayList<>();
public CheckingAccount(String accountHolder, double balance) {
super(accountHolder, balance);
transactions.add("Account opened with initial balance: " + balance);
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
transactions.add("Deposited: " + amount + ", New Balance: " + balance);
System.out.println("Successfully deposited " + amount);
} else {
System.out.println("Invalid deposit amount!");
}
}
public void withdraw(double amount) {
if (amount > 0 && (balance + OVERDRAFT_LIMIT) >= amount) {
balance -= amount;
transactions.add("Withdrew: " + amount + ", New Balance: " + balance);
System.out.println("Successfully withdrew " + amount);
} else {
System.out.println("Overdraft limit exceeded!");
}
}
public void printTransactions() {
System.out.println("\nTransaction History for Account No. " + getAccountNumber() + ":");
for (String transaction : transactions) {
System.out.println(transaction);
}
}
}
public class BankAccountManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<BankAccount> accounts = new ArrayList<>();
try {
while (true) {
System.out.println("\n1. Open Account\n2. Deposit\n3. Withdraw\n4. Calculate Interest (Savings)\n5. Print Transactions\n6. Exit");
System.out.print("Choose an option: ");
int option = Integer.parseInt(scanner.nextLine());
if (option == 1) {
System.out.print("Enter account holder name: ");
String name = scanner.nextLine();
System.out.print("Initial deposit: ");
double deposit = Double.parseDouble(scanner.nextLine());
System.out.print("Choose account type (1 for Savings, 2 for Checking): ");
int type = Integer.parseInt(scanner.nextLine());
BankAccount account;
if (type == 1) {
account = new SavingsAccount(name, deposit);
} else {
account = new CheckingAccount(name, deposit);
}
accounts.add(account);
System.out.println("Account created with Account Number: " + account.getAccountNumber());
} else if (option == 2) {
System.out.print("Enter account number: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
BankAccount account = findAccountByNumber(accounts, accountNumber);
if (account != null) {
System.out.print("Enter amount to deposit: ");
double amount = Double.parseDouble(scanner.nextLine());
account.deposit(amount);
} else {
System.out.println("Account not found.");
}
} else if (option == 3) {
System.out.print("Enter account number: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
BankAccount account = findAccountByNumber(accounts, accountNumber);
if (account != null) {
System.out.print("Enter amount to withdraw: ");
double amount = Double.parseDouble(scanner.nextLine());
account.withdraw(amount);
} else {
System.out.println("Account not found.");
}
} else if (option == 4) {
System.out.print("Enter account number (Savings only): ");
int accountNumber = Integer.parseInt(scanner.nextLine());
BankAccount account = findAccountByNumber(accounts, accountNumber);
if (account != null && account instanceof SavingsAccount) {
((SavingsAccount) account).calculateInterest();
} else {
System.out.println("Account not found or is not a Savings Account.");
}
} else if (option == 5) {
System.out.print("Enter account number to view transactions: ");
int accountNumber = Integer.parseInt(scanner.nextLine());
BankAccount account = findAccountByNumber(accounts, accountNumber);
if (account != null) {
if (account instanceof SavingsAccount) {
((SavingsAccount) account).printTransactions();
} else if (account instanceof CheckingAccount) {
((CheckingAccount) account).printTransactions();
}
} else {
System.out.println("Account not found.");
}
} else if (option == 6) {
System.out.println("Exiting the system.");
break;
} else {
System.out.println("Invalid option. Please try again.");
}
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter correct data.");
} finally {
scanner.close();
}
}
private static BankAccount findAccountByNumber(List<BankAccount> accounts, int accountNumber) {
for (BankAccount account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return account;
}
}
return null;
}
}