-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersonalExpense.cpp
355 lines (289 loc) · 10.5 KB
/
PersonalExpense.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
struct User {
std::string firstName;
std::string lastName;
std::string nickName;
std::string phNumber;
std::string emailId;
std::string password;
};
struct Expense {
double amount;
std::string category;
std::string date;
std::string notes;
};
void displayMenu();
void signUp();
void logIn();
void addExpense();
void editExpense();
void deleteExpense();
void generateReport();
std::vector<Expense> readExpenses();
void displayExpenses(const std::vector<Expense>& expenses);
int selectExpenseToEdit(const std::vector<Expense>& expenses);
void editExpenseDetails(Expense& expense);
void writeExpenses(const std::vector<Expense>& expenses);
void removeExpense(int index, std::vector<Expense>& expenses);
std::vector<User> users; // Vector to store multiple users
std::vector<Expense> expenses; // Vector to store multiple expenses
int main() {
int choice;
while (true) {
displayMenu();
std::cin >> choice;
switch (choice) {
case 1:
signUp();
break;
case 2:
logIn();
break;
case 3:
addExpense();
break;
case 4:
editExpense();
break;
case 5:
deleteExpense();
break;
case 6:
generateReport();
break;
case 7:
std::cout << "Exiting from the program." << std::endl;
return 0;
default:
std::cerr << "Invalid input!!!" << std::endl;
}
}
}
void displayMenu() {
std::cout << "------Personal Expenses Tracker------" << std::endl;
std::cout << "1. Sign Up" << std::endl;
std::cout << "2. Log In" << std::endl;
std::cout << "3. Add Expense" << std::endl;
std::cout << "4. Edit Expense" << std::endl;
std::cout << "5. Delete Expense" << std::endl;
std::cout << "6. Generate Report" << std::endl;
std::cout << "7. Exit" << std::endl;
std::cout << std::endl;
std::cout << "Enter your choice: ";
}
void signUp() {
std::cout << "Sign up function called" << std::endl;
std::cout << std::endl;
std::string firstName, lastName, nickName, phNumber, emailId, password, confirmPassword;
std::cout << "Enter your First Name: ";
std::cin >> firstName;
std::cout << "Enter your Last Name: ";
std::cin >> lastName;
std::cout << "Enter your Nick Name: ";
std::cin >> nickName;
std::cout << "Enter your Phone Number: ";
std::cin >> phNumber;
std::cout << "Enter your Email Id: ";
std::cin >> emailId;
// Checking valid Email ID
if (emailId.find('@') == std::string::npos || emailId.find('.') == std::string::npos) {
std::cerr << "Invalid email format!" << std::endl;
return;
}
std::cout << "Enter your Password: ";
std::cin >> password;
std::cout << "Confirm your Password: ";
std::cin >> confirmPassword;
// Checking if passwords match
if (password != confirmPassword) {
std::cerr << "Passwords do not match!" << std::endl;
return;
}
// Create a new User and add it to the users vector
User newUser = {firstName, lastName, nickName, phNumber, emailId, password};
users.push_back(newUser);
std::cout << "User registered successfully!" << std::endl;
}
void logIn() {
std::cout << "Log In function called." << std::endl;
std::cout << std::endl;
std::cout << "Enter any one of these:" << std::endl;
std::cout << "1. Nick Name" << std::endl;
std::cout << "2. Email ID" << std::endl;
std::cout << "3. Phone Number" << std::endl;
int choice;
std::cin >> choice;
bool userFound = false;
std::string logName, logEmail, logPhone, logPassword;
if (choice == 1) {
std::cout << "Enter your Nick Name: ";
std::cin >> logName;
} else if (choice == 2) {
std::cout << "Enter your Email Id: ";
std::cin >> logEmail;
} else if (choice == 3) {
std::cout << "Enter your phone number: ";
std::cin >> logPhone;
} else {
std::cerr << "Invalid choice!" << std::endl;
return;
}
std::cout << "Enter password: ";
std::cin >> logPassword;
// Check credentials
for (const auto& user : users) {
if ((choice == 1 && user.nickName == logName) || (choice == 2 && user.emailId == logEmail) || (choice == 3 && user.phNumber == logPhone)) {
if (user.password == logPassword) {
std::cout << "Login successful. Welcome " << user.firstName << "!" << std::endl;
userFound = true;
break;
} else {
std::cerr << "Incorrect Password. Try Again!!" << std::endl;
}
}
}
if (!userFound) {
std::cerr << "User not found!" << std::endl;
}
}
void addExpense() {
Expense newExpense;
std::cout << "Enter your Expense amount: ";
std::cin >> newExpense.amount;
std::cout << "Enter Category: ";
std::cin >> newExpense.category;
std::cout << "Enter Date (DD/MM/YYYY): ";
std::cin >> newExpense.date;
std::cout << "Enter note (Optional): ";
std::cin.ignore(); // Ignore the newline character from previous input
std::getline(std::cin, newExpense.notes);
if (newExpense.amount <= 0) {
std::cerr << "Expense must be a positive number!!" << std::endl;
return;
}
expenses.push_back(newExpense);
std::ofstream outFile("expenses.csv", std::ios::app); // Open file in append mode
if (outFile.is_open()) {
outFile << newExpense.amount << "," << newExpense.category << ","
<< newExpense.date << "," << newExpense.notes << "\n";
outFile.close();
std::cout << "Expense added successfully and saved to expenses.csv." << std::endl;
} else {
std::cerr << "Error: Unable to open expenses.csv for writing." << std::endl;
}
}
std::vector<Expense> readExpenses() {
std::vector<Expense> expenses;
std::ifstream inFile("expenses.csv");
std::string line;
while (std::getline(inFile, line)) {
std::istringstream ss(line);
std::string amountStr, category, date, notes;
std::getline(ss, amountStr, ',');
std::getline(ss, category, ',');
std::getline(ss, date, ',');
std::getline(ss, notes, ',');
Expense expense;
expense.amount = std::stod(amountStr);
expense.category = category;
expense.date = date;
expense.notes = notes;
expenses.push_back(expense);
}
return expenses;
}
void displayExpenses(const std::vector<Expense>& expenses) {
for (size_t i = 0; i < expenses.size(); ++i) {
std::cout << i + 1 << ". " << expenses[i].amount << ", " << expenses[i].category << ", "
<< expenses[i].date << ", " << expenses[i].notes << std::endl;
}
}
int selectExpenseToEdit(const std::vector<Expense>& expenses) {
int choice;
displayExpenses(expenses);
std::cout << "Enter the number of the expense you want to edit/delete: ";
std::cin >> choice;
return choice - 1; // Convert to zero-based index
}
void editExpenseDetails(Expense& expense) {
std::cout << "Editing expense: " << expense.amount << ", " << expense.category << ", "
<< expense.date << ", " << expense.notes << std::endl;
std::cout << "Enter new amount: ";
std::cin >> expense.amount;
std::cout << "Enter new category: ";
std::cin.ignore(); // Ignore any leftover newline character in the buffer
std::getline(std::cin, expense.category);
std::cout << "Enter new date: ";
std::getline(std::cin, expense.date);
std::cout << "Enter new notes: ";
std::getline(std::cin, expense.notes);
}
void writeExpenses(const std::vector<Expense>& expenses) {
std::ofstream outFile("expenses.csv");
for (const auto& expense : expenses) {
outFile << expense.amount << "," << expense.category << ","
<< expense.date << "," << expense.notes << "\n";
}
}
void editExpense() {
std::vector<Expense> expenses = readExpenses();
if (expenses.empty()) {
std::cerr << "No expenses to edit!" << std::endl;
return;
}
int index = selectExpenseToEdit(expenses);
if (index >= 0 && index < expenses.size()) {
editExpenseDetails(expenses[index]);
writeExpenses(expenses);
std::cout << "Expense edited successfully." << std::endl;
} else {
std::cerr << "Invalid selection!" << std::endl;
}
}
void removeExpense(int index, std::vector<Expense>& expenses) {
if (index >= 0 && index < expenses.size()) {
expenses.erase(expenses.begin() + index);
} else {
std::cerr << "Invalid index!" << std::endl;
}
}
void deleteExpense() {
std::vector<Expense> expenses = readExpenses();
if (expenses.empty()) {
std::cerr << "No expenses to delete!" << std::endl;
return;
}
int index = selectExpenseToEdit(expenses);
if (index >= 0 && index < expenses.size()) {
removeExpense(index, expenses);
writeExpenses(expenses);
std::cout << "Expense deleted successfully." << std::endl;
} else {
std::cerr << "Invalid selection!" << std::endl;
}
}
void generateReport() {
std::vector<Expense> expenses = readExpenses();
if (expenses.empty()) {
std::cerr << "No expenses to generate report!" << std::endl;
return;
}
double totalAmount = 0;
std::map<std::string, double> categoryTotals;
for (const auto& expense : expenses) {
totalAmount += expense.amount;
categoryTotals[expense.category] += expense.amount;
}
std::cout << "------Expenses Report------" << std::endl;
std::cout << "Total Amount Spent: ₹" << totalAmount << std::endl;
std::cout << "Expenses by Category:" << std::endl;
for (const auto& category : categoryTotals) {
std::cout << category.first << ": ₹" << category.second << std::endl;
}
}