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()));
BankAccount::money.swap(ptr);
ptr.release();
return std::make_unique<BankAccount>(this);
}

View File

@ -15,7 +15,7 @@ private:
std::unique_ptr<Money> money;
public:
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);
bool operator<(std::unique_ptr<BankAccount> bankAccount);
std::string getName();

View File

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