10_PITF: Weitere Arbeit an Modifikation

This commit is contained in:
Johannes Theiner 2018-12-29 15:55:20 +01:00
parent 0d19570306
commit 86ba4d1500
3 changed files with 66 additions and 56 deletions

View File

@ -9,11 +9,11 @@ BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name
} }
std::unique_ptr<BankAccount> BankAccount::operator+(std::unique_ptr<Money> money) { std::unique_ptr<BankAccount> BankAccount::operator+(Money* money) {
std::unique_ptr<Money> ptr = std::make_unique<Money>(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); std::unique_ptr<Money> ptr = std::make_unique<Money>(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency()));
BankAccount::money.swap(ptr); BankAccount::money.swap(ptr);
ptr.release();
return std::make_unique<BankAccount>(this); return std::make_unique<BankAccount>(this);
} }

View File

@ -15,7 +15,7 @@ private:
std::unique_ptr<Money> money; std::unique_ptr<Money> money;
public: public:
explicit BankAccount(std::shared_ptr<Person> owner, std::string name); explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
std::unique_ptr<BankAccount> operator+(std::unique_ptr<Money> money); std::unique_ptr<BankAccount> operator+(Money* money);
std::unique_ptr<Money> operator-(int value); std::unique_ptr<Money> operator-(int value);
bool operator<(std::unique_ptr<BankAccount> bankAccount); bool operator<(std::unique_ptr<BankAccount> bankAccount);
std::string getName(); std::string getName();

View File

@ -24,13 +24,13 @@ std::ostream &operator<<(std::ostream &os, Currency currency) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Bill bill) { std::ostream &operator<<(std::ostream &os, std::unique_ptr<Bill> bill) {
os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial(); os << bill->getValue() << " " << bill->getCurrency() << " " << bill->getSerial();
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Cash cash) { std::ostream &operator<<(std::ostream &os, std::unique_ptr<Cash> cash) {
os << cash.getValue() << " " << cash.getCurrency(); os << cash->getValue() << " " << cash->getCurrency();
return os; return os;
} }
@ -39,45 +39,45 @@ std::ostream &operator<<(std::ostream &os, std::unique_ptr<Money> money) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Person *person) { std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> person) {
os << person->getName(); os << person->getName();
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { std::ostream &operator<<(std::ostream &os, std::unique_ptr<BankAccount> bankAccount) {
os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney(); os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney();
return os; return os;
} }
std::set<Bank *> banks; std::set<std::unique_ptr<Bank>> banks;
std::set<std::unique_ptr<Person>> owners; std::set<std::shared_ptr<Person>> owners;
CashFactory *cashFactory; CashFactory *cashFactory;
void setup() { void setup() {
Bank *bank1 = new Bank("Bank 1"); std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(new Bank("Bank 1"));
Bank *bank2 = new Bank("Bank 2"); std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(new Bank("Bank 2"));
Bank *bank3 = new Bank("Bank 3"); std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(new Bank("Bank 3"));
std::unique_ptr<Person> max = std::make_unique<Person>(new Person("Max Müller")); std::shared_ptr<Person> max = std::make_shared<Person>(new Person("Max Müller"));
std::unique_ptr<Person> marius = std::make_unique<Person>(new Person("Marius Müller")); std::shared_ptr<Person> marius = std::make_shared<Person>(new Person("Marius Müller"));
std::unique_ptr<Person> alex = std::make_unique<Person>(new Person("Alex Maier")); std::shared_ptr<Person> alex = std::make_shared<Person>(new Person("Alex Maier"));
owners.insert(max); owners.insert(max);
owners.insert(marius); owners.insert(marius);
owners.insert(alex); owners.insert(alex);
bank1->addAccount(new BankAccount(max, "Konto 1")); bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 1")));
bank2->addAccount(new BankAccount(max, "Konto 2")); bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 2")));
bank1->addAccount(new BankAccount(alex, "privat")); bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "privat")));
bank3->addAccount(new BankAccount(alex, "geschäftlich 1")); bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 1")));
bank2->addAccount(new BankAccount(alex, "geschäftlich 2")); bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 2")));
bank1->addAccount(new BankAccount(alex, "geschäftlich 3")); bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 3")));
bank2->addAccount(new BankAccount(marius, "Konto 1")); bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 1")));
bank1->addAccount(new BankAccount(marius, "Konto 2")); bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 2")));
bank3->addAccount(new BankAccount(marius, "Konto 3")); bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 3")));
banks.insert(bank1); banks.insert(bank1);
banks.insert(bank2); banks.insert(bank2);
@ -86,40 +86,50 @@ void setup() {
cashFactory = new CashFactory(); cashFactory = new CashFactory();
} }
void simulate() { void forBankAccount(std::unique_ptr<BankAccount> bankAccount) {
for (Bank *bank : banks) { for (int i = 0; i < 10000; ++i) {
std::cout << std::endl; int rnd = distribution(generator);
std::cout << "======================== " << bank->getName() << " ========================" << std::endl; std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
std::cout << std::endl; std::unique_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
for (std::unique_ptr<BankAccount> bankAccount : bank->getAccounts()) { if (bill != nullptr) {
for (int i = 0; i < 10000; ++i) { bankAccount->operator+(bill.get());
int rnd = distribution(generator);
std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
if (bill != nullptr) {
*bankAccount + bill;
}
if (coin != nullptr) {
*bankAccount + coin;
}
Money *fee = *bankAccount - 10;
}
std::cout << *bankAccount << std::endl;
} }
if (coin != nullptr) {
bankAccount->operator+(coin.get());
}
std::unique_ptr<Money> fee = bankAccount->operator-(10);
} }
std::cout << bankAccount.get() << std::endl;
}
void forBank(std::unique_ptr<Bank> bank) {
std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl;
std::set<std::unique_ptr<BankAccount>> bankAccounts = bank->getAccounts();
std::for_each(bankAccounts.begin(), bankAccounts.end(), forBankAccount);
}
void simulate() {
std::for_each(banks.begin(), banks.end(), forBank);
} }
void tearDown() { void tearDown() {
for (Bank *bank : banks) { std::for_each(banks.begin(), banks.end(), [] (std::unique_ptr<Bank> bank) {
for (BankAccount *bankAccount : bank->getAccounts()) { std::set<std::unique_ptr<BankAccount>> bankAccounts = bank->getAccounts();
delete bankAccount; std::for_each(bankAccounts.begin(), bankAccounts.end(), [] (std::unique_ptr<BankAccount> bankAccount){
} *bankAccount.release();
delete bank; });
} *bank.release();
for(Person *person : owners) { });
delete person;
} std::for_each(owners.begin(), owners.end(), [] (std::shared_ptr<Person> person) {
*person.release();
});
} }