-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (72 loc) · 2.49 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using ExpenseTracker.Helper;
using ExpenseTracker.Manager;
using ExpenseTracker.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddSingleton<ExpenseManager>();
services.AddSingleton<BudgetManager>();
services.AddSingleton<CsvExporter>();
})
.Build();
var expenseManager = host.Services.GetRequiredService<ExpenseManager>();
var budgetManager = host.Services.GetRequiredService<BudgetManager>();
if (args.Length == 0)
{
CLIHelper.ShowHelp();
return;
}
switch (args[0].ToLower())
{
case "add":
string description = args[2];
decimal amount = Convert.ToDecimal(args[4]);
string category = args.Length > 6 ? args[6] : "General";
expenseManager.AddExpense(description, amount, category);
break;
case "delete":
int idToDelete = Convert.ToInt32(args[2]);
expenseManager.DeleteExpense(idToDelete);
break;
case "list":
string filterCategory = args.Length > 2 ? args[2] : null;
ListExpenses(expenseManager.GetExpensesByCategory(filterCategory));
break;
case "summary":
if (args.Length > 2 && args[2] == "--month")
{
int month = Convert.ToInt32(args[3]);
decimal total = expenseManager.GetTotalExpensesForMonth(month);
Console.WriteLine($"Total expenses for {DateTime.Now.ToString("MMMM")}: ${total}");
budgetManager.CheckBudget(total);
}
else
{
decimal total = expenseManager.GetTotalExpenses();
Console.WriteLine($"Total expenses: ${total}");
budgetManager.CheckBudget(total);
}
break;
case "set-budget":
decimal budget = Convert.ToDecimal(args[2]);
budgetManager.SetBudget(budget);
break;
case "export":
string csvFilePath = args[2];
var csvExporter = host.Services.GetRequiredService<CsvExporter>();
csvExporter.ExportToCsv(expenseManager.GetAllExpenses(), csvFilePath);
break;
default:
CLIHelper.ShowHelp();
break;
}
void ListExpenses(List<Expense> expenses)
{
Console.WriteLine("ID Date Description Amount Category");
foreach (var expense in expenses)
{
Console.WriteLine($"{expense.Id} {expense.Date.ToString("yyyy-MM-dd")} {expense.Description} ${expense.Amount} {expense.Category}");
}
}