-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatm.js
82 lines (65 loc) · 2.17 KB
/
atm.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
"use strict";
// Functions for use of the ATM
const account = require("./account");
const prompt = require("prompt-sync")();
let balance = parseFloat(account.balance);
/*====================================================================*/
function getBalance() {
console.log("Your balance is: $" + balance.toFixed(2));
}
/*====================================================================*/
function withdraw(amount) {
while (notANumber(amount)) {
console.log("The amount you have entered is invalid. Please enter a number for withdrawl.");
amount = prompt();
}
while (amount > balance) {
console.log("Insufficient funds. Please enter a number less than " + balance.toFixed(2));
amount = prompt();
while (notANumber(amount)) {
console.log("The amount you have entered is invalid. Please enter a number for withdrawl.");
amount = prompt();
}
}
balance -= parseFloat(amount);
console.log("Thank you. Your new balance is: $" + balance.toFixed(2));
return balance;
}
/*====================================================================*/
function deposit(amount) {
while (notANumber(amount)) {
console.log("The amount you have entered is invalid. Please enter a number for deposit.");
amount = prompt();
}
balance += parseFloat(amount);
console.log("Thank you. Your new balance is: $" + balance.toFixed(2));
return balance;
}
/*====================================================================*/
function validatePin(number) {
if (number === account.pinNum) {
return true;
}
else {
return false;
}
}
/*====================================================================*/
function notANumber (amount) {
let regex = new RegExp(/^[0-9]+\.*[0-9]*$/);
if (Number.isNaN(amount) && !regex.test(amount)) {
return true;
}
else if (!regex.test(amount)) {
return true;
}
else {
return false;
}
}
/*======================================================================*/
module.exports.getBalance = getBalance;
module.exports.withdraw = withdraw;
module.exports.deposit = deposit;
module.exports.validatePin = validatePin;
module.exports.balance = balance;