diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..0d4580b 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,10 +3,14 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = 40; + this.#dayTotalWithdrawals = 0; } get name() { @@ -22,8 +26,13 @@ class Wallet { console.log(`Insufficient funds!`); return 0; } + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Daily allowance limit not sufficient!`); + return 0; + } this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -42,6 +51,16 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } } function main() { @@ -49,6 +68,9 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); + walletJack.setDailyAllowance(80); + + walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..bff2153 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,11 @@ import eurosFormatter from './euroFormatter.js'; -function createWallet(name, cash = 0) { +function createWallet(name, cash = 0, dailyAllowance = 40, dayTotalWithdrawals = 0) { return { _name: name, _cash: cash, + _dailyAllowance: dailyAllowance, + _dayTotalWithdrawals: dayTotalWithdrawals, deposit: function (amount) { this._cash += amount; @@ -15,20 +17,36 @@ function createWallet(name, cash = 0) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Daily allowance limit not sufficient!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, transferInto: function (wallet, amount) { console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` + `Transferring ${eurosFormatter.format(amount)} from ${this._name} to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; + console.log("Daily withdrawal limit has been reset."); + }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -37,7 +55,7 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; - }, + } }; } diff --git a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js index bd4fd20..a6d346b 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -10,7 +10,13 @@ function withdraw(amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Daily withdrawal limit exceeded!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -29,6 +35,16 @@ function reportBalance() { `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; + console.log("Daily withdrawal limit has been reset."); +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`New daily withdrawal limit is set to ${newAllowance} euros.`); +} + function getName() { return this._name; @@ -38,10 +54,14 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, + resetDailyAllowance, + setDailyAllowance, getName, }; } diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..05b1f4f 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,11 +3,13 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { this._cash += amount; -}; +} Wallet.prototype.withdraw = function (amount) { if (this._cash - amount < 0) { @@ -15,9 +17,15 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Daily withdrawal limit exceeded!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; -}; +} Wallet.prototype.transferInto = function (wallet, amount) { console.log( @@ -27,17 +35,27 @@ Wallet.prototype.transferInto = function (wallet, amount) { ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); -}; +} Wallet.prototype.reportBalance = function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); -}; +} Wallet.prototype.getName = function () { return this._name; -}; +} + +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; + console.log("Daily withdrawal limit has been reset."); +} + +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`New daily withdrawal limit is set to ${newAllowance} euros.`); +} function main() { const walletJack = new Wallet('Jack', 100);