-
Notifications
You must be signed in to change notification settings - Fork 11
/
ATMAccount.java
141 lines (126 loc) · 4.36 KB
/
ATMAccount.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
140
141
import java.util.Scanner;
class Account {
String name;
String accNumber;
String accType;
double balance;
String[] transactions;
int transactionCount;
public Account() {
transactions = new String[5];
transactionCount = 0;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
addTransaction("Deposited: " + amount);
System.out.println("Deposit successful.");
}
}
void displayBalance() {
System.out.println("Account Balance: " + balance);
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
addTransaction("Withdrawn: " + amount);
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance for withdrawal.");
}
}
void addTransaction(String transaction) {
if (transactionCount < 5) {
transactions[transactionCount] = transaction;
transactionCount++;
} else {
for (int i = 0; i < 4; i++) {
transactions[i] = transactions[i + 1];
}
transactions[4] = transaction;
}
}
void displayTransactions() {
System.out.println("Recent Transactions:");
for (int i = 0; i < transactionCount; i++) {
System.out.println(transactions[i]);
}
}
}
class CurrAcct extends Account {
void checkMinBalance(double minBalance) {
if (balance < minBalance) {
double penalty = balance * 0.01; // 1% penalty
balance -= penalty;
addTransaction("Penalty imposed: " + penalty);
System.out.println("Penalty imposed due to low balance.");
}
}
}
class SavAcct extends Account {
void computeInterest(double rate) {
double interest = balance * (rate / 100);
balance += interest;
addTransaction("Interest:" + interest);
}
}
public class bankaccount{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter account type (Savings/Current): ");
String accType = scanner.next().toLowerCase();
Account account;
if (accType.equals("savings")) {
account = new SavAcct();
System.out.print("Enter interest rate: ");
double rate = scanner.nextDouble();
((SavAcct) account).computeInterest(rate);
} else if (accType.equals("current")) {
account = new CurrAcct();
System.out.print("Enter minimum balance: ");
double minBalance = scanner.nextDouble();
((CurrAcct) account).checkMinBalance(minBalance);
} else {
System.out.println("Invalid account type.");
return;
}
account.name = scanner.nextLine();
System.out.print("Enter customer name: ");
account.name = scanner.nextLine();
System.out.print("Enter account number: ");
account.accNumber = scanner.nextLine();
account.accType = accType;
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Display Balance");
System.out.println("4. Display Transactions");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter amount to deposit: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
break;
case 2:
System.out.print("Enter amount to withdraw: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
break;
case 3:
account.displayBalance();
break;
case 4:
account.displayTransactions();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}