Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (118 loc) · 3.8 KB
/
Copy pathProgram.cs
File metadata and controls
118 lines (118 loc) · 3.8 KB
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
using System;
using System.Collections.Generic;
namespace ExpenseTrackingModule
{
class Expense
{
public string Category { get; set; }
public double Amount { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public Expense(string category, double amount, string description)
{
Category = category;
Amount = amount;
Description = description;
Date = DateTime.Now; // Store Current Date & Time
}
}
class Program
{
static List<Expense> expenses = new List<Expense>();
static void Main(string[] args)
{
int choice = 0;
do
{
Console.WriteLine("\n========== Expense Tracking Module ==========");
Console.WriteLine("1. Add Expense");
Console.WriteLine("2. View Expenses");
Console.WriteLine("3. Show Total Expense");
Console.WriteLine("4. Exit");
Console.Write("Enter your choice: ");
try
{
choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
AddExpense();
break;
case 2:
ViewExpenses();
break;
case 3:
ShowTotalExpense();
break;
case 4:
Console.WriteLine("Thank You!");
break;
default:
Console.WriteLine("Invalid Choice! Please enter 1 to 4.");
break;
}
}
catch (FormatException)
{
Console.WriteLine("Error: Please enter a valid numeric choice.");
}
catch (Exception ex)
{
Console.WriteLine("Unexpected Error: " + ex.Message);
}
} while (choice != 4);
}
static void AddExpense()
{
try
{
Console.Write("Enter Category: ");
string category = Console.ReadLine();
Console.Write("Enter Amount: ");
double amount = Convert.ToDouble(Console.ReadLine());
if (amount <= 0)
{
throw new Exception("Amount must be greater than zero.");
}
Console.Write("Enter Description: ");
string description = Console.ReadLine();
expenses.Add(new Expense(category, amount, description));
Console.WriteLine("Expense Added Successfully!");
}
catch (FormatException)
{
Console.WriteLine("Invalid Amount! Please enter numeric value.");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
static void ViewExpenses()
{
if (expenses.Count == 0)
{
Console.WriteLine("No expenses found.");
return;
}
Console.WriteLine("\n================ Expense List ================");
Console.WriteLine("Category\tAmount\tDescription\tDate & Time");
Console.WriteLine("---------------------------------------------------------------");
foreach (Expense expense in expenses)
{
Console.WriteLine(
$"{expense.Category}\t\t{expense.Amount}\t{expense.Description}\t{expense.Date:dd-MM-yyyy hh:mm:ss tt}"
);
}
}
static void ShowTotalExpense()
{
double total = 0;
foreach (Expense expense in expenses)
{
total += expense.Amount;
}
Console.WriteLine("\nTotal Expense = " + total);
}
}
}