diff --git a/finance-tracker/.gitignore b/finance-tracker/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/finance-tracker/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/finance-tracker/.prettierrc b/finance-tracker/.prettierrc new file mode 100644 index 0000000..1efd31a --- /dev/null +++ b/finance-tracker/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": true, + "singleQuote": true +} diff --git a/finance-tracker/app.js b/finance-tracker/app.js index 7cfcd07..2d817ae 100644 --- a/finance-tracker/app.js +++ b/finance-tracker/app.js @@ -1,3 +1,33 @@ -// This is the entrypoint for your application. -// node app.js +import { + addTransaction, + getLargestExpense, + printAllTransactions, + printSummary, + getTotalTransactions, +} from './finance.js'; +import chalk from 'chalk'; + +console.log(chalk.bold(' PERSONAL FINANCE TRACKER \n')); + +const transaction = { + type: 'expense', + category: 'Personal Care', + amount: 20, + description: 'baber shop', + date: '2025-01-30', +}; + +addTransaction(transaction); + +printAllTransactions(); +printSummary(); + +const largestExpense = getLargestExpense(); +console.log( + chalk.bold( + `\nLargest Expense: ${largestExpense.category} (€${largestExpense.amount})`, + ), +); + +console.log(chalk.bold(`Total Transactions: ${getTotalTransactions()}`)); diff --git a/finance-tracker/data.js b/finance-tracker/data.js index d7863ff..94fac0b 100644 --- a/finance-tracker/data.js +++ b/finance-tracker/data.js @@ -1,2 +1,51 @@ // 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-02', + }, + { + id: 2, + type: 'expense', + category: 'Rent', + amount: 1200, + description: 'Monthly rent', + date: '2025-01-05', + }, + { + id: 3, + type: 'expense', + category: 'Groceries', + amount: 300, + description: 'food', + date: '2025-01-12', + }, + { + id: 4, + type: 'income', + category: 'Freelance', + amount: 500, + description: 'side-income', + date: '2025-01-15', + }, + { + id: 5, + type: 'expense', + category: 'Utilities', + amount: 150, + description: 'bills', + date: '2025-01-20', + }, + { + id: 6, + type: 'income', + category: 'Freelance', + amount: 150, + description: 'side-income', + date: '2025-01-25', + }, +]; diff --git a/finance-tracker/finance.js b/finance-tracker/finance.js index ac2118f..eb2ae67 100644 --- a/finance-tracker/finance.js +++ b/finance-tracker/finance.js @@ -1,27 +1,104 @@ -function addTransaction(transaction) { - // TODO: Implement this function +import { transactions } from './data.js'; +import chalk from 'chalk'; + +export function addTransaction(transaction) { + if (!transaction.id) { + transaction.id = transactions.length + 1; + } + transactions.push(transaction); + return transactions; +} + +export function getTotalIncome() { + let totalIncome = 0; + for (const transaction of transactions) { + if (transaction.type === 'income') { + totalIncome += transaction.amount; + } + } + return 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 totalExpenses; } -function getTotalExpenses() { - // TODO: Implement this function +export function getBalance() { + return getTotalIncome() - getTotalExpenses(); } -function getBalance() { - // TODO: Implement this function +export function getTransactionsByCategory(category) { + let filteredTransactions = []; + for (const transaction of transactions) { + if (transaction.category === category) { + filteredTransactions.push(transaction); + } + } + return filteredTransactions; } -function getTransactionsByCategory(category) { - // TODO: Implement this function +export function getLargestExpense() { + let largest = null; + for (const transaction of transactions) { + if (transaction.type === 'expense') { + if (largest === null || transaction.amount > largest.amount) { + largest = transaction; + } + } + } + return largest; } -function getLargestExpense() { - // TODO: Implement this function +export function printAllTransactions() { + console.log('All Transactions:'); + let id = 1; + for (const transaction of transactions) { + const { type, category, amount, description } = transaction; + + // To determine the income or expense, and to set a proper color for display + let amountColor = null; + if (transaction.type === 'income') { + amountColor = chalk.green(`€${amount}`); + } else { + amountColor = chalk.red(`€${amount}`); + } + + console.log( + `${id}. [${type.toUpperCase()}] ${chalk.yellow(category)} - ${amountColor} (${description})`, + ); + id++; + } + + console.log('\n'); } -function printAllTransactions() { - // TODO: Implement this function -} \ No newline at end of file +export function printSummary() { + console.log(chalk.bold('FINANCIAL SUMMARY')); + console.log(chalk.green(`Total Income: €${getTotalIncome()}`)); + console.log(chalk.red(`Total Expenses: €${getTotalExpenses()}`)); + if (getBalance() >= 0) { + console.log(chalk.cyan(`Current Balance: €${getBalance()}`)); + } else { + console.log(chalk.red(`Current Balance: €${getBalance()}`)); + } +} + +export function getTransactionsByType(type) { + let filteredTransactions = []; + for (const transaction of transactions) { + if (transaction.type === type) { + filteredTransactions.push(transaction); + } + } + return filteredTransactions; +} + +export function getTotalTransactions() { + return transactions.length; +} diff --git a/finance-tracker/package-lock.json b/finance-tracker/package-lock.json new file mode 100644 index 0000000..148aad2 --- /dev/null +++ b/finance-tracker/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "finance-tracker", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "finance-tracker", + "version": "1.0.0", + "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..c552168 --- /dev/null +++ b/finance-tracker/package.json @@ -0,0 +1,13 @@ +{ + "name": "finance-tracker", + "version": "1.0.0", + "description": "", + "main": "app.js", + "type": "module", + "scripts": { + "start": "node ./app.js" + }, + "dependencies": { + "chalk": "^5.6.2" + } +}