-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion45.java
65 lines (62 loc) · 2.16 KB
/
Question45.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
import java.util.Scanner;
/* declare a Java class called SavingsAccount with members accountNumber and
Balance and member functions depositAmount and withdrawAmount if user tries to withdraw amount greater than their balance throw a user-defined exception */
class SavingsAccount extends Exception { // inheriting Exception class
String accountNumber;
double Balance;
SavingsAccount (String acNum, double initmoney) {
accountNumber = acNum;
Balance = initmoney;
}
public void depositAmount (double deposit) {
Balance += deposit;
System.out.println ("Money deposited! Balance = " + Balance);
}
public void withdrawAmount (double withdraw) throws SavingsAccount {
// method can throw SavingsAccount type error
if (withdraw > Balance) throw new SavingsAccount (accountNumber, Balance);
else {
Balance -= withdraw;
System.out.println ("Money withdrawn! Balance = " + Balance);
}
}
public String toString () { // reimplementing toString method of Exception class
return "WithdrawException! " + accountNumber + " balance = " + Balance + " less than withdraw amount:";
}
}
class Question45 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
double value;
char ans = 'Y';
byte choice;
SavingsAccount acc = new SavingsAccount ("SB1001", 0.0);
System.out.print ("Make initial deposit: ");
value = input.nextDouble ();
acc.depositAmount (value);
// menu-driven
while (ans == 'Y' || ans == 'y') {
System.out.println ("1.Deposit\n2.Withdraw");
System.out.print ("Choice?: ");
choice = input.nextByte ();
switch (choice) {
case 1: System.out.print ("Enter deposit amount: ");
value = input.nextDouble ();
acc.depositAmount (value);
break;
case 2: try { // try-catch inside a switch-case inside a while loop
System.out.print ("Enter withdraw amount: ");
value = input.nextDouble ();
acc.withdrawAmount (value);
}
catch (SavingsAccount sa) {
System.out.println (sa + " " + value);
}
break;
default: System.out.println ("Invalid choice!");
}
System.out.print ("Continue? (Y/N): ");
ans = input.next ().charAt (0); // taking character input
}
}
}