|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <vector> |
| 4 | +#include <iomanip> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +class Transaction { |
| 10 | +private: |
| 11 | + string category; |
| 12 | + double amount; |
| 13 | + string note; |
| 14 | + |
| 15 | +public: |
| 16 | + Transaction(string c, double a, string n) { |
| 17 | + category = c; |
| 18 | + amount = a; |
| 19 | + note = n; |
| 20 | + } |
| 21 | + |
| 22 | + string getCategory() const { return category; } |
| 23 | + double getAmount() const { return amount; } |
| 24 | + string getNote() const { return note; } |
| 25 | + |
| 26 | + void display() const { |
| 27 | + cout << left << setw(15) << category |
| 28 | + << setw(10) << fixed << setprecision(2) << amount |
| 29 | + << setw(25) << note << endl; |
| 30 | + } |
| 31 | + |
| 32 | + string toFileString() const { |
| 33 | + return category + "," + to_string(amount) + "," + note + "\n"; |
| 34 | + } |
| 35 | + |
| 36 | + static Transaction fromFileString(const string &line) { |
| 37 | + string c, a, n; |
| 38 | + size_t pos1 = line.find(','); |
| 39 | + size_t pos2 = line.find(',', pos1 + 1); |
| 40 | + |
| 41 | + if (pos1 == string::npos || pos2 == string::npos) |
| 42 | + return Transaction("Invalid", 0.0, "Corrupt"); |
| 43 | + |
| 44 | + c = line.substr(0, pos1); |
| 45 | + a = line.substr(pos1 + 1, pos2 - pos1 - 1); |
| 46 | + n = line.substr(pos2 + 1); |
| 47 | + return Transaction(c, stod(a), n); |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +class ExpenseTracker { |
| 52 | +private: |
| 53 | + vector<Transaction> transactions; |
| 54 | + const string filename = "expenses.txt"; |
| 55 | + |
| 56 | + void loadFromFile() { |
| 57 | + ifstream fin(filename); |
| 58 | + if (!fin.is_open()) return; |
| 59 | + |
| 60 | + string line; |
| 61 | + while (getline(fin, line)) { |
| 62 | + if (!line.empty()) |
| 63 | + transactions.push_back(Transaction::fromFileString(line)); |
| 64 | + } |
| 65 | + fin.close(); |
| 66 | + } |
| 67 | + |
| 68 | + void saveToFile() const { |
| 69 | + ofstream fout(filename); |
| 70 | + for (const auto &t : transactions) |
| 71 | + fout << t.toFileString(); |
| 72 | + fout.close(); |
| 73 | + } |
| 74 | + |
| 75 | +public: |
| 76 | + ExpenseTracker() { |
| 77 | + loadFromFile(); |
| 78 | + } |
| 79 | + |
| 80 | + void addTransaction() { |
| 81 | + string category, note; |
| 82 | + double amount; |
| 83 | + cout << "\nEnter category: "; |
| 84 | + cin.ignore(); |
| 85 | + getline(cin, category); |
| 86 | + cout << "Enter amount: "; |
| 87 | + cin >> amount; |
| 88 | + cin.ignore(); |
| 89 | + cout << "Enter note: "; |
| 90 | + getline(cin, note); |
| 91 | + |
| 92 | + transactions.push_back(Transaction(category, amount, note)); |
| 93 | + saveToFile(); |
| 94 | + cout << "\n✅ Transaction added successfully!\n"; |
| 95 | + } |
| 96 | + |
| 97 | + void viewAll() const { |
| 98 | + if (transactions.empty()) { |
| 99 | + cout << "\nNo transactions recorded yet.\n"; |
| 100 | + return; |
| 101 | + } |
| 102 | + cout << "\n================= All Transactions =================\n"; |
| 103 | + cout << left << setw(15) << "Category" << setw(10) << "Amount" << setw(25) << "Note" << endl; |
| 104 | + cout << "----------------------------------------------------\n"; |
| 105 | + for (const auto &t : transactions) |
| 106 | + t.display(); |
| 107 | + } |
| 108 | + |
| 109 | + void viewSummary() const { |
| 110 | + if (transactions.empty()) { |
| 111 | + cout << "\nNo transactions available.\n"; |
| 112 | + return; |
| 113 | + } |
| 114 | + double total = 0; |
| 115 | + for (const auto &t : transactions) |
| 116 | + total += t.getAmount(); |
| 117 | + |
| 118 | + cout << "\nTotal Transactions: " << transactions.size(); |
| 119 | + cout << "\nTotal Amount: ₹" << fixed << setprecision(2) << total << endl; |
| 120 | + } |
| 121 | + |
| 122 | + void searchByCategory() const { |
| 123 | + if (transactions.empty()) { |
| 124 | + cout << "\nNo transactions available.\n"; |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + string search; |
| 129 | + cout << "\nEnter category to search: "; |
| 130 | + cin.ignore(); |
| 131 | + getline(cin, search); |
| 132 | + |
| 133 | + bool found = false; |
| 134 | + cout << "\nTransactions in category \"" << search << "\":\n"; |
| 135 | + cout << left << setw(15) << "Category" << setw(10) << "Amount" << setw(25) << "Note" << endl; |
| 136 | + cout << "----------------------------------------------------\n"; |
| 137 | + for (const auto &t : transactions) { |
| 138 | + if (t.getCategory() == search) { |
| 139 | + t.display(); |
| 140 | + found = true; |
| 141 | + } |
| 142 | + } |
| 143 | + if (!found) |
| 144 | + cout << "No transactions found in this category.\n"; |
| 145 | + } |
| 146 | + |
| 147 | + void deleteTransaction() { |
| 148 | + if (transactions.empty()) { |
| 149 | + cout << "\nNo transactions to delete.\n"; |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + viewAll(); |
| 154 | + cout << "\nEnter transaction number to delete (1 - " << transactions.size() << "): "; |
| 155 | + int index; |
| 156 | + cin >> index; |
| 157 | + |
| 158 | + if (index < 1 || index > (int)transactions.size()) { |
| 159 | + cout << "❌ Invalid index!\n"; |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + transactions.erase(transactions.begin() + index - 1); |
| 164 | + saveToFile(); |
| 165 | + cout << "✅ Transaction deleted successfully.\n"; |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +int main() { |
| 170 | + ExpenseTracker tracker; |
| 171 | + int choice; |
| 172 | + |
| 173 | + while (true) { |
| 174 | + cout << "\n================= Expense Tracker ================="; |
| 175 | + cout << "\n1. Add Transaction"; |
| 176 | + cout << "\n2. View All Transactions"; |
| 177 | + cout << "\n3. View Summary"; |
| 178 | + cout << "\n4. Search by Category"; |
| 179 | + cout << "\n5. Delete a Transaction"; |
| 180 | + cout << "\n6. Exit"; |
| 181 | + cout << "\n==================================================="; |
| 182 | + cout << "\nEnter your choice: "; |
| 183 | + cin >> choice; |
| 184 | + |
| 185 | + switch (choice) { |
| 186 | + case 1: tracker.addTransaction(); break; |
| 187 | + case 2: tracker.viewAll(); break; |
| 188 | + case 3: tracker.viewSummary(); break; |
| 189 | + case 4: tracker.searchByCategory(); break; |
| 190 | + case 5: tracker.deleteTransaction(); break; |
| 191 | + case 6: |
| 192 | + cout << "\n💾 Data saved. Exiting... Goodbye!\n"; |
| 193 | + return 0; |
| 194 | + default: |
| 195 | + cout << "\nInvalid choice! Try again.\n"; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments