-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
179 lines (160 loc) · 5.52 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Expense_Tracker;
using Newtonsoft.Json;
class Program
{
private static readonly string expenseFile = "expenses.json";
static void Main(string[] args)
{
bool exit = false;
while (!exit)
{
Console.WriteLine("\nExpense Tracker Commands:");
Console.WriteLine("1. Add Expense");
Console.WriteLine("2. Update Expense");
Console.WriteLine("3. Delete Expense");
Console.WriteLine("4. List Expenses");
Console.WriteLine("5. Show Summary");
Console.WriteLine("6. Show Summary for Month");
Console.WriteLine("0. Exit");
Console.Write("\nSelect a command: ");
var command = Console.ReadLine();
switch (command)
{
case "1":
AddExpense();
break;
case "2":
UpdateExpense();
break;
case "3":
DeleteExpense();
break;
case "4":
ListExpenses();
break;
case "5":
ShowSummary();
break;
case "6":
ShowSummaryForMonth();
break;
case "0":
exit = true;
Console.WriteLine("Exiting...");
break;
default:
Console.WriteLine("Invalid command. Try again.");
break;
}
}
}
static void AddExpense()
{
Console.Write("Enter description: ");
string description = Console.ReadLine();
Console.Write("Enter amount: ");
if (!decimal.TryParse(Console.ReadLine(), out decimal amount) || amount < 0)
{
Console.WriteLine("Invalid amount. Try again.");
return;
}
var expenses = LoadExpenses();
int newId = expenses.Count > 0 ? expenses.Max(e => e.Id) + 1 : 1;
expenses.Add(new Expense { Id = newId, Description = description, Amount = amount, Date = DateTime.Now });
SaveExpenses(expenses);
Console.WriteLine($"Expense added successfully (ID: {newId})");
}
static void UpdateExpense()
{
Console.Write("Enter expense ID to update: ");
if (!int.TryParse(Console.ReadLine(), out int id))
{
Console.WriteLine("Invalid ID. Try again.");
return;
}
var expenses = LoadExpenses();
var expense = expenses.FirstOrDefault(e => e.Id == id);
if (expense != null)
{
Console.Write("Enter new description: ");
string description = Console.ReadLine();
Console.Write("Enter new amount: ");
if (!decimal.TryParse(Console.ReadLine(), out decimal amount) || amount < 0)
{
Console.WriteLine("Invalid amount. Try again.");
return;
}
expense.Description = description;
expense.Amount = amount;
SaveExpenses(expenses);
Console.WriteLine($"Expense (ID: {id}) updated successfully.");
}
else
{
Console.WriteLine("Expense not found.");
}
}
static void DeleteExpense()
{
Console.Write("Enter expense ID to delete: ");
if (!int.TryParse(Console.ReadLine(), out int id))
{
Console.WriteLine("Invalid ID. Try again.");
return;
}
var expenses = LoadExpenses();
var expense = expenses.FirstOrDefault(e => e.Id == id);
if (expense != null)
{
expenses.Remove(expense);
SaveExpenses(expenses);
Console.WriteLine("Expense deleted successfully.");
}
else
{
Console.WriteLine("Expense not found.");
}
}
static void ListExpenses()
{
var expenses = LoadExpenses();
Console.WriteLine($"{"ID",-5} {"Date",-12} {"Description",-20} {"Amount",10}");
foreach (var expense in expenses)
{
Console.WriteLine($"{expense.Id,-5} {expense.Date.ToShortDateString(),-12} {expense.Description,-20} {expense.Amount,10:C}");
}
}
static void ShowSummary()
{
var expenses = LoadExpenses();
decimal totalExpenses = expenses.Sum(e => e.Amount);
Console.WriteLine($"Total expenses: {totalExpenses:C}");
}
static void ShowSummaryForMonth()
{
Console.Write("Enter month (1-12): ");
if (!int.TryParse(Console.ReadLine(), out int month) || month < 1 || month > 12)
{
Console.WriteLine("Invalid month. Try again.");
return;
}
var expenses = LoadExpenses();
var filteredExpenses = expenses.Where(e => e.Date.Month == month && e.Date.Year == DateTime.Now.Year).ToList();
decimal totalMonthExpenses = filteredExpenses.Sum(e => e.Amount);
Console.WriteLine($"Total expenses for month {month}: {totalMonthExpenses:C}");
}
static List<Expense> LoadExpenses()
{
if (!File.Exists(expenseFile))
{
return [];
}
var json = File.ReadAllText(expenseFile);
return JsonConvert.DeserializeObject<List<Expense>>(json) ?? [];
}
static void SaveExpenses(List<Expense> expenses)
{
var json = JsonConvert.SerializeObject(expenses, Formatting.Indented);
File.WriteAllText(expenseFile, json);
}
}