-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccountSystemModify.java
183 lines (167 loc) · 6.75 KB
/
BankAccountSystemModify.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
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
/*
Modify the assignment number 23 with following changes:
Create a class Account that stores customer name, account
number and type of account. From this, derive the classes
Curr-acct and Sav-acct to make them more specific to their requirements
. Include necessary methods in order to achieve the following tasks:
(a)Accept deposit from a customer and update the balance.
(b)Display the balance.
(c)Compute and deposit interest.
(d)Permit withdrawal and update the balance.
(e)Check for the minimum balance, impose penalty,
if necessary and update the balance.
(f)Do not use any constructors, Use methods to initialize the class members.
*/
import java.util.Scanner;
class Account {
protected String customerName;
protected int accountNumber;
protected String accountType;
protected double balance;
public void setInitialValues(String name, int accNum, String accType, double initBalance) {
customerName = name;
accountNumber = accNum;
accountType = accType;
balance = initBalance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposit of $" + amount + " successful.");
}
public void displayBalance() {
System.out.println("Depositor Name: " + customerName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Type: " + accountType);
System.out.println("Balance: $" + balance);
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawal of $" + amount + " successful.");
} else {
System.out.println("Insufficient funds.");
}
}
public void computeInterest(double rate) {
System.out.println("Account type does not earn interest.");
}
public void checkMinimumBalance(double minBalance, double penalty) {
System.out.println("Account type does not have minimum balance check.");
}
}
class CurrAcct extends Account {
@Override
public void withdraw(double amount) {
if (balance - amount >= 1000) {
balance -= amount;
System.out.println("Withdrawal of $" + amount + " successful.");
} else {
System.out.println("Insufficient funds or below minimum balance of $1000.");
}
}
@Override
public void checkMinimumBalance(double minBalance, double penalty) {
if (balance < minBalance) {
balance -= penalty;
System.out.println("Minimum balance penalty of $" + penalty + " imposed.");
}
}
}
class SavAcct extends Account {
@Override
public void computeInterest(double rate) {
double interest = balance * rate / 100;
balance += interest;
System.out.println("Interest of $" + interest + " credited.");
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Account account = null;
while (true) {
System.out.println("\n1. Create Account\n2. Deposit\n3. Withdraw\n4. Display Balance\n5. Compute Interest (Savings)\n6. Check Minimum Balance (Current)\n7. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
account = createAccount(scanner);
break;
case 2:
if (account != null) {
System.out.print("Enter deposit amount: ");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
} else {
System.out.println("Create an account first.");
}
break;
case 3:
if (account != null) {
System.out.print("Enter withdrawal amount: ");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
} else {
System.out.println("Create an account first.");
}
break;
case 4:
if (account != null) {
account.displayBalance();
} else {
System.out.println("Create an account first.");
}
break;
case 5:
if (account instanceof SavAcct) {
SavAcct savingsAccount = (SavAcct) account;
System.out.print("Enter interest rate: ");
double rate = scanner.nextDouble();
savingsAccount.computeInterest(rate);
} else {
System.out.println("Interest can only be computed for savings account.");
}
break;
case 6:
if (account instanceof CurrAcct) {
CurrAcct currentAccount = (CurrAcct) account;
System.out.print("Enter minimum balance: ");
double minBalance = scanner.nextDouble();
System.out.print("Enter penalty amount: ");
double penalty = scanner.nextDouble();
currentAccount.checkMinimumBalance(minBalance, penalty);
} else {
System.out.println("Minimum balance check is only for current account.");
}
break;
case 7:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice.");
}
}
}
private static Account createAccount(Scanner scanner) {
System.out.print("Enter depositor name: ");
String name = scanner.next();
System.out.print("Enter account number: ");
int accNum = scanner.nextInt();
System.out.print("Enter account type (savings/current): ");
String accType = scanner.next();
System.out.print("Enter initial balance: ");
double initBalance = scanner.nextDouble();
Account account;
if (accType.equalsIgnoreCase("savings")) {
account = new SavAcct();
} else if (accType.equalsIgnoreCase("current")) {
account = new CurrAcct();
} else {
System.out.println("Invalid account type. Account not created.");
return null;
}
account.setInitialValues(name, accNum, accType, initBalance);
return account;
}
}