From d811cdff03599bedc0f70cc664472797ad0ea786 Mon Sep 17 00:00:00 2001 From: Mareh Date: Wed, 4 Feb 2026 22:43:43 +0200 Subject: [PATCH] week4 --- .gitignore => finance-tracker/.gitignore | 0 finance-tracker/.prettierrc | 6 + finance-tracker/app.js | 1 - finance-tracker/data.js | 45 +++++++- finance-tracker/finance.js | 134 ++++++++++++++++++++++- finance-tracker/package-lock.json | 105 ++++++++++++++++++ finance-tracker/package.json | 27 +++++ finance-tracker/run.js | 4 + 8 files changed, 317 insertions(+), 5 deletions(-) rename .gitignore => finance-tracker/.gitignore (100%) create mode 100644 finance-tracker/.prettierrc create mode 100644 finance-tracker/package-lock.json create mode 100644 finance-tracker/package.json create mode 100644 finance-tracker/run.js diff --git a/.gitignore b/finance-tracker/.gitignore similarity index 100% rename from .gitignore rename to finance-tracker/.gitignore diff --git a/finance-tracker/.prettierrc b/finance-tracker/.prettierrc new file mode 100644 index 0000000..02a74ca --- /dev/null +++ b/finance-tracker/.prettierrc @@ -0,0 +1,6 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none" +} diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..4a75396 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,2 @@ // This is the entrypoint for your application. // node app.js - diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..9286836 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,45 @@ // Place here the transaction data array. Use it in your application as needed. -const transactions = []; \ No newline at end of file +const transactions = [ + { + id: 1, + type: 'income', + category: 'salary', + amount: 3000, + description: 'Monthly salary', + date: '2025-01-15' + }, + { + id: 2, + type: 'expense', + category: 'groceries', + amount: 1000, + description: 'Weekly groceries shopping', + date: '2025-01-16' + }, + { + id: 3, + type: 'expense', + category: 'rent', + amount: 1500, + description: 'Monthly rent payment', + date: '2025-01-30' + }, + { + id: 4, + type: 'income', + category: 'freelance', + amount: 500, + description: 'Freelance project payment', + date: '2025-01-20' + }, + { + id: 5, + type: 'expense', + category: 'entertainment', + amount: 75, + description: 'Movie tickets', + date: '2025-01-18' + } +]; + +module.exports = transactions; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..a901fc1 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,155 @@ +/***2. Required Functions** + +Implement these functions using appropriate loops and array methods: + +- `addTransaction(transaction)` - Add new transaction to array +- `getTotalIncome()` - Sum all income using a loop +- `getTotalExpenses()` - Sum all expenses using a loop +- `getBalance()` - Calculate total income minus expenses +- `getTransactionsByCategory(category)` - Filter transactions (use loop + push or indexOf/includes) +- `getLargestExpense()` - Find highest expense amount +- `printAllTransactions()` - Display all transactions with formatting*/ + +const transactions = require('./data.js'); +const chalk = require('chalk'); function addTransaction(transaction) { // TODO: Implement this function + const newTransaction = { + ...transaction, + id: transactions.length + 1 + }; + transactions.push(newTransaction); } function getTotalIncome() { // TODO: Implement this function + let total = 0; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].type === 'income') { + //total = transactions[i].amount + total; + total += transactions[i].amount; + } + } + return total; } function getTotalExpenses() { // TODO: Implement this function + let total = 0; + //for(const transaction of transactions) + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].type === 'expense') { + total = transactions[i].amount + total; + } + } + return total; } function getBalance() { // TODO: Implement this function + const Balance = getTotalIncome() - getTotalExpenses(); + return Balance; } function getTransactionsByCategory(category) { // TODO: Implement this function + const result = []; + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].category === category) { + result.push(transactions[i]); + } + } + return result; } function getLargestExpense() { - // TODO: Implement this function + let largest = null; + + for (let i = 0; i < transactions.length; i++) { + if (transactions[i].type === 'expense') { + if (largest === null || transactions[i].amount > largest.amount) { + largest = transactions[i]; + } + } + } + + return largest; } function printAllTransactions() { - // TODO: Implement this function -} \ No newline at end of file + console.log(chalk.cyan.bold('\n šŸ’° PERSONAL FINANCE TRACKER šŸ’°\n')); + + for (let i = 0; i < transactions.length; i++) { + const transaction = transactions[i]; + const { id, type, description, amount, category } = transaction; + const typeDisplay = + type === 'income' + ? chalk.green.bold('[income]') + : chalk.red.bold('[expense]'); + + const amountDisplay = + type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`); + + console.log( + `${chalk.yellow(id)}. ${typeDisplay} ${description} - ${amountDisplay} ` + + `(${chalk.yellow(category)})` + ); + } +} + +function printSummary() { + const totalIncome = getTotalIncome(); + const totalExpenses = getTotalExpenses(); + const balance = getBalance(); + const largestExpense = getLargestExpense(); + const transactionCount = transactions.length; + + console.log(chalk.cyan.bold('\nšŸ“Š FINANCIAL SUMMARY šŸ“Š\n')); + + console.log(chalk.bold(`Total Income: ${chalk.green(`€${totalIncome}`)}`)); + + console.log(chalk.bold(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`)); + + const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; + console.log(chalk.bold(`Current Balance: ${balanceColor(`€${balance}`)}`)); + + if (largestExpense) { + console.log( + chalk.bold( + `Largest Expense: ${largestExpense.description} (` + + `${chalk.red(`€${largestExpense.amount}`)})` + ) + ); + } + + console.log(chalk.bold(`Total Transactions : ${transactionCount}`)); +} + +function removeTransaction(id) { + let indexToRemove = -1; + let i = 0; + + while (i < transactions.length) { + if (transactions[i].id === id) { + indexToRemove = i; + break; + } + i++; + } + + if (indexToRemove !== -1) { + return transactions.splice(indexToRemove, 1)[0]; + } + return null; +} +module.exports = { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions, + printSummary, + removeTransaction +}; diff --git a/finance-tracker/package-lock.json b/finance-tracker/package-lock.json new file mode 100644 index 0000000..228d484 --- /dev/null +++ b/finance-tracker/package-lock.json @@ -0,0 +1,105 @@ +{ + "name": "c55-core-week-4", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "c55-core-week-4", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } + }, + "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/prettier": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", + "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "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/finance-tracker/package.json b/finance-tracker/package.json new file mode 100644 index 0000000..839cc67 --- /dev/null +++ b/finance-tracker/package.json @@ -0,0 +1,27 @@ +{ + "name": "c55-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/mareh-aboghanem/c55-core-week-4.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "bugs": { + "url": "https://github.com/mareh-aboghanem/c55-core-week-4/issues" + }, + "homepage": "https://github.com/mareh-aboghanem/c55-core-week-4#readme", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } +} diff --git a/finance-tracker/run.js b/finance-tracker/run.js new file mode 100644 index 0000000..98589ce --- /dev/null +++ b/finance-tracker/run.js @@ -0,0 +1,4 @@ +const { printAllTransactions, printSummary } = require('./finance.js'); + +printAllTransactions(); +printSummary();