-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBankService.java
More file actions
90 lines (89 loc) · 2.48 KB
/
BankService.java
File metadata and controls
90 lines (89 loc) · 2.48 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
package Bank;
import java.util.List;
import java.util.ArrayList;
public class BankService {
private List<BankAccount> customers = new ArrayList<>();
public void addCustomer(BankAccount customer)
{
customers.add(customer);
System.out.println("Account added successfuly");
}
public void depositCustomer(String accNo, double amount)
{
for(BankAccount acc : customers)
{
if(acc.getAccno().equals(accNo))
{
double newBalance = acc.getBalance() + amount;
acc.setBalance(newBalance);
System.out.println("Deposited " + amount + "\nNew Balance : " + newBalance);
return;
}
else
{
System.out.println("Account not found.");
}
}
}
public void withdrawCustomer(String accNo, double amount)
{
for (BankAccount acc : customers)
{
if (acc.getAccno().equals(accNo))
{
if (acc.getBalance() >= amount)
{
double newBalance = acc.getBalance() - amount;
acc.setBalance(newBalance);
System.out.println("Withdrawn " + amount + "\nNew Balance: " + newBalance);
} else {
System.out.println("Insufficient balance.");
}
return;
}
else
{
System.out.println("Account not found.");
}
}
}
public void viewCustomer()
{
if (customers.isEmpty()) {
System.out.println("No accounts to display.");
}
else
{
for (BankAccount acc : customers)
{
System.out.println("Account No: " + acc.getAccno());
System.out.println("Holder Name: " + acc.getAccholder());
System.out.println("Balance: " + acc.getBalance());
}
}
}
public void transfer(String fromAccNo, String toAccNo, double amount)
{
BankAccount sender = null, receiver = null;
for (BankAccount acc : customers) {
if (acc.getAccno().equals(fromAccNo))
{
sender = acc;
} else if (acc.getAccno().equals(toAccNo))
{
receiver = acc;
}
}
if (sender == null || receiver == null) {
System.out.println("One or both accounts not found.");
return;
}
if (sender.getBalance() < amount) {
System.out.println("Insufficient funds in sender's account.");
return;
}
sender.setBalance(sender.getBalance() - amount);
receiver.setBalance(receiver.getBalance() + amount);
System.out.println("Transfer successful: " + amount + " from " + fromAccNo + " to " + toAccNo);
}
}