-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_7a.java
More file actions
66 lines (56 loc) · 1.91 KB
/
Practical_7a.java
File metadata and controls
66 lines (56 loc) · 1.91 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
class NotEnoughMoneyException extends Exception {
public NotEnoughMoneyException(String message) {
super(message);
}
}
class Bank {
private double balance;
public Bank(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
System.out.println("Depositing: " + amount);
balance += amount;
System.out.println("New Balance: " + balance);
}
public void withdraw(double amount) throws NotEnoughMoneyException {
if (amount > balance) {
System.out.println("Insufficient balance!");
return;
}
balance -= amount;
System.out.println("Withdrawn: " + amount);
System.out.println("New Balance: " + balance);
if (balance < 500) {
throw new NotEnoughMoneyException("Balance below 500 Rs!");
}
}
}
public class Practical_7a {
public static void main(String[] args) {
System.out.println("12402080503006");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter initial balance: ");
Bank account = new Bank(scanner.nextDouble());
while (true) {
System.out.println("\n1. Deposit 2. Withdraw 3. Exit");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter amount: ");
account.deposit(scanner.nextDouble());
} else if (choice == 2) {
System.out.print("Enter amount: ");
try {
account.withdraw(scanner.nextDouble());
} catch (NotEnoughMoneyException e) {
System.out.println(e.getMessage()+"Not have Sufficeient Balance");
}
} else {
System.out.println("Goodbye!");
break;
}
}
scanner.close();
}
}