#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;


struct Asset {
    string name;
    string type;
    int quantity;
    float purchase_price;
    float current_price;
    float transaction_cost;
};
//ici on definit la fonction qui nous permet d'ajouter  un autre actif
void addAsset(vector<Asset>& portfolio) {
    Asset new_asset;

    cout << " we are going to define a new Asset: "<<endl;
  
    cout << "Enter the name: ";
    cin >> ws;
    getline(cin, new_asset.name);  //ici on demande a l'utilisateur d'ajouter le nom du nouveau actif

    cout << "Type: ";
    getline(cin, new_asset.type);

    cout << "Quantity: ";
    cin >> new_asset.quantity;
    while (new_asset.quantity <= 0) {
        cout << "Invalid quantity . Enter again: ";
        cin >> new_asset.quantity;
    }

    cout << "Purchase price: ";
    cin >> new_asset.purchase_price;
    while (new_asset.purchase_price <= 0) {
        cout << "Invalid purchase price. Enter again: ";
        cin >> new_asset.purchase_price;
    }

    cout << "Current price: ";
    cin >> new_asset.current_price;
    while (new_asset.current_price <= 0) {
        cout << "Invalid current price (must be > 0). Enter again: ";
        cin >> new_asset.current_price;
    }

    cout << "Transaction cost: ";
    cin >> new_asset.transaction_cost;
    while (new_asset.transaction_cost < 0) {
        cout << "Invalid transaction cost (must be >= 0). Enter again: ";
        cin >> new_asset.transaction_cost;
    }

    
    portfolio.push_back(new_asset);
}
//ici on definit la fonction qui nous permet d'afficher notre porte feuille.
void displayPortfolio( vector<Asset> portfolio) {
    for (size_t i = 0; i < portfolio.size(); i++) {
        cout << portfolio[i].name << "  " 
             << portfolio[i].type << "  " 
             << portfolio[i].quantity << "  " 
             << portfolio[i].purchase_price << " " 
             << portfolio[i].current_price << endl;
    }
}
//ici on definit une fonction qui permet de rechercher le nom d'un actif dans notre portefeuille
int searchAssetByName(vector<Asset> portfolio, string search_name) {
    for (size_t i = 0; i < portfolio.size(); ++i) {
        if (portfolio[i].name.compare(search_name) == 0) {
            return i;//si on trouve un nom d'actif qui correspond a search_name
            //on recupere la position de cet actif
        }
    }
    //si aucun actif a le nom search_name, on affiche un message d'erreur.  
    throw runtime_error("Error the requested asset does not exist in the portfolio!");
}
//ici on va creer une fonction pour mettre a jour lactif donc le nom est asset_name
void updateAsset(vector<Asset>& portfolio, string asset_name) {
    int index = searchAssetByName(portfolio, asset_name);
    //ici on affiche l'actif non modifier asset name.
    cout << portfolio[index].name << " "
     << portfolio[index].type << " "
     << portfolio[index].quantity << " "
     << portfolio[index].purchase_price << " "
     << portfolio[index].current_price << " "
     << portfolio[index].transaction_cost
     << endl;
    //nous voulons a present modifier l'actif numero index de notre porte feuille
      cout << "enter the new name: ";
    cin >> ws;
    getline(cin, portfolio[index].name);  //ici on demande a l'utilisateur d'ajouter le nom de modifier de l'actif

    cout << "enter the new type: ";
    getline(cin, portfolio[index].type);
    cout << "enter the new quantity: ";
    cin >> portfolio[index].quantity;
    while (portfolio[index].quantity <= 0) {
        cout << "Error, the quantity must be positive. Try again: ";
        cin >> portfolio[index].quantity;
    }
    
    cout << "enter the new purchase price: ";
    cin >> portfolio[index].purchase_price;
    while (portfolio[index].purchase_price <= 0) {
        cout << "Error the purchase price must be positive. Try again: ";
        cin >> portfolio[index].purchase_price;
    }
    
    cout << "enter the new current price: ";
    cin >> portfolio[index].current_price;
    while (portfolio[index].current_price <= 0) {
        cout << "Error the Current price must be positive. Try again: ";
        cin >> portfolio[index].current_price;
    }
    
    cout << "enter the new transaction cost: ";
    cin >> portfolio[index].transaction_cost;
    while (portfolio[index].transaction_cost < 0) {
        cout << "Error the transaction cost cannot be negative. Try again: ";
        cin >> portfolio[index].transaction_cost;
    }
    
    cout << " the madified asset is given by:" << endl;
    //il st question ici d'afficher l'actif modifier. 
    cout << portfolio[index].name << " "
     << portfolio[index].type << " "
     << portfolio[index].quantity << " "
     << portfolio[index].purchase_price << " "
     << portfolio[index].current_price << " "
     << portfolio[index].transaction_cost
     << endl;
}
int main() {
    // Initialisation du portefeuille 
    vector<Asset> portfolio = {
        {"ABC Corp",   "Stock", 100,  25.50,  28.20,  25.00},
        {"XYZ Bank",   "Stock", 150,  18.75,  17.90,  28.00},
        {"CAM Bond",   "Bond",  200,  98.50, 101.20,  28.00},
        {"QF ETF",     "ETF",    80,  42.00,  46.50,  16.00},
        {"NXT Energy", "Stock",  60,  55.00,  44.25,  16.00}
    };

   
    

    return 0;
}