// PortfelInwestycyjny.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Resources.h" const std::vector explode(const std::string& s, const char& c) { std::string buff{ "" }; std::vector v; for (auto n : s) { if (n != c) buff += n; else if (n == c && buff != "") { v.push_back(buff); buff = ""; } } if (buff != "") v.push_back(buff); return v; } template class SavingsWallet { private: std::vector assets; public: void AddAsset(T Asset, bool CalledByLoad=false) { bool nameExists = false; if (Asset.Name == ";") { std::cout << "Nieprawidlowa nazwa"; return; } for (int i = 0; i < assets.size(); i++) { if (assets[i].Name == Asset.Name) { nameExists = true; break; } } if (nameExists) { std::cout << "Asset with this name already exists"; return; } else assets.push_back(Asset); if (CalledByLoad==false) this->SaveState(false); } void DisplayAssets() { system("cls"); std::cout << "Obecnie posiadane aktywa: "; for (T asset : assets) { if (asset.ID == 1) { std::cout << "\nLokata " << asset.GetName() << " ilosc pieniedzy na lokacie " << asset.GetValue() << " oprocentowanie lokaty " << asset.GetProfitPercent() << " %\n"; } if (asset.ID == 2) { std::cout << "\nKonto emerytalne " << asset.GetName() << " ilosc pieniedzy na koncie " << asset.GetValue() << " oprocentowanie konta " << asset.GetProfitPercent() << " %" << " miesieczne wplaty na konto " << asset.GetFlatProfit() << std::endl; } if (asset.ID == 3) { std::cout << "\nNieruchomosc " << asset.GetName() << " miesieczne dochody z nieruchomosci " << asset.GetFlatProfit() / 12 << std::endl; } } std::cout << std::endl; } void RemoveAsset(std::string Name) { system("cls"); bool ifRemoved = false; for (int i = 0; i < assets.size(); i++) { if (Name == assets[i].Name) { assets.erase(assets.begin() + i); this->SaveState(true); ifRemoved = true; } } if (ifRemoved==false) { std::cout << "Aktyw o podanej nazwie nie istnieje\n"; } } void EditAsset(T NewAssetValues,std::string EditedAssetName) { for (T& asset : assets) { if (asset.Name == EditedAssetName) { asset += NewAssetValues; } } } T GetInfoAboutAsset(std::string Name) { for (T& asset : assets) { if (asset.Name == Name) { return asset; } } throw std::string("Aktyw z taka nazwa nie istnieje"); } double CalculateSavings(int Years) { double sum = 0; for(T& asset:assets) { sum += asset.SimulateProfit(Years); } return sum; } void LoadState() { std::fstream File; try { File.open("Wallet.txt", std::ios::in); std::string line; int value, profitPerCent, flatProfit, ID; if (File.good() == true) { while (getline(File, line)) { std::vector v{ explode(line, ';') }; if (v.size()==5) { value = std::atoi(v[0].c_str()); profitPerCent = std::atoi(v[1].c_str()); flatProfit = std::atoi(v[2].c_str()); ID = std::atoi(v[3].c_str()); this->AddAsset(Asset(profitPerCent, flatProfit, value, v[4], ID),true); } } } else throw 1; } catch (int e) { std::cout << "Ɓadowanie pliku nie powiodlo sie, twoj portfel jest pusty"; } } void SaveState(bool Overwrite) { std::fstream File; try { if (Overwrite) { File.open("Wallet.txt", std::ios::out); if (File.good() == true) { for (int i = 0; i < assets.size(); i++) { assets[i].SaveState(File); File << std::endl; } } else throw 1; } else { File.open("Wallet.txt", std::ios::out | std::ios::app); if (File.good() == true) { assets[assets.size() - 1].SaveState(File); File << std::endl; } else throw 1; } File.close(); } catch (int e) { std::cout << "\nZapis portfela nie powiodl sie"; } } }; void Asset::SetName(std::string n) { this->Name = n; } void Asset::SetProfitPercent(double PerCent) { this->ProfitPercentPerYear = PerCent; } void Asset::DisplayName() { std::cout << Name; } Asset& Asset::operator -=(const Asset& Modified) { if (this->ID == Modified.ID) { this->Name = Modified.Name; this->Value -= Modified.Value; this->FlatProfitPerYear -= Modified.FlatProfitPerYear; this->ProfitPercentPerYear -= Modified.FlatProfitPerYear; } else throw std::string( "Id's not matching"); return *this; } Asset& Asset::operator +=(const Asset& Modified) { if (this->ID == Modified.ID) { this->Name = Modified.Name; this->Value += Modified.Value; this->FlatProfitPerYear += Modified.FlatProfitPerYear; this->ProfitPercentPerYear += Modified.FlatProfitPerYear; } else throw std::string("Id's not matching"); return *this; } void Asset::SetFlatProfit(double Flat) { this->FlatProfitPerYear = Flat; } void Asset::SetValue(double v) { this->Value = v; } double Asset::GetFlatProfit() { return this->FlatProfitPerYear; } void Asset::SaveState(std::fstream& File) { File << this->Value << ";" << this->ProfitPercentPerYear << ";" << this->FlatProfitPerYear << ";" << this->ID << ";" << this->Name << ";"; } double Asset::GetProfitPercent() { return this->ProfitPercentPerYear; } std::string Asset::GetName() { return this->Name; } double Asset::GetValue() { return this->Value; } double Asset::SimulateProfit(int Years) { return Years * FlatProfitPerYear + Value * pow((ProfitPercentPerYear/100) + 1, Years); } int main() { SavingsWallet wallet; wallet.LoadState(); char firstChoice, subChoice; do { std::cout << "\nMENU\n1. Dodaj Aktyw\n2. Wyswietl Aktywa\n3. Modeluj przyszle oszczednosci\n4. Usun aktyw\n5. Obecne Oszczednosci\n6. Modyfikuj Aktywa7. Zamknac\n"; std::cin >> firstChoice; std::cout << firstChoice; switch (firstChoice) { case 49: { system("cls"); std::cout << "1. Lokata\n2. Nieruchomosc\n3. Konto Emerytalne\n"; std::cin >> subChoice; switch (subChoice) { case 49: { system("cls"); std::string name; double perCent, ammount; std::cout << "Podaj wplacona kwote: \n"; std::cin >> ammount; std::cout << "Podaj oprocentowanie lokaty: \n"; std::cin >> perCent; std::cout << "Podaj nazwe lokaty: \n"; std::cin >> name; wallet.AddAsset(Asset(perCent, 0, ammount, name,1)); system("cls"); }break; case 50: { system("cls"); std::string name; double costs, flat; std::cout << "Podaj miesieczny czynsz: \n"; std::cin >> flat; std::cout << "Podaj roczne koszty zwiazane z nieruchmoscia : \n"; std::cin >> costs; std::cout << "Podaj nazwe nieruchomosci: \n"; std::cin >> name; wallet.AddAsset(Asset(0, 12*flat-costs, 0, name, 3)); system("cls"); }break; case 51: { system("cls"); std::string name; double monthlyTransfers, perCent, moneyIn; std::cout << "Podaj ile juz znajduje sie na koncie: \n"; std::cin >> moneyIn; std::cout << "Podaj miesieczne wplaty na konto: \n"; std::cin >> monthlyTransfers; std::cout << "Podaj oprocentowanie konta: \n"; std::cin >> perCent; std::cout << "Podaj nazwe konta: \n"; std::cin >> name; wallet.AddAsset(Asset(perCent,monthlyTransfers,moneyIn,name,2)); system("cls"); }break; } }break; case 50: { wallet.DisplayAssets(); }break; case 51: { system("cls"); int years; std::cout << "Podaj ilosc lat "; std::cin >> years; if(years==1) std::cout << "Twoje oszczednosci za rok wyniosa: " << wallet.CalculateSavings(years) << "\n"; if(years<5 && years!=1) std::cout << "Twoje oszczednosci za " << years << " lata wyniosa: " << wallet.CalculateSavings(years) << "\n"; else std::cout <<"Twoje oszczednosci za "<< years << " lat wyniosa: "<< wallet.CalculateSavings(years) << "\n"; }break; case 52: { std::string name; std::cout << "Podaj nazwe aktywu ktory chcesz usunac: \n"; std::cin >> name; wallet.RemoveAsset(name); }break; case 53: { system("cls"); std::cout << "Twoje obecne oszczednosci wynosza: " << wallet.CalculateSavings(0) << std::endl; }break; case 54: { std::string editedName; Asset c; system("cls"); std::cout << "Podaj nazwe aktywu ktory chcesz modyfikowac: \n"; std::cin >> editedName; try { c = wallet.GetInfoAboutAsset(editedName); } catch (std::string s) { std::cout << s; continue; } if (c.ID == 1) { double percent, value; std::string newName=";"; std::cout << "Podaj zmiane w ilosci pieniedzy na lokacie\n: \n"; std::cin >> value; std::cout << "Podaj zmiane oprocentowania : \n"; std::cin >> percent; std::cout << "Podaj nowa nazwe lokaty : \n"; std::cin >> newName; if (newName[0] == ';') newName = editedName; Asset modified{ percent,0,value,newName,c.ID }; wallet.EditAsset(modified, editedName); } if (c.ID == 2) { double percent, value, flat; std::string newName = ";"; std::cout << "Podaj zmiane w ilosci pieniedzy na koncie\n: \n"; std::cin >> value; std::cout << "Podaj zmiane w ilosci pieniedzy wplacanych co miesaic na konto\n: \n"; std::cin >> flat; std::cout << "Podaj zmiane oprocentowania\n: \n"; std::cin >> percent; std::cout << "Podaj nowa nazwe konta\n: \n"; std::cin >> newName; if (newName[0] == ';') newName = editedName; Asset modified{ percent,flat,value,newName,c.ID }; wallet.EditAsset(modified, editedName); } if (c.ID == 3) { double flat, value; std::string newName = ";"; std::cout << "Podaj zmiane w miesiecznym czynszu\n: \n"; std::cin >> flat; std::cout << "Podaj nowa nazwe nieruchomosci\n: \n"; std::cin >> newName; if (newName[0] == ';') newName = editedName; Asset modified{ 0,flat,0,newName,c.ID }; wallet.EditAsset(modified, editedName); } }break; default: system("cls"); std::cout << "Nie ma takiej opcji\n"; } } while (firstChoice != 9999); return 0; }