-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3p2.java
More file actions
46 lines (37 loc) · 1.34 KB
/
Assignment3p2.java
File metadata and controls
46 lines (37 loc) · 1.34 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
import java.util.Scanner;
class InvalidPinException extends Exception {
public InvalidPinException(String message) {
super(message);
}
}
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
public class Assignment3p2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double balance = 3000;
int correctPin = 1234;
try {
System.out.print("Enter PIN: ");
int pin = scanner.nextInt();
if (pin != correctPin) {
throw new InvalidPinException("Error: Invalid PIN.");
}
System.out.print("Withdraw Amount: ");
double withdrawAmount = scanner.nextDouble();
if (withdrawAmount > balance) {
throw new InsufficientBalanceException("Error: Insufficient balance. Current Balance: " + balance);
}
balance -= withdrawAmount;
System.out.println("Withdrawal successful. Remaining Balance: " + balance);
} catch (InvalidPinException | InsufficientBalanceException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("Remaining Balance: " + balance);
scanner.close();
}
}
}