Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compiled with: g++ -Wall -std=c++14 -pthread
- #include <iostream>
- #include <memory>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <algorithm>
- enum class AccountType
- {
- SavingAccount,
- RegularAccount,
- StudentAccount
- };
- //h
- class IAccount
- {
- public:
- IAccount() { std::cout << "Account created" << std::endl; }
- virtual ~IAccount() { std::cout << "Account destroyed" << std::endl; }
- virtual void deposit(double money) = 0;
- virtual void withdraw(double money) = 0;
- virtual void printInfo() const = 0;
- virtual int Id() const = 0;
- virtual int Id() = 0;
- };
- class SavingAccount : public IAccount
- {
- private:
- std::string name;
- int ID;
- double interest;
- double savedMoney;
- public:
- SavingAccount(const std::string& name, int ID, double interest, int money = 100)
- : IAccount()
- , name(name)
- , ID(ID)
- , interest(interest)
- , savedMoney(money)
- {
- }
- void deposit(double money) override {
- if (money >= 2000) {
- money = money + money * interest / 100;
- }
- savedMoney += money;
- }
- void withdraw(double money) override {
- int remaining = savedMoney - money;
- if (remaining >= -1000) {
- savedMoney = remaining;
- }
- }
- void applyInterest(double interest) {
- savedMoney = savedMoney + savedMoney * interest / 100;
- }
- void printInfo() const override {
- std::stringstream ss;
- ss << "Saving account " << name << " " << ID << " " << savedMoney;
- std::cout << ss.str() << std::endl;
- }
- int Id() const override { return ID; }
- int Id() override { return ID; }
- bool operator==(const SavingAccount& rhs) { return ID == rhs.ID; }
- bool operator!=(const SavingAccount& rhs) { return !operator==(rhs); }
- bool operator< (const SavingAccount& rhs) { return savedMoney < rhs.savedMoney; }
- bool operator> (const SavingAccount& rhs) { return operator< (rhs); }
- bool operator<=(const SavingAccount& rhs) { return !operator> (rhs); }
- bool operator>=(const SavingAccount& rhs) { return !operator< (rhs); }
- };
- class RegularAccount : public IAccount
- {
- private:
- std::string name;
- int ID;
- double savedMoney;
- public:
- RegularAccount(const std::string& name, int ID, int money = 0)
- : IAccount()
- , name(name)
- , ID(ID)
- , savedMoney(money)
- {
- }
- void deposit(double money) override {
- savedMoney += money;
- if (money >= 2000) {
- savedMoney += 50;
- }
- }
- void withdraw(double money) override {
- int remaining = savedMoney - money;
- if (remaining > 0) {
- savedMoney = remaining;
- }
- }
- void printInfo() const override {
- std::stringstream ss;
- ss << "Regular account " << name << " " << ID << " " << savedMoney;
- std::cout << ss.str() << std::endl;
- }
- int Id() const override { return ID; }
- int Id() override { return ID; }
- bool operator==(const RegularAccount& rhs) { return ID == rhs.ID; }
- bool operator!=(const RegularAccount& rhs) { return !operator==(rhs); }
- bool operator< (const RegularAccount& rhs) { return savedMoney < rhs.savedMoney; }
- bool operator> (const RegularAccount& rhs) { return operator< (rhs); }
- bool operator<=(const RegularAccount& rhs) { return !operator> (rhs); }
- bool operator>=(const RegularAccount& rhs) { return !operator< (rhs); }
- };
- class StudentAccount : public IAccount
- {
- private:
- std::string name;
- int ID;
- double savedMoney;
- bool bonusApplied = false;
- public:
- StudentAccount(const std::string& name, int ID, int money = 50)
- : IAccount()
- , name(name)
- , ID(ID)
- , savedMoney(money)
- {
- }
- void deposit(double money) override {
- savedMoney += money;
- if (savedMoney >= 1000 && !bonusApplied)
- {
- bonusApplied = true;
- savedMoney += 50;
- }
- }
- void withdraw(double money) override {
- int remaining = savedMoney - money;
- if (remaining > 0) {
- savedMoney = remaining;
- }
- }
- void printInfo() const override {
- std::stringstream ss;
- ss << "Student account " << name << " " << ID << " " << savedMoney;
- std::cout << ss.str() << std::endl;
- }
- int Id() const override { return ID; }
- int Id() override { return ID; }
- bool operator==(const StudentAccount& rhs) { return ID == rhs.ID; }
- bool operator!=(const StudentAccount& rhs) { return !operator==(rhs); }
- bool operator< (const StudentAccount& rhs) { return savedMoney < rhs.savedMoney; }
- bool operator> (const StudentAccount& rhs) { return operator< (rhs); }
- bool operator<=(const StudentAccount& rhs) { return !operator> (rhs); }
- bool operator>=(const StudentAccount& rhs) { return !operator< (rhs); }
- };
- struct AccountNotImplemented : public std::exception
- {
- const char* what() const throw ()
- {
- return "This type of account is not implemented";
- }
- };
- //h
- class AccountFactory
- {
- public:
- AccountFactory() {}
- std::unique_ptr< IAccount > createAccount(AccountType accountType, std::string name, int Id, int money, double interest = 0.15f) const { switch (accountType) {
- case AccountType::SavingAccount:
- {
- return std::make_unique<SavingAccount>(name, Id, interest, money);
- }
- case AccountType::RegularAccount:
- {
- return std::make_unique<RegularAccount>(name, Id, money);
- }
- case AccountType::StudentAccount:
- {
- return std::make_unique<StudentAccount>(name, Id, money);
- }
- default:
- throw new AccountNotImplemented();
- }
- }
- };
- class AccountManager {
- private:
- std::ifstream input;
- std::vector <std::unique_ptr<IAccount>> accounts;
- public:
- AccountManager(std::string fileName) {
- input.open(fileName);
- accounts = readAccounts();
- }
- std::vector<std::unique_ptr<IAccount>> readAccounts() {
- std::string accountType;
- int Id;
- std::string firstName;
- std::string lastName;
- int money;
- double interest;
- AccountFactory factory;
- std::vector < std::unique_ptr< IAccount >> accounts;
- while (input >> accountType >> Id >> firstName >> lastName >> money) {
- interest = 0;
- if (accountType == "SA") {
- input >> interest;
- accounts.emplace_back(factory.createAccount(AccountType::SavingAccount, firstName + " " + lastName, Id, money, interest));
- }
- if (accountType == "ST") {
- accounts.emplace_back(factory.createAccount(AccountType::StudentAccount, firstName + " " + lastName, Id, money));
- }
- if (accountType == "RG") {
- accounts.emplace_back(factory.createAccount(AccountType::RegularAccount, firstName + " " + lastName, Id, money));
- }
- }
- return accounts;
- }
- IAccount* find(int Id) {
- auto value = std::find_if(accounts.begin(), accounts.end(), [Id](std::unique_ptr<IAccount>& account) { return Id == account->Id(); });
- if (value == accounts.end()) {
- return nullptr;
- }
- return value->get();
- }
- void runProgram() {
- int Id;
- char operation;
- double money;
- std::cout << "Id: ";
- std::cin >> Id;
- IAccount* account = find(Id);
- if (account == nullptr) { std::cout << "Account does not exist" << std::endl; return; }
- while (true) {
- std::cout << "Operations: i - getInfo, d - deposit, w - withdraw" << std::endl;
- std::cin >> operation;
- switch (operation) {
- case 'i':
- account->printInfo();
- break;
- case 'd':
- std::cout << "How much: ";
- std::cin >> money;
- account->deposit(money);
- break;
- case 'w':
- std::cout << "How much: ";
- std::cin >> money;
- account->withdraw(money);
- break;
- default:
- std::cout << "Operation not supported";
- }
- }
- }
- };
- int main() {
- AccountManager manager("Accounts.txt");
- manager.runProgram();
- return 0;
- }
- =============
- SA 1 Marek Jankovic 2339 4
- ST 2 Maria Cierna 435
- RG 3 Martin Polak 626
- SA 4 Martina Datolova 27953 6
- RG 5 Ivan Richret 217
- ST 6 Stano Polak 4241
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement