#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct User {
    string name;
    string password;
};

struct Record {
    string date;
    double revenue;
    double profit;
};

vector<User> users;
vector<Record> records;

void addUser() {
    User user;
    cout << "Enter name: ";
    cin >> user.name;
    cout << "Enter password: ";
    cin >> user.password;
    users.push_back(user);
}

void editUser() {
    string name;
    cout << "Enter name of user to edit: ";
    cin >> name;
    for (int i = 0; i < users.size(); i++) {
        if (users[i].name == name) {
            cout << "Enter new password: ";
            cin >> users[i].password;
            return;
        }
    }
    cout << "User not found." << endl;
}

void deleteUser() {
    string name;
    cout << "Enter name of user to delete: ";
    cin >> name;
    for (int i = 0; i < users.size(); i++) {
        if (users[i].name == name) {
            users.erase(users.begin() + i);
            return;
        }
    }
    cout << "User not found." << endl;
}

void addRecord() {
    Record record;
    cout << "Enter date (YYYY-MM-DD): ";
    cin >> record.date;
    cout << "Enter revenue: ";
    cin >> record.revenue;
    cout << "Enter profit: ";
    cin >> record.profit;
    records.push_back(record);
}

void editRecord() {
    string date;
    cout << "Enter date of record to edit (YYYY-MM-DD): ";
    cin >> date;
    for (int i = 0; i < records.size(); i++) {
        if (records[i].date == date) {
            cout << "Enter new revenue: ";
            cin >> records[i].revenue;
            cout << "Enter new profit: ";
            cin >> records[i].profit;
            return;
        }
    }
    cout << "Record not found." << endl;
}

void deleteRecord() {
    string date;
    cout << "Enter date of record to delete (YYYY-MM-DD): ";
    cin >> date;
    for (int i = 0; i < records.size(); i++) {
        if (records[i].date == date) {
            records.erase(records.begin() + i);
            return;
        }
    }
    cout << "Record not found." << endl;
}

void outputAnalytics() {
    double totalRevenue = 0;
    double totalProfit = 0;
    for (int i = 0; i < records.size(); i++) {
        totalRevenue += records[i].revenue;
        totalProfit += records[i].profit;
    }
    double averageRevenue = totalRevenue / records.size();
    double averageProfit = totalProfit / records.size();
    cout << "Total revenue: $" << totalRevenue << endl;
    cout << "Total profit: $" << totalProfit << endl;
    cout << "Average revenue: $" << averageRevenue << endl;
    cout << "Average profit: $" << averageProfit << endl;
}

void saveRecordsToFile() {
    ofstream file("records.csv");
    file << "Date,Revenue,Profit" << endl;
    for (int i = 0; i < records.size(); i++) {
        file << records[i].date << "," << records[i].revenue << "," << records[i].profit << endl;
    }
    file.close();
}

int main() {

    int choice = 0;

    do {

        cout << "\nSelect option and press enter \n1. Add user\n2. edit user\n3. delete user\n4. add record\n5. edit record\n6. delete record\n7. output analytics\n8. save records to file\n0. exit)\nEnter option: ";

        cin >> choice;

        switch (choice) {

        case 1:
            addUser();
            break;
        case 2:
            editUser();
            break;
        case 3:
            deleteUser();
            break;
        case 4:
            addRecord();
            break;
        case 5:
            editRecord();
            break;
        case 6:
            deleteRecord();
            break;
        case 7:
            outputAnalytics();
            break;
        case 8:
            saveRecordsToFile();
        case 0:
            break;
            exit(0);
        default:
            cout << "\nPlease enter correct option" << endl;
        }

    } while (choice != 0);

   
    return 0;
}
