-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.java
139 lines (113 loc) · 4.42 KB
/
bank.java
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
//Write a Java program to create a banking system with three classes - Bank, Savings Account, and CurrentAccount.
//The bank should have a list of accounts and methods for adding them.
//Accounts should be an interface with methods to deposit, withdraw,calculate interest, and view balances.
//SavingsAccount and CurrentAccount should implement the Account interface and have their own unique methods
interface Account {
void deposit(double amount);
void withdraw(double amount);
double calculateInterest();
void viewBalance();
}
class SavingsAccount implements Account {
private String username;
private int accNo;
private double balance;
private double interestRate;
SavingsAccount(String username, int accNo, double balance, double interestRate) {
this.username = username;
this.accNo = accNo;
this.balance = balance;
this.interestRate = interestRate;
}
public void deposit(double amount) {
balance += amount;
System.out.println(amount + " deposited successfully.");
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Insufficient balance.");
}
}
public double calculateInterest() {
return balance * (interestRate / 100);
}
public void viewBalance() {
System.out.println("Account Balance: " + balance);
}
}
class CurrentAccount implements Account {
private String username;
private int accNo;
private double balance;
private double overdraftLimit;
CurrentAccount(String username, int accNo, double balance, double overdraftLimit) {
this.username = username;
this.accNo = accNo;
this.balance = balance;
this.overdraftLimit = overdraftLimit;
}
public void deposit(double amount) {
balance += amount;
System.out.println(amount + " deposited successfully.");
}
public void withdraw(double amount) {
if (balance - overdraftLimit >= amount) {
balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Withdrawal amount exceeds overdraft limit.");
}
}
public double calculateInterest() {
// Current account typically doesn't earn interest
return 0;
}
public void viewBalance() {
System.out.println("Account Balance: " + balance);
}
}
class Bank {
private Account[] accounts;
private int numOfAccounts;
Bank(int size) {
accounts = new Account[size];
numOfAccounts = 0;
}
void addAccount(Account account) {
if (numOfAccounts < accounts.length) {
accounts[numOfAccounts] = account;
numOfAccounts++;
System.out.println("Account added successfully.");
} else {
System.out.println("Cannot add more accounts. Bank is full.");
}
}
}
public class main {
public static void main(String[] args) {
Bank bank = new Bank(10); // Assume the bank can hold up to 10 accounts
// Example usage:
SavingsAccount savingsAccount = new SavingsAccount("John", 1001, 5000, 5); // 5% interest rate
CurrentAccount currentAccount = new CurrentAccount("Alice", 2001, 10000, 2000); // $2000 overdraft limit
bank.addAccount(savingsAccount);
bank.addAccount(currentAccount);
// You can add more accounts and perform transactions as needed
// Accessing methods of savingsAccount
System.out.println("Savings Account:");
savingsAccount.viewBalance(); // View balance
savingsAccount.deposit(2000); // Deposit $2000
savingsAccount.withdraw(1000); // Withdraw $1000
System.out.println("Interest earned: " + savingsAccount.calculateInterest()); // Calculate interest
savingsAccount.viewBalance(); // View balance again
// Accessing methods of currentAccount
System.out.println("\nCurrent Account:");
currentAccount.viewBalance(); // View balance
currentAccount.deposit(3000); // Deposit $3000
currentAccount.withdraw(15000); // Withdraw $15000 (exceeding overdraft limit)
currentAccount.withdraw(8000); // Withdraw $8000 (within overdraft limit)
currentAccount.viewBalance(); // View balance again
}
}