From e4915e836558b96cf8976154166343c0cc45b473 Mon Sep 17 00:00:00 2001 From: Saad Zetouny Date: Mon, 23 Nov 2026 22:56:03 +0100 Subject: [PATCH 1/2] finished the assignment --- finance-tracker/app.js | 3 ++ finance-tracker/data.js | 43 ++++++++++++++++- finance-tracker/finance.js | 97 ++++++++++++++++++++++++++++++++------ package-lock.json | 86 +++++++++++++++++++++++++++++++++ package.json | 24 ++++++++++ 5 files changed, 237 insertions(+), 16 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..573e1bd 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,6 @@ // This is the entrypoint for your application. // node app.js +import * as finance from "./finance.js"; +console.log(finance.printAllTransactions()); +console.log(finance.printAllTransactions()); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..fae1871 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,43 @@ // Place here the transaction data array. Use it in your application as needed. -const transactions = []; \ No newline at end of file +export const transactions = [ + { + id: 1, + type: "income", + category: "salary", + amount: 3000, + description: "Monthly salary", + date: "2025-01-15", + }, + { + id: 2, + type: "expense", + category: "transport", + amount: 60, + description: "Monthly train pass", + date: "2025-01-15", + }, + { + id: 3, + type: "expense", + category: "groceries", + amount: 85.5, + description: "Weekly grocery shopping", + date: "2025-01-17", + }, + { + id: 4, + type: "expense", + category: "rent", + amount: 1200, + description: "January apartment rent", + date: "2025-01-01", + }, + { + id: 5, + type: "income", + category: "freelance", + amount: 450, + description: "Website design project", + date: "2025-01-22", + }, +]; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..27b5dd6 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,94 @@ -function addTransaction(transaction) { - // TODO: Implement this function +import { transactions } from "./data.js"; +import chalk from "chalk"; + +export function addTransaction(transaction) { + transactions.push({ ...transaction }); +} + +export function getTotalIncome() { + let totalIncome = 0; + for (const transaction of transactions) { + if (transaction.type === "income") { + totalIncome += transaction.amount; + } + } + return chalk.green(totalIncome); } -function getTotalIncome() { - // TODO: Implement this function +export function getTotalExpenses() { + let totalExpenses = 0; + for (const transaction of transactions) { + if (transaction.type === "expense") { + totalExpenses += transaction.amount; + } + } + return chalk.red(totalExpenses); } -function getTotalExpenses() { - // TODO: Implement this function +export function getBalance() { + let balance = 0; + let index = 0; + + while (index < transactions.length) { + const { type, amount } = transactions[index]; + // Income increases the balance, anything else (expense) decreases it + if (type === "income") { + balance += amount; + } else { + balance -= amount; + } + index++; + } + return balance >= 0 ? chalk.cyan(balance) : chalk.red(balance); } -function getBalance() { - // TODO: Implement this function +export function getTransactionsByCategory(category) { + const matchingTransactions = []; + for (const transaction of transactions) { + if (transaction.category === category) { + matchingTransactions.push(transaction); + } + } + return matchingTransactions; } -function getTransactionsByCategory(category) { - // TODO: Implement this function +export function getLargestExpense() { + let largestExpenseAmount = 0; + let largestExpense; + + for (let transaction of transactions) { + if ( + transaction.type === "expense" && + largestExpenseAmount < transaction.amount + ) { + largestExpenseAmount = transaction.amount; + largestExpense = transaction.category; + } + } + return `${chalk.yellow(largestExpense)} (${chalk.red(largestExpenseAmount)})`; } -function getLargestExpense() { - // TODO: Implement this function +export function printAllTransactions() { + let finalMessage = "💰 PERSONAL FINANCE TRACKER 💰\n\nAll Transactions:\n"; + for (let transaction of transactions) { + finalMessage += `${transactions.indexOf(transaction) + 1}.`; + if (transaction.type === "income") { + finalMessage += `[INCOME] `; + } else { + finalMessage += `[EXPENSE] `; + } + finalMessage += `${chalk.yellow(transaction.category)} - €${transaction.type === "income" ? chalk.green(transaction.amount) : chalk.red(transaction.amount)} (${transaction.description}) +`; + } + return finalMessage; } -function printAllTransactions() { - // TODO: Implement this function -} \ No newline at end of file +export function printSummary() { + return `📊 FINANCIAL SUMMARY 📊 + Total Income: €${getTotalIncome()} + Total Expenses: €${getTotalExpenses()} + Current Balance: €${getBalance()} + + Largest Expense: ${getLargestExpense()} + Total Transactions: ${transactions.length}`; +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..055a260 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,86 @@ +{ + "name": "c56-core-week-4", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "c56-core-week-4", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "license": "MIT" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f8fb416 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "c56-core-week-4", + "version": "1.0.0", + "description": "The week 4 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-4-assignment", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/SaadZetouny/c56-core-week-4.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "module", + "bugs": { + "url": "https://github.com/SaadZetouny/c56-core-week-4/issues" + }, + "homepage": "https://github.com/SaadZetouny/c56-core-week-4#readme", + "dependencies": { + "chalk": "^4.1.2" + } +} From 3256c704c70ff7458a7054be6fca8c78103a88dc Mon Sep 17 00:00:00 2001 From: Saad Zetouny Date: Mon, 23 Nov 2026 23:07:13 +0100 Subject: [PATCH 2/2] added .prettierrc --- finance-tracker/.prettierrc | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 finance-tracker/.prettierrc diff --git a/finance-tracker/.prettierrc b/finance-tracker/.prettierrc new file mode 100644 index 0000000..ef485de --- /dev/null +++ b/finance-tracker/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": true, + "singleQuote": false +}