-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (110 loc) · 3.63 KB
/
index.js
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
#! /usr/bin/env node
import inquirer from 'inquirer';
// Bank Account class
class BankAccount {
accountNumber;
balance;
constructor(accountNumber, balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Debit money
withdraw(amount) {
if (this.balance >= amount) {
this.balance -= amount;
console.log(`Withdrawal of $${amount} successful. Remaining balance: $${this.balance}`);
}
else {
console.log('Insufficient balance.');
}
}
// Credit money
deposit(amount) {
if (amount > 100) {
amount -= 1; // $1 fee charged if more than $100 is deposited
}
this.balance += amount;
console.log(`Deposit of $${amount} successful. Remaining balance: $${this.balance}`);
}
// Check balance
checkBalance() {
console.log(`Current balance: $${this.balance}`);
}
}
// Customer class
class Customer {
firstName;
lastName;
gender;
age;
mobileNumber;
account;
constructor(firstName, lastName, gender, age, mobileNumber, account) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.age = age;
this.mobileNumber = mobileNumber;
this.account = account;
}
}
// Create bank accounts
const accounts = [
new BankAccount(1001, 500),
new BankAccount(1002, 1000),
new BankAccount(1003, 2000)
];
// Create customers
const customers = [
new Customer('Abdul', 'Waheed', 'Male', 32, 3222392618, accounts[0]),
new Customer('Muhammad', 'Amin', 'Male', 26, 3002115450, accounts[1]),
new Customer('Rizwan', 'Ahmed', 'Male', 35, 3155454544, accounts[2])
];
// Function to interact with bank account
async function service() {
do {
const accountNumberInput = await inquirer.prompt({
name: 'accountNumber',
type: 'number',
message: 'Enter your account number:'
});
const customer = customers.find(customer => customer.account.accountNumber === accountNumberInput.accountNumber);
if (customer) {
console.log(`Welcome, ${customer.firstName} ${customer.lastName}!\n`);
const answer = await inquirer.prompt([{
name: 'select',
type: 'list',
message: 'Select an operation',
choices: ['Deposit', 'Withdrawal', 'Check Balance', 'Exit']
}]);
switch (answer.select) {
case 'Deposit':
const depositAmount = await inquirer.prompt({
name: 'amount',
type: 'number',
message: 'Enter the amount to deposit:'
});
customer.account.deposit(depositAmount.amount);
break;
case 'Withdrawal':
const withdrawalAmount = await inquirer.prompt({
name: 'amount',
type: 'number',
message: 'Enter the amount to withdraw:'
});
customer.account.withdraw(withdrawalAmount.amount);
break;
case 'Check Balance':
customer.account.checkBalance();
break;
case 'Exit':
console.log('Exiting bank program....');
return; // Exit the loop and the program
}
}
else {
console.log('Account not found. Please try again.');
}
} while (true);
}
service();