diff --git a/finance-tracker/.gitignore b/finance-tracker/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/finance-tracker/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/finance-tracker/.prettierrc b/finance-tracker/.prettierrc new file mode 100644 index 0000000..b0ec5b3 --- /dev/null +++ b/finance-tracker/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "es5", + "printWidth": 80 +} diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..1f959eb 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,14 @@ -// This is the entrypoint for your application. -// node app.js +import { transactions } from './data.js'; +import { + addTransaction, + getTotalIncome, + getTotalExpenses, + getBalance, + getTransactionsByCategory, + getLargestExpense, + printAllTransactions, + printSummary, +} from './finance.js'; +printAllTransactions(transactions); +printSummary(transactions); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..f68fd10 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,84 @@ -// 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: 'salary', + date: '2025-12-28', + }, + { + id: 2, + type: 'expense', + category: 'rent', + amount: 800, + description: 'housing', + date: '2026-01-01', + }, + { + id: 3, + type: 'expense', + category: 'utility', + amount: 250, + description: 'bills', + date: '2026-01-16', + }, + { + id: 4, + type: 'expense', + category: 'groceries', + amount: 180, + description: 'food', + date: '2026-01-26', + }, + { + id: 5, + type: 'income', + category: 'benefits', + amount: 200, + description: 'huurtoeslag', + date: '2026-01-20', + }, + { + id: 6, + type: 'expense', + category: 'insurance', + amount: 210, + description: 'bills', + date: '2026-01-24', + }, + { + id: 7, + type: 'income', + category: 'salary', + amount: 350, + description: 'freelance', + date: '2026-01-19', + }, + { + id: 8, + type: 'expense', + category: 'transport', + amount: 80, + description: 'bills', + date: '2026-01-30', + }, + { + id: 9, + type: 'expense', + category: 'entertainment', + amount: 60, + description: 'pc-games', + date: '2026-01-01', + }, + { + id: 10, + type: 'expense', + category: '', + amount: 200, + description: '', + date: '2026-01-20', + }, +]; + +export default transactions; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..811d564 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,85 @@ -function addTransaction(transaction) { - // TODO: Implement this function +import transactions from `./data.js`; +import chalk from 'chalk'; + +export function addTransaction(transaction) { + transactions.push({ ...transaction }); } -function getTotalIncome() { - // TODO: Implement this function +export function getTotalIncome(transactions) { + let totalIncome = 0; + for (const transaction of transactions) { + if (transaction.type === `income`) { + totalIncome += transaction.amount; + } + } + return totalIncome; } -function getTotalExpenses() { - // TODO: Implement this function +export function getTotalExpenses(transactions) { + let totalExpenses = 0; + for (const transaction of transactions) { + if (transaction.type === `expense`) { + totalExpenses += transaction.amount; + } + } + return totalExpenses; } -function getBalance() { - // TODO: Implement this function +export function getBalance() { + return getTotalIncome() - getTotalExpenses(); } -function getTransactionsByCategory(category) { - // TODO: Implement this function +export function getTransactionsByCategory(transactions, category) { + const result = []; + for (const transaction of transactions) { + if (transaction.category === category) { + result.push(transaction); + } + } + return result; } -function getLargestExpense() { - // TODO: Implement this function + +export function getLargestExpense() { + let largest = null; + for (const transaction of transactions) { + if (transaction.type === 'expense' && (!largest || transaction.amount > largest.amount)) { + largest = transaction; + } + } + return largest; } -function printAllTransactions() { - // TODO: Implement this function +export function printAllTransactions(transactions) { + console.log(chalk.bold.cyan('PERSONAL FINANCE TRACKER\n')); + console.log(chalk.bold('All Transactions:\n')); + + transactions.forEach((transaction, index) => { + const { id, type, category, amount, description } = transaction; + const typeLabel = type === 'income' ? '[INCOME]' : '[EXPENSE]'; + const coloredAmount = + type === 'income' ? chalk.green(`€${amount}`) : chalk.red(`€${amount}`); + const coloredCategory = chalk.yellow(`(${category})`); + + console.log(`${index + 1}. ${typeLabel} ${description} - ${coloredAmount} ${coloredCategory}`); + }); + +export function printSummary(transactions) { + const totalIncome = getTotalIncome(transactions); + const totalExpenses = getTotalExpenses(transactions); + const balance = getBalance(transactions); + const largestExpense = getLargestExpense(transactions); + const transactionCount = transactions.length; + + console.log(chalk.bold.cyan('FINANCIAL SUMMARY\n')); + console.log(`Total Income: ${chalk.green(`€${totalIncome}`)}`); + console.log(`Total Expenses: ${chalk.red(`€${totalExpenses}`)}`); + const balanceColor = balance >= 0 ? chalk.cyan : chalk.red; + console.log(`Current Balance: ${balanceColor(`€${balance}`)}`); + + if (largestExpense) { + console.log( + `\nLargest Expense: ${largestExpense.description} (${chalk.red(`€${largestExpense.amount}`)})`); + } + console.log(`Total Transactions: ${chalk.bold(transactionCount)}\n`); } \ No newline at end of file diff --git a/finance-tracker/package-lock.json b/finance-tracker/package-lock.json new file mode 100644 index 0000000..f93f7a6 --- /dev/null +++ b/finance-tracker/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "hyf-assignment", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "hyf-assignment", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^5.6.2" + } + }, + "node_modules/chalk": { + "version": "5.6.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", + "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/finance-tracker/package.json b/finance-tracker/package.json new file mode 100644 index 0000000..c35fb09 --- /dev/null +++ b/finance-tracker/package.json @@ -0,0 +1,16 @@ +{ + "name": "hyf-assignment", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "chalk": "^5.6.2" + } +}