-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (54 loc) · 2.91 KB
/
main.cpp
File metadata and controls
56 lines (54 loc) · 2.91 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
#include <iostream>
#include <iomanip>
#include <limits>
#include "../include/ExpenseManager.h"
static void clear_stdin() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int main(){
ExpenseManager mgr;
bool running = true;
while (running) {
std::cout << "\n--- Expense Tracker ---\n";
std::cout << "1) Add expense\n2) View all\n3) Search\n4) Totals by category\n5) Delete by ID\n6) Exit\n";
std::cout << "Choose: ";
int choice; if (!(std::cin >> choice)) { std::cin.clear(); clear_stdin(); continue; }
clear_stdin();
if (choice == 1) {
std::string date, cat, desc; double amt;
std::cout << "Date (YYYY-MM-DD): "; std::getline(std::cin, date);
std::cout << "Category: "; std::getline(std::cin, cat);
std::cout << "Description: "; std::getline(std::cin, desc);
std::cout << "Amount: "; if (!(std::cin >> amt)) { std::cout << "Invalid amount\n"; std::cin.clear(); clear_stdin(); continue; }
clear_stdin();
int id = mgr.add_expense(date, cat, desc, amt);
std::cout << "Added with ID: " << id << '\n';
} else if (choice == 2) {
auto all = mgr.all();
if (all.empty()) { std::cout << "No expenses yet.\n"; continue; }
std::cout << std::left << std::setw(6) << "ID" << std::setw(12) << "Date" << std::setw(15) << "Category" << std::setw(30) << "Description" << std::setw(8) << "Amount" << '\n';
std::cout << std::string(75, '-') << '\n';
for (const auto &e: all) {
std::cout << std::left << std::setw(6) << e.id << std::setw(12) << e.date << std::setw(15) << e.category << std::setw(30) << e.description << std::setw(8) << std::fixed << std::setprecision(2) << e.amount << '\n';
}
} else if (choice == 3) {
std::string kw; std::cout << "Enter date/category/keyword: "; std::getline(std::cin, kw);
auto res = mgr.search_by_keyword(kw);
if (res.empty()) { std::cout << "No matches\n"; continue; }
for (const auto &e: res) std::cout << e.id << " | " << e.date << " | " << e.category << " | " << e.description << " | " << e.amount << '\n';
} else if (choice == 4) {
auto m = mgr.totals_by_category();
std::cout << "Totals by category:\n";
for (const auto &p: m) std::cout << std::setw(15) << p.first << " : " << std::fixed << std::setprecision(2) << p.second << '\n';
} else if (choice == 5) {
int id; std::cout << "Enter ID to delete: "; if (!(std::cin >> id)) { std::cout << "Invalid\n"; std::cin.clear(); clear_stdin(); continue; }
clear_stdin();
bool ok = mgr.delete_expense(id);
std::cout << (ok ? "Deleted\n" : "Not found\n");
} else if (choice == 6) {
running = false;
}
}
std::cout << "Bye!\n";
return 0;
}