-
Notifications
You must be signed in to change notification settings - Fork 19
Hamed R. #13
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
base: main
Are you sure you want to change the base?
Hamed R. #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "singleQuote": true, | ||
| "semi": true, | ||
| "trailingComma": "es5" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,83 @@ | ||
| // This is the entrypoint for your application. | ||
| // This is the entrypoint for your application | ||
| // node app.js | ||
| const chalk = require('chalk'); | ||
| const transactions = require('./data'); | ||
| const { | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small note 🙂
|
||
| getLargestExpense, | ||
| getTransactionsByDateRange, | ||
| averageExpensePerCategory, | ||
| } = require('./finance'); | ||
|
|
||
| // Display all transactions | ||
| function printAllTransactions() { | ||
| console.log(chalk.bold('\n💰 PERSONAL FINANCE TRACKER 💰\n')); | ||
| console.log('All Transactions:'); | ||
|
|
||
| transactions.forEach((t, i) => { | ||
| const label = | ||
| t.type === 'income' ? chalk.green('[INCOME]') : chalk.red('[EXPENSE]'); | ||
| const amountColor = | ||
| t.type === 'income' | ||
| ? chalk.green(`€${t.amount}`) | ||
| : chalk.red(`€${t.amount}`); | ||
| console.log( | ||
| `${i + 1}. ${label} ${t.description} - ${amountColor} (${chalk.yellow(t.category)})` | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| // Display transactions by date range | ||
| function printTransactionsByDateRange(start, end) { | ||
| const results = getTransactionsByDateRange(start, end); | ||
| console.log(chalk.bold(`\nTransactions from ${start} to ${end}:`)); | ||
|
|
||
| results.forEach((t, i) => { | ||
| const label = | ||
| t.type === 'income' ? chalk.green('[INCOME]') : chalk.red('[EXPENSE]'); | ||
| const amountColor = | ||
| t.type === 'income' | ||
| ? chalk.green(`€${t.amount}`) | ||
| : chalk.red(`€${t.amount}`); | ||
| console.log( | ||
| `${i + 1}. ${label} ${t.description} - ${amountColor} (${chalk.yellow(t.category)})` | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| // Display summary | ||
| function printSummary() { | ||
| const income = getTotalIncome(); | ||
| const expenses = getTotalExpenses(); | ||
| const balance = getBalance(); | ||
| const largestExpense = getLargestExpense(); | ||
|
|
||
| console.log(chalk.bold('\n📊 FINANCIAL SUMMARY 📊')); | ||
| console.log(chalk.bold(`Total Income: €${income}`)); | ||
| console.log(chalk.bold(`Total Expenses: €${expenses}`)); | ||
|
|
||
| const balanceText = | ||
| balance >= 0 ? chalk.cyan(`€${balance}`) : chalk.red(`€${balance}`); | ||
| console.log(chalk.bold('Current Balance: '), balanceText); | ||
|
|
||
| if (largestExpense) { | ||
| console.log( | ||
| `\nLargest Expense: ${largestExpense.description} (€${largestExpense.amount})` | ||
| ); | ||
| } | ||
|
|
||
| console.log(`Total Transactions: ${transactions.length}`); | ||
|
|
||
| const averages = averageExpensePerCategory(); | ||
| console.log(chalk.bold('\nAverage Expense per Category:')); | ||
| for (const category in averages) { | ||
| console.log(`${chalk.yellow(category)}: €${averages[category].toFixed(2)}`); | ||
| } | ||
| } | ||
|
|
||
| printAllTransactions(); | ||
| printSummary(); | ||
| printTransactionsByDateRange('2025-01-01', '2025-01-25'); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,101 @@ | ||
| // Place here the transaction data array. Use it in your application as needed. | ||
| const transactions = []; | ||
| const transactions = [ | ||
| { | ||
| id: 1, | ||
| type: 'income', | ||
| category: 'salary', | ||
| amount: 3000, | ||
| description: 'Salary', | ||
| date: '2025-01-15', | ||
| }, | ||
| { | ||
| id: 2, | ||
| type: 'expense', | ||
| category: 'housing', | ||
| amount: 1200, | ||
| description: 'Rent', | ||
| date: '2025-01-01', | ||
| }, | ||
| { | ||
| id: 3, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 450, | ||
| description: 'Groceries', | ||
| date: '2025-01-10', | ||
| }, | ||
| { | ||
| id: 4, | ||
| type: 'income', | ||
| category: 'side-income', | ||
| amount: 700, | ||
| description: 'Freelance', | ||
| date: '2025-01-20', | ||
| }, | ||
| { | ||
| id: 5, | ||
| type: 'expense', | ||
| category: 'bills', | ||
| amount: 300, | ||
| description: 'Utilities', | ||
| date: '2025-01-05', | ||
| }, | ||
| { | ||
| id: 6, | ||
| type: 'expense', | ||
| category: 'transport', | ||
| amount: 120, | ||
| description: 'Public transport', | ||
| date: '2025-01-12', | ||
| }, | ||
| { | ||
| id: 7, | ||
| type: 'expense', | ||
| category: 'entertainment', | ||
| amount: 200, | ||
| description: 'Cinema & dinner', | ||
| date: '2025-01-18', | ||
| }, | ||
| { | ||
| id: 8, | ||
| type: 'income', | ||
| category: 'bonus', | ||
| amount: 400, | ||
| description: 'Performance bonus', | ||
| date: '2025-01-25', | ||
| }, | ||
| { | ||
| id: 9, | ||
| type: 'expense', | ||
| category: 'health', | ||
| amount: 90, | ||
| description: 'Pharmacy', | ||
| date: '2025-01-08', | ||
| }, | ||
| { | ||
| id: 10, | ||
| type: 'expense', | ||
| category: 'shopping', | ||
| amount: 250, | ||
| description: 'Clothes', | ||
| date: '2025-01-22', | ||
| }, | ||
| { | ||
| id: 11, | ||
| type: 'income', | ||
| category: 'gift', | ||
| amount: 150, | ||
| description: 'Birthday gift', | ||
| date: '2025-01-28', | ||
| }, | ||
| { | ||
| id: 12, | ||
| type: 'expense', | ||
| category: 'food', | ||
| amount: 80, | ||
| description: 'Restaurant', | ||
| date: '2025-01-27', | ||
| }, | ||
| ]; | ||
|
|
||
| module.exports = transactions; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,91 @@ | ||
| const transactions = require('./data'); | ||
|
|
||
| // Add new transaction | ||
| function addTransaction(transaction) { | ||
| // TODO: Implement this function | ||
| transactions.push({ ...transaction }); | ||
| } | ||
|
|
||
| // Total income | ||
| function getTotalIncome() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'income') total += transaction.amount; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| // Total expenses | ||
| function getTotalExpenses() { | ||
| // TODO: Implement this function | ||
| let total = 0; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') total += transaction.amount; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| // Current balance | ||
| function getBalance() { | ||
| // TODO: Implement this function | ||
| return getTotalIncome() - getTotalExpenses(); | ||
| } | ||
|
|
||
| // Filter transactions by category | ||
| function getTransactionsByCategory(category) { | ||
| // TODO: Implement this function | ||
| const result = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.category.includes(category)) result.push(transaction); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // Find largest expense | ||
| function getLargestExpense() { | ||
| // TODO: Implement this function | ||
| let largest = null; | ||
| for (const transaction of transactions) { | ||
| if ( | ||
| transaction.type === 'expense' && | ||
| (!largest || transaction.amount > largest.amount) | ||
| ) { | ||
| largest = transaction; | ||
| } | ||
| } | ||
| return largest; | ||
| } | ||
|
Comment on lines
41
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice implementation 👍. |
||
|
|
||
| // Search by date range | ||
| function getTransactionsByDateRange(start, end) { | ||
| const result = []; | ||
| for (const transaction of transactions) { | ||
| if (transaction.date >= start && transaction.date <= end) | ||
| result.push(transaction); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // Average expense per category | ||
| function averageExpensePerCategory() { | ||
| const totals = {}; | ||
| const counts = {}; | ||
| for (const transaction of transactions) { | ||
| if (transaction.type === 'expense') { | ||
| totals[transaction.category] = | ||
| (totals[transaction.category] || 0) + transaction.amount; | ||
| counts[transaction.category] = (counts[transaction.category] || 0) + 1; | ||
| } | ||
| } | ||
| const averages = {}; | ||
| for (const category in totals) { | ||
| averages[category] = totals[category] / counts[category]; | ||
| } | ||
| return averages; | ||
| } | ||
|
|
||
| function printAllTransactions() { | ||
| // TODO: Implement this function | ||
| } | ||
| module.exports = { | ||
| addTransaction, | ||
| getTotalIncome, | ||
| getTotalExpenses, | ||
| getBalance, | ||
| getTransactionsByCategory, | ||
| getLargestExpense, | ||
| getTransactionsByDateRange, | ||
| averageExpensePerCategory, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line could be deleted.