From a6c3ad99d1add39b00d463c2a3a12345b0ca9a16 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Sat, 5 Oct 2024 21:49:24 +0200 Subject: [PATCH 1/2] ex2 classes --- Week4/prep-exercises/1-wallet/ex2-classes.js | 22 ++++++++++++++++++++ Week4/prep-exercises/1-wallet/ex3-object.js | 18 +++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) 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..613a93b 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; @@ -15,7 +17,12 @@ 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; }, @@ -34,8 +41,17 @@ function createWallet(name, cash = 0) { `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); }, + resetDailyAllowance: function() { + this._dayTotalWithdrawals = 0; + console.log("Daily withdrawal limit has been reset."); + }, + + setDailyAllowance: function(newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`New daily withdrawal limit is set to ${newAllowance} euros.`); + }, - getName: function () { + getName: function () { return this._name; }, }; From 53fcefff7a203fa8b40bfd6e58bdf7e06255dfa3 Mon Sep 17 00:00:00 2001 From: Esen Karatas Date: Sat, 5 Oct 2024 22:48:17 +0200 Subject: [PATCH 2/2] exercise-3 --- Week4/prep-exercises/1-wallet/ex3-object.js | 30 ++++++++++--------- .../1-wallet/ex4-object-shared-methods.js | 20 +++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 28 +++++++++++++---- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index 613a93b..bff2153 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,11 +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: 40, - _dayTotalWithdrawals: 0, + _dailyAllowance: dailyAllowance, + _dayTotalWithdrawals: dayTotalWithdrawals, deposit: function (amount) { this._cash += amount; @@ -20,6 +20,7 @@ function createWallet(name, cash = 0) { if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { console.log(`Daily allowance limit not sufficient!`); return 0; + } this._cash -= amount; this._dayTotalWithdrawals += amount; @@ -28,32 +29,33 @@ function createWallet(name, cash = 0) { 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); }, - reportBalance: function () { + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` ); }, - resetDailyAllowance: function() { + + resetDailyAllowance: function () { this._dayTotalWithdrawals = 0; console.log("Daily withdrawal limit has been reset."); }, - setDailyAllowance: function(newAllowance) { - this._dailyAllowance = newAllowance; - console.log(`New daily withdrawal limit is set to ${newAllowance} euros.`); + reportBalance: function () { + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); }, - getName: function () { + 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);