Facebook
From Queen Gibbon, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 286
  1. // PortfelInwestycyjny.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "Resources.h"
  5.  
  6. const std::vector<std::string> explode(const std::string& s, const char& c)
  7. {
  8.         std::string buff{ "" };
  9.         std::vector<std::string> v;
  10.  
  11.         for (auto n : s)
  12.         {
  13.                 if (n != c) buff += n; else
  14.                         if (n == c && buff != "") { v.push_back(buff); buff = ""; }
  15.         }
  16.         if (buff != "") v.push_back(buff);
  17.  
  18.         return v;
  19. }
  20. template <class T> class SavingsWallet
  21. {
  22. private:
  23.         std::vector <T> assets;
  24. public:
  25.         void AddAsset(T Asset, bool CalledByLoad=false)
  26.         {
  27.                 bool nameExists = false;
  28.                 if (Asset.Name == ";")
  29.                 {
  30.                         std::cout << "Nieprawidlowa nazwa";
  31.                         return;
  32.                 }
  33.                 for (int i = 0; i < assets.size(); i++)
  34.                 {
  35.                         if (assets[i].Name == Asset.Name)
  36.                         {
  37.                         nameExists = true;
  38.                         break;
  39.                         }      
  40.                 }
  41.                 if (nameExists)
  42.                 {
  43.                         std::cout << "Asset with this name already exists";
  44.                         return;
  45.                 }
  46.                 else
  47.                 assets.push_back(Asset);
  48.                 if (CalledByLoad==false)
  49.                  this->SaveState(false);
  50.         }
  51.  
  52.         void DisplayAssets()
  53.         {
  54.                 system("cls");
  55.                 std::cout << "Obecnie posiadane aktywa: ";
  56.                 for (T asset : assets)
  57.                 {
  58.                         if (asset.ID == 1)
  59.                         {
  60.                                 std::cout << "\nLokata " << asset.GetName() << " ilosc pieniedzy na lokacie " << asset.GetValue() << " oprocentowanie lokaty " << asset.GetProfitPercent() << " %\n";
  61.                         }
  62.                         if (asset.ID == 2)
  63.                         {
  64.                                 std::cout << "\nKonto emerytalne " << asset.GetName() << " ilosc pieniedzy na koncie " << asset.GetValue() << " oprocentowanie konta " << asset.GetProfitPercent() << " %" << " miesieczne wplaty na konto " << asset.GetFlatProfit() << std::endl;
  65.                         }
  66.                         if (asset.ID == 3)
  67.                         {
  68.                                 std::cout << "\nNieruchomosc " << asset.GetName() << " miesieczne dochody z nieruchomosci " << asset.GetFlatProfit() / 12 << std::endl;
  69.                         }
  70.                 }
  71.                 std::cout << std::endl;
  72.         }
  73.  
  74.         void RemoveAsset(std::string Name)
  75.         {
  76.                 system("cls");
  77.                 bool ifRemoved = false;
  78.                 for (int i = 0; i < assets.size(); i++)
  79.                 {
  80.                         if (Name == assets[i].Name)
  81.                         {
  82.                                 assets.erase(assets.begin() + i);
  83.                                 this->SaveState(true);
  84.                                 ifRemoved = true;
  85.                         }
  86.                 }
  87.                 if (ifRemoved==false)
  88.                 {
  89.                 std::cout << "Aktyw o podanej nazwie nie istnieje\n";
  90.                 }
  91.                
  92.         }
  93.         void EditAsset(T NewAssetValues,std::string EditedAssetName)
  94.         {
  95.                 for (T& asset : assets)
  96.                 {
  97.                         if (asset.Name == EditedAssetName)
  98.                         {
  99.                                 asset += NewAssetValues;
  100.                         }
  101.                 }
  102.         }
  103.         T GetInfoAboutAsset(std::string Name)
  104.         {
  105.                 for (T& asset : assets)
  106.                 {
  107.                         if (asset.Name == Name)
  108.                         {
  109.                                 return asset;
  110.                         }
  111.                 }
  112.                 throw std::string("Aktyw z taka nazwa nie istnieje");
  113.         }
  114.         double CalculateSavings(int Years)
  115.         {
  116.                 double sum = 0;
  117.                 for(T& asset:assets)
  118.                 {
  119.                         sum += asset.SimulateProfit(Years);
  120.                 }
  121.                 return sum;
  122.         }
  123.         void LoadState()
  124.         {
  125.                 std::fstream File;
  126.                 try {
  127.                         File.open("Wallet.txt", std::ios::in);
  128.                         std::string line;
  129.                         int value, profitPerCent, flatProfit, ID;
  130.                         if (File.good() == true)
  131.                         {
  132.                                 while (getline(File, line))
  133.                                 {
  134.                                         std::vector<std::string> v{ explode(line, ';') };
  135.                                         if (v.size()==5)
  136.                                         {
  137.                                                 value = std::atoi(v[0].c_str());
  138.                                                 profitPerCent = std::atoi(v[1].c_str());
  139.                                                 flatProfit = std::atoi(v[2].c_str());
  140.                                                 ID = std::atoi(v[3].c_str());
  141.                                                 this->AddAsset(Asset(profitPerCent, flatProfit, value, v[4], ID),true);
  142.                                         }
  143.                                 }
  144.                         }
  145.                         else
  146.                                 throw 1;
  147.                 }
  148.                 catch (int e)
  149.                 {
  150.                         std::cout << "Ładowanie pliku nie powiodlo sie, twoj portfel jest pusty";
  151.                 }
  152.         }
  153.         void SaveState(bool Overwrite)
  154.         {
  155.                 std::fstream File;
  156.                 try {
  157.                         if (Overwrite)
  158.                         {
  159.                                 File.open("Wallet.txt", std::ios::out);
  160.                                 if (File.good() == true)
  161.                                 {
  162.                                         for (int i = 0; i < assets.size(); i++)
  163.                                         {
  164.                                                 assets[i].SaveState(File);
  165.                                                 File << std::endl;
  166.                                         }              
  167.                                 }
  168.                                 else
  169.                                         throw 1;
  170.                         }
  171.                         else
  172.                         {
  173.                                 File.open("Wallet.txt", std::ios::out | std::ios::app);
  174.                                 if (File.good() == true)
  175.                                 {
  176.                                         assets[assets.size() - 1].SaveState(File);
  177.                                         File << std::endl;
  178.                                 }
  179.                                 else
  180.                                         throw 1;
  181.                         }
  182.                         File.close();
  183.                 }
  184.                 catch (int e)
  185.                 {
  186.                         std::cout << "\nZapis portfela nie powiodl sie";
  187.                 }
  188.         }
  189. };
  190.  
  191. void Asset::SetName(std::string n)
  192. {
  193.         this->Name = n;
  194. }
  195. void Asset::SetProfitPercent(double PerCent)
  196. {
  197.         this->ProfitPercentPerYear = PerCent;
  198. }
  199. void Asset::DisplayName()
  200. {
  201.         std::cout << Name;
  202. }
  203. Asset& Asset::operator -=(const Asset& Modified)
  204. {
  205.         if (this->ID == Modified.ID)
  206.         {
  207.                 this->Name = Modified.Name;
  208.                 this->Value -= Modified.Value;
  209.                 this->FlatProfitPerYear -= Modified.FlatProfitPerYear;
  210.                 this->ProfitPercentPerYear -= Modified.FlatProfitPerYear;
  211.         }
  212.         else
  213.                 throw std::string( "Id's not matching");
  214.         return *this;
  215. }
  216.  
  217. Asset& Asset::operator +=(const Asset& Modified)
  218. {
  219.         if (this->ID == Modified.ID)
  220.         {
  221.                 this->Name = Modified.Name;
  222.                 this->Value += Modified.Value;
  223.                 this->FlatProfitPerYear += Modified.FlatProfitPerYear;
  224.                 this->ProfitPercentPerYear += Modified.FlatProfitPerYear;
  225.         }
  226.         else
  227.                 throw std::string("Id's not matching");
  228.         return *this;
  229. }
  230.  
  231. void Asset::SetFlatProfit(double Flat)
  232. {
  233.         this->FlatProfitPerYear = Flat;
  234. }
  235. void Asset::SetValue(double v)
  236. {
  237.         this->Value = v;
  238. }
  239. double Asset::GetFlatProfit()
  240. {
  241.         return this->FlatProfitPerYear;
  242. }
  243. void Asset::SaveState(std::fstream& File)
  244. {
  245.         File << this->Value << ";" << this->ProfitPercentPerYear << ";" << this->FlatProfitPerYear << ";" << this->ID << ";" << this->Name << ";";
  246. }
  247. double Asset::GetProfitPercent()
  248. {
  249.         return this->ProfitPercentPerYear;
  250. }
  251. std::string Asset::GetName()
  252. {
  253.         return this->Name;
  254. }
  255. double Asset::GetValue()
  256. {
  257.         return this->Value;
  258. }
  259. double Asset::SimulateProfit(int Years)
  260. {
  261.         return Years * FlatProfitPerYear + Value * pow((ProfitPercentPerYear/100) + 1, Years);
  262. }
  263. int main()
  264. {
  265.         SavingsWallet<Asset> wallet;
  266.         wallet.LoadState();
  267.         char firstChoice, subChoice;
  268.         do {
  269.                 std::cout << "\nMENU\n1. Dodaj Aktyw\n2. Wyswietl Aktywa\n3. Modeluj przyszle oszczednosci\n4. Usun aktyw\n5. Obecne Oszczednosci\n6. Modyfikuj Aktywa7. Zamknac\n";
  270.                 std::cin >> firstChoice;
  271.                 std::cout << firstChoice;
  272.                 switch (firstChoice)
  273.                 {
  274.                 case 49:
  275.                 {
  276.                         system("cls");
  277.                         std::cout << "1. Lokata\n2. Nieruchomosc\n3. Konto Emerytalne\n";
  278.                         std::cin >> subChoice;
  279.                         switch (subChoice)
  280.                         {
  281.                         case 49:
  282.                         {
  283.                                 system("cls");
  284.                                 std::string name;
  285.                                 double perCent, ammount;
  286.                                 std::cout << "Podaj wplacona kwote: \n";
  287.                                 std::cin >> ammount;
  288.                                 std::cout << "Podaj oprocentowanie lokaty: \n";
  289.                                 std::cin >> perCent;
  290.                                 std::cout << "Podaj nazwe lokaty: \n";
  291.                                 std::cin >> name;
  292.                                 wallet.AddAsset(Asset(perCent, 0, ammount, name,1));
  293.                                 system("cls");
  294.                         }break;
  295.                         case 50:
  296.                         {
  297.                                 system("cls");
  298.                                 std::string name;
  299.                                 double costs, flat;
  300.                                 std::cout << "Podaj miesieczny czynsz: \n";
  301.                                 std::cin >> flat;
  302.                                 std::cout << "Podaj roczne koszty zwiazane z nieruchmoscia <podatek, remonty>: \n";
  303.                                 std::cin >> costs;
  304.                                 std::cout << "Podaj nazwe nieruchomosci: \n";
  305.                                 std::cin >> name;
  306.                                 wallet.AddAsset(Asset(0, 12*flat-costs, 0, name, 3));
  307.                                 system("cls");
  308.                         }break;
  309.                         case 51:
  310.                         {
  311.                                 system("cls");
  312.                                 std::string name;
  313.                                 double monthlyTransfers, perCent, moneyIn;
  314.                                 std::cout << "Podaj ile juz znajduje sie na koncie: \n";
  315.                                 std::cin >> moneyIn;
  316.                                 std::cout << "Podaj miesieczne wplaty na konto: \n";
  317.                                 std::cin >> monthlyTransfers;
  318.                                 std::cout << "Podaj oprocentowanie konta: \n";
  319.                                 std::cin >> perCent;
  320.                                 std::cout << "Podaj nazwe konta: \n";
  321.                                 std::cin >> name;
  322.                                 wallet.AddAsset(Asset(perCent,monthlyTransfers,moneyIn,name,2));
  323.                                 system("cls");
  324.                         }break;
  325.                         }
  326.                 }break;
  327.                 case 50:
  328.                 {
  329.                         wallet.DisplayAssets();
  330.                 }break;
  331.                 case 51:
  332.                 {
  333.                         system("cls");
  334.                         int years;
  335.                         std::cout << "Podaj ilosc lat ";
  336.                         std::cin >> years;
  337.                         if(years==1)
  338.                                 std::cout << "Twoje oszczednosci za rok wyniosa: " << wallet.CalculateSavings(years) << "\n";
  339.                         if(years<5 && years!=1)
  340.                                 std::cout << "Twoje oszczednosci za " << years << " lata wyniosa: " << wallet.CalculateSavings(years) << "\n";
  341.                         else
  342.                         std::cout <<"Twoje oszczednosci za "<< years << " lat wyniosa: "<< wallet.CalculateSavings(years) << "\n";
  343.                 }break;
  344.                 case 52:
  345.                 {
  346.                         std::string name;
  347.                         std::cout << "Podaj nazwe aktywu ktory chcesz usunac: \n";
  348.                         std::cin >> name;
  349.                         wallet.RemoveAsset(name);
  350.                 }break;
  351.                 case 53:
  352.                 {
  353.                         system("cls");
  354.                         std::cout << "Twoje obecne oszczednosci wynosza: " << wallet.CalculateSavings(0) << std::endl;
  355.                 }break;
  356.                 case 54:
  357.                 {
  358.                         std::string editedName;
  359.                         Asset c;
  360.                         system("cls");
  361.                         std::cout << "Podaj nazwe aktywu ktory chcesz modyfikowac: \n";
  362.                         std::cin >> editedName;
  363.                         try {
  364.                          c = wallet.GetInfoAboutAsset(editedName);
  365.                         }
  366.                         catch (std::string s)
  367.                         {
  368.                                 std::cout << s;
  369.                                 continue;
  370.                         }
  371.                         if (c.ID == 1)
  372.                         {
  373.                                 double percent, value;
  374.                                 std::string newName=";";
  375.                                 std::cout << "Podaj zmiane w ilosci pieniedzy na lokacie\n<ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  376.                                 std::cin >> value;
  377.                                 std::cout << "Podaj zmiane oprocentowania <ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  378.                                 std::cin >> percent;
  379.                                 std::cout << "Podaj nowa nazwe lokaty <srednik(;) oznacza brak zmian>: \n";
  380.                                 std::cin >> newName;
  381.  
  382.                                 if (newName[0] == ';')
  383.                                         newName = editedName;
  384.                                 Asset modified{ percent,0,value,newName,c.ID };
  385.                                 wallet.EditAsset(modified, editedName);
  386.                         }
  387.                         if (c.ID == 2)
  388.                         {
  389.                                 double percent, value, flat;
  390.                                 std::string newName = ";";
  391.                                 std::cout << "Podaj zmiane w ilosci pieniedzy na koncie\n<ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  392.                                 std::cin >> value;
  393.                                 std::cout << "Podaj zmiane w ilosci pieniedzy wplacanych co miesaic na konto\n<ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  394.                                 std::cin >> flat;
  395.                                 std::cout << "Podaj zmiane oprocentowania\n<ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  396.                                 std::cin >> percent;
  397.                                 std::cout << "Podaj nowa nazwe konta\n<srednik(;) oznacza brak zmian>: \n";
  398.                                 std::cin >> newName;
  399.  
  400.                                 if (newName[0] == ';')
  401.                                         newName = editedName;
  402.                                 Asset modified{ percent,flat,value,newName,c.ID };
  403.                                 wallet.EditAsset(modified, editedName);
  404.                         }
  405.                         if (c.ID == 3)
  406.                         {
  407.                                 double flat, value;
  408.                                 std::string newName = ";";
  409.                                 std::cout << "Podaj zmiane w miesiecznym czynszu\n<ujemne wartosci ubytek, 0 brak zmian, dodatnie wartosci wzrost>: \n";
  410.                                 std::cin >> flat;
  411.                                 std::cout << "Podaj nowa nazwe nieruchomosci\n<srednik(;) oznacza brak zmian>: \n";
  412.                                 std::cin >> newName;
  413.  
  414.                                 if (newName[0] == ';')
  415.                                         newName = editedName;
  416.                                 Asset modified{ 0,flat,0,newName,c.ID };
  417.                                 wallet.EditAsset(modified, editedName);
  418.                         }
  419.                 }break;
  420.                 default:
  421.                         system("cls");
  422.                         std::cout << "Nie ma takiej opcji\n";
  423.                 }
  424.         } while (firstChoice != 9999);
  425.     return 0;
  426. }