Skip to content

feat(m5): added solution exercise 001 #2897

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

Open
wants to merge 3 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
128 changes: 128 additions & 0 deletions projects/m5/001-budget-app/js/class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
class Category {
/**
* Creates a new category
*
* @param {string} category - Name of the new category to be created
*/
constructor(category) {
this.category = category;
this.ledger = [];
}

/**
* Deposit a certain amount of money to this category's ledger
*
* @param {number} amount - The amount of money to deposit
* @param {string} [description = ''] - Optional: the description of the operation
*/
deposit(amount, description = "") {
if (this.checkAmount(amount)) {
this.ledger.push({ amount: +amount, description: description });
}
}

/**
* Withdraw a certain amount of money from the category's ledger, if there is enough deposited money
*
* @param {number} amount - The amount of money to withdraw
* @param {string} [description = ''] - Optional: the description of the operation
* @returns {boolean} - True if the operation was successful, otherwise false
*/
withdraw(amount, description = "") {
if (this.checkFunds(amount) && this.checkAmount(amount)) {
this.ledger.push({
amount: -amount,
description: description,
});
return true;
} else {
return false;
}
}

/**
* Get the balance from this category
*
* @returns {string} - returns formatted balance with two decimals
*/
getBalance() {
let tempAmount = 0;
for(const operation of this.ledger){
tempAmount += +operation.amount;
}
return tempAmount.toFixed(2);
}

/**
* Transfer the amount of money from one category to another, if the first category has enough funds
*
* @param {number} amount - The amount of money to transfer
* @param {string} category - The recipient category
* @returns {boolean} - True if the operation was successful, otherwise false
*/
transfer(amount, category) {
if (this.withdraw(+amount, `Transfer to ${category.category}`)) {
category.deposit(+amount, `Transfer from ${this.category}`);
return true;
} else {
return false;
}
}

/**
* Check if the balance of the category has at least the amount entered
*
* @param {number} amount - The amount to be checked against the balance
* @returns {boolean} - True if the balance is enough to cover the amount, false otherwise
*/
checkFunds(amount) {
return !(+amount > +this.getBalance());
}

/**
* Check if the amount, with two decimal, is equal or less than 7 digits
*
* @param {number} amount - amount to check
* @returns {boolean} - True if the amount is equal or less than 7 digits, false otherwise
*/
checkAmount(amount) {
if (Number(amount).toFixed(2).length > 7) {
console.log("The amount is too high, enter less than 9999.99€");
return false;
} else {
return true;
}
}

/**
* Prints the budget for the category
*
* @returns {string} - The printed budget
*/
printBudget() {
const voidCharacters = (30 - this.category.length) / 2;
let output = "";
const titleLine =
"*".repeat(Math.ceil(voidCharacters)) +
this.category +
"*".repeat(Math.floor(voidCharacters));
output += titleLine + "\n";
let totalAmount = 0;

for (const row of this.ledger) {
const rowDescription = row.description
.substr(0, 23)
.padEnd(30 - row.amount.toFixed(2).length, " ");
const rowAmount = row.amount.toFixed(2);
output += `${rowDescription}${rowAmount}\n`;

totalAmount += +row.amount;
}

output += `Total: ${totalAmount.toFixed(2)}`;

return output;
}
}

module.exports = Category;
91 changes: 91 additions & 0 deletions projects/m5/001-budget-app/js/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Creates the statistics and the chart for up to 4 categories (only the first 4 categories will be used)
*
* @param {array} categories - An array of the categories to be included in the chart
* @returns {string} - Returns the chart of up to 4 categories ordered by the amount spent in each
*/
function createSpendChart(categories) {
categories = categories.slice(0, 4);
let total = 0;
let percentageChart = [];

for (const category of categories) {
let categoryTotal = 0;
for (const movement of category.ledger) {
if (movement.amount < 0) {
total += -movement.amount;
categoryTotal += -movement.amount;
}
}
percentageChart.push({
category: category.category,
totalAmount: categoryTotal,
});
}

for (const category of percentageChart) {
category.percentage = Math.floor((category.totalAmount / total) * 10) * 10;
}

percentageChart = percentageChart.sort(
(a, b) => b.totalAmount - a.totalAmount
);
return writeChart(percentageChart);
}

/**
* Writes the formatted chart for the selected categories
*
* @param {Array.<Object>} chart - Array of the chart of each category that will be printed
* @returns {string} - Returns the formatted chart
*/
function writeChart(chart) {
let writtenChart = "Percentage spent by category\n";
const maxCategoryNameLength = Math.max(
...chart.map((chart) => chart.category).map((element) => element.length)
);

const percentages = [
"100|",
" 90|",
" 80|",
" 70|",
" 60|",
" 50|",
" 40|",
" 30|",
" 20|",
" 10|",
" 0|",
];

for (const percentage of percentages) {
writtenChart += percentage;
for (const category of chart) {
if (category.percentage >= parseInt(percentage)) {
writtenChart += " o ";
} else {
writtenChart += " ";
}
}
writtenChart += "\n";
}
writtenChart += ` ${"-".repeat(chart.length * 3 + 1)}\n`;

for (let i = 0; i < maxCategoryNameLength; i++) {
writtenChart += " ";
for (let j = 0; j < chart.length; j++) {
const letter = chart[j].category[i];
if (letter) {
writtenChart += ` ${letter} `;
} else {
writtenChart += " ";
}
}
writtenChart += "\n";
}

return writtenChart;
}

module.exports = createSpendChart;
30 changes: 30 additions & 0 deletions projects/m5/001-budget-app/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Category = require("./class.js");
const createSpendChart = require("./function.js");

//////////////////////////

const food = new Category("Food");
const business = new Category("Business");
const entertainment = new Category("Enternainment");
const clothes = new Category("Clothes");

food.deposit(900, "deposit");

business.deposit(500);

food.withdraw(40.0, "milk, cereal, eggs, bacon, bread");

business.withdraw(50);

business.withdraw(60);
clothes.withdraw(100);

clothes.deposit(1000, "budget");
clothes.withdraw(450, "New jacket");

clothes.transfer(150, entertainment);

clothes.transfer(20, entertainment);
clothes.transfer(5000, entertainment);
business.checkFunds(1000);
entertainment.checkFunds(10);
Loading