-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-account.js
More file actions
34 lines (31 loc) · 1.75 KB
/
Copy pathcreate-account.js
File metadata and controls
34 lines (31 loc) · 1.75 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
function createAccount(pin, amount = 0) {
// It returns an object with four methods: checkBalance, deposit, withdraw, and changePin.
return {
// checkBalance takes an inputPin, checks if it matches the account's pin, and if it does, returns the account balance.
checkBalance(inputPin) {
if (inputPin !== pin) return "Invalid PIN.";
return `$${amount}`;
},
// deposit takes an inputPin and a newAmount. If the inputPin matches the account's pin, it increments the account balance by newAmount and returns a success message with the new balance.
deposit(inputPin, newAmount) {
if (inputPin !== pin) return "Invalid PIN.";
amount += newAmount;
return `Succesfully deposited $${newAmount}. Current balance: $${amount}.`;
},
// withdraw takes an inputPin and a withdrawalAmount. If the inputPin matches the account's pin, and the withdrawalAmount does not exceed the account balance, it decrements the account balance by withdrawalAmount and returns a success message with the new balance.
withdraw(inputPin, withdrawalAmount) {
if (inputPin !== pin) return "Invalid PIN.";
if (withdrawalAmount > amount)
return "Withdrawal amount exceeds account balance. Transaction cancelled.";
amount -= withdrawalAmount;
return `Succesfully withdrew $${withdrawalAmount}. Current balance: $${amount}.`;
},
// changePin takes an oldPin and a newPin. If the oldPin matches the account's pin, it changes the pin to newPin and returns a success message.
changePin(oldPin, newPin) {
if (oldPin !== pin) return "Invalid PIN.";
pin = newPin;
return "PIN successfully changed!";
}
};
}
module.exports = { createAccount };