Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week-4 #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Week4/prep-exercises/1-wallet/ex2-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}

Expand All @@ -42,13 +51,26 @@ 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() {
const walletJack = new Wallet('Jack', 100);
const walletJoe = new Wallet('Joe', 10);
const walletJane = new Wallet('Jane', 20);

walletJack.setDailyAllowance(80);


walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);

Expand Down
28 changes: 23 additions & 5 deletions Week4/prep-exercises/1-wallet/ex3-object.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)}`
Expand All @@ -37,7 +55,7 @@ function createWallet(name, cash = 0) {

getName: function () {
return this._name;
},
}
};
}

Expand Down
20 changes: 20 additions & 0 deletions Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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,
};
}
Expand Down
28 changes: 23 additions & 5 deletions Week4/prep-exercises/1-wallet/ex5-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ 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) {
console.log(`Insufficient funds!`);
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(
Expand All @@ -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);
Expand Down