-
Notifications
You must be signed in to change notification settings - Fork 0
/
atm.ts
78 lines (68 loc) · 2.02 KB
/
atm.ts
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
import promptSync from 'prompt-sync';
const prompt = promptSync();
class ATM {
private balance: number;
constructor(initialBalance: number = 0) {
this.balance = initialBalance;
}
deposit(amount: number): void {
if (amount > 0) {
this.balance += amount;
console.log(`Deposited: ${amount}`);
} else {
console.log('Deposit amount must be positive.');
}
}
transaction(amount: number): void {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
console.log(`Withdrew: ${amount}`);
} else {
console.log('Insufficient funds or invalid amount.');
}
}
getBalance(): void {
console.log(`Current balance: ${this.balance}`);
}
exit(): void {
console.log('Exiting the ATM. Have a nice day!');
}
showMenu(): void {
console.log(`
ATM Menu:
1. Deposit
2. Withdraw
3. Balance Inquiry
4. Exit
`);
}
run(): void {
let running = true;
while (running) {
this.showMenu();
const choice = prompt('Enter your choice: ');
switch (choice) {
case '1':
const depositAmount = parseFloat(prompt('Enter amount to deposit: '));
this.deposit(depositAmount);
break;
case '2':
const withdrawAmount = parseFloat(prompt('Enter amount to withdraw: '));
this.transaction(withdrawAmount);
break;
case '3':
this.getBalance();
break;
case '4':
this.exit();
running = false;
break;
default:
console.log('Invalid choice. Please try again.');
}
}
}
}
// Initialize ATM with an initial balance of 1000
const atm = new ATM(1000);
atm.run();