fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdexcept>
  5. using namespace std;
  6.  
  7.  
  8. struct Asset {
  9. string name;
  10. string type;
  11. int quantity;
  12. float purchase_price;
  13. float current_price;
  14. float transaction_cost;
  15. };
  16. //ici on definit la fonction qui nous permet d'ajouter un autre actif
  17. void addAsset(vector<Asset>& portfolio) {
  18. Asset new_asset;
  19.  
  20. cout << " we are going to define a new Asset: "<<endl;
  21.  
  22. cout << "Enter the name: ";
  23. cin >> ws;
  24. getline(cin, new_asset.name); //ici on demande a l'utilisateur d'ajouter le nom du nouveau actif
  25.  
  26. cout << "Type: ";
  27. getline(cin, new_asset.type);
  28.  
  29. cout << "Quantity: ";
  30. cin >> new_asset.quantity;
  31. while (new_asset.quantity <= 0) {
  32. cout << "Invalid quantity . Enter again: ";
  33. cin >> new_asset.quantity;
  34. }
  35.  
  36. cout << "Purchase price: ";
  37. cin >> new_asset.purchase_price;
  38. while (new_asset.purchase_price <= 0) {
  39. cout << "Invalid purchase price. Enter again: ";
  40. cin >> new_asset.purchase_price;
  41. }
  42.  
  43. cout << "Current price: ";
  44. cin >> new_asset.current_price;
  45. while (new_asset.current_price <= 0) {
  46. cout << "Invalid current price (must be > 0). Enter again: ";
  47. cin >> new_asset.current_price;
  48. }
  49.  
  50. cout << "Transaction cost: ";
  51. cin >> new_asset.transaction_cost;
  52. while (new_asset.transaction_cost < 0) {
  53. cout << "Invalid transaction cost (must be >= 0). Enter again: ";
  54. cin >> new_asset.transaction_cost;
  55. }
  56.  
  57.  
  58. portfolio.push_back(new_asset);
  59. }
  60. //ici on definit la fonction qui nous permet d'afficher notre porte feuille.
  61. void displayPortfolio( vector<Asset> portfolio) {
  62. for (size_t i = 0; i < portfolio.size(); i++) {
  63. cout << portfolio[i].name << " "
  64. << portfolio[i].type << " "
  65. << portfolio[i].quantity << " "
  66. << portfolio[i].purchase_price << " "
  67. << portfolio[i].current_price << endl;
  68. }
  69. }
  70. //ici on definit une fonction qui permet de rechercher le nom d'un actif dans notre portefeuille
  71. int searchAssetByName(vector<Asset> portfolio, string search_name) {
  72. for (size_t i = 0; i < portfolio.size(); ++i) {
  73. if (portfolio[i].name.compare(search_name) == 0) {
  74. return i;//si on trouve un nom d'actif qui correspond a search_name
  75. //on recupere la position de cet actif
  76. }
  77. }
  78. //si aucun actif a le nom search_name, on affiche un message d'erreur.
  79. throw runtime_error("Error the requested asset does not exist in the portfolio!");
  80. }
  81. //ici on va creer une fonction pour mettre a jour lactif donc le nom est asset_name
  82. void updateAsset(vector<Asset>& portfolio, string asset_name) {
  83. int index = searchAssetByName(portfolio, asset_name);
  84. //ici on affiche l'actif non modifier asset name.
  85. cout << portfolio[index].name << " "
  86. << portfolio[index].type << " "
  87. << portfolio[index].quantity << " "
  88. << portfolio[index].purchase_price << " "
  89. << portfolio[index].current_price << " "
  90. << portfolio[index].transaction_cost
  91. << endl;
  92. //nous voulons a present modifier l'actif numero index de notre porte feuille
  93. cout << "enter the new name: ";
  94. cin >> ws;
  95. getline(cin, portfolio[index].name); //ici on demande a l'utilisateur d'ajouter le nom de modifier de l'actif
  96.  
  97. cout << "enter the new type: ";
  98. getline(cin, portfolio[index].type);
  99. cout << "enter the new quantity: ";
  100. cin >> portfolio[index].quantity;
  101. while (portfolio[index].quantity <= 0) {
  102. cout << "Error, the quantity must be positive. Try again: ";
  103. cin >> portfolio[index].quantity;
  104. }
  105.  
  106. cout << "enter the new purchase price: ";
  107. cin >> portfolio[index].purchase_price;
  108. while (portfolio[index].purchase_price <= 0) {
  109. cout << "Error the purchase price must be positive. Try again: ";
  110. cin >> portfolio[index].purchase_price;
  111. }
  112.  
  113. cout << "enter the new current price: ";
  114. cin >> portfolio[index].current_price;
  115. while (portfolio[index].current_price <= 0) {
  116. cout << "Error the Current price must be positive. Try again: ";
  117. cin >> portfolio[index].current_price;
  118. }
  119.  
  120. cout << "enter the new transaction cost: ";
  121. cin >> portfolio[index].transaction_cost;
  122. while (portfolio[index].transaction_cost < 0) {
  123. cout << "Error the transaction cost cannot be negative. Try again: ";
  124. cin >> portfolio[index].transaction_cost;
  125. }
  126.  
  127. cout << " the madified asset is given by:" << endl;
  128. //il st question ici d'afficher l'actif modifier.
  129. cout << portfolio[index].name << " "
  130. << portfolio[index].type << " "
  131. << portfolio[index].quantity << " "
  132. << portfolio[index].purchase_price << " "
  133. << portfolio[index].current_price << " "
  134. << portfolio[index].transaction_cost
  135. << endl;
  136. }
  137. int main() {
  138. // Initialisation du portefeuille
  139. vector<Asset> portfolio = {
  140. {"ABC Corp", "Stock", 100, 25.50, 28.20, 25.00},
  141. {"XYZ Bank", "Stock", 150, 18.75, 17.90, 28.00},
  142. {"CAM Bond", "Bond", 200, 98.50, 101.20, 28.00},
  143. {"QF ETF", "ETF", 80, 42.00, 46.50, 16.00},
  144. {"NXT Energy", "Stock", 60, 55.00, 44.25, 16.00}
  145. };
  146.  
  147.  
  148.  
  149.  
  150. return 0;
  151. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty