10_PITF: weniger Fehlermeldungen

This commit is contained in:
Johannes Theiner 2018-12-29 23:28:16 +01:00
parent 86ba4d1500
commit 5d1a567415
4 changed files with 55 additions and 101 deletions

View File

@ -10,11 +10,11 @@ BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name
} }
std::unique_ptr<BankAccount> BankAccount::operator+(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>(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);
} }
std::unique_ptr<Money> BankAccount::operator-(int value) { std::unique_ptr<Money> BankAccount::operator-(int value) {
@ -26,6 +26,3 @@ std::unique_ptr<Money> BankAccount::operator-(int value) {
bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) { bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) {
return money->getValue() < bankAccount->getMoney()->getValue(); return money->getValue() < bankAccount->getMoney()->getValue();
} }

View File

@ -4,38 +4,32 @@ Money::Money(int value, Currency currency) : value(value), currency(currency) {
} }
Money& Money::operator=(Money money) { std::unique_ptr<Money> Money::operator=(Money money) {
value = money.getValue(); value = money.getValue();
currency = money.getCurrency(); currency = money.getCurrency();
return *this; return std::make_unique<Money>(*this);
} }
Money &Money::operator=(Money *money) { std::unique_ptr<Money> Money::operator+(Money &a) {
value = money->getValue();
currency = money->getCurrency();
return *this;
}
Money Money::operator+(Money &a) {
if(currency == a.currency) { if(currency == a.currency) {
return Money(value + a.value); return std::make_unique<Money>(Money(value + a.value));
}else return Money(-1, Currency(CurrencyValue::USD)); }else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
} }
Money Money::operator-(Money &a) { std::unique_ptr<Money> Money::operator-(Money &a) {
if(currency == a.currency) { if(currency == a.currency) {
return Money(value - a.value); return std::make_unique<Money>(Money(value - a.value));
}else return Money(-1, Currency(CurrencyValue::USD)); }else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
} }
const std::unique_ptr<Money> Money::operator++(int) { const std::unique_ptr<Money> Money::operator++(int) {
value++; value++;
return std::make_unique<Money>(this); return std::make_unique<Money>(*this);
} }
const std::unique_ptr<Money> Money::operator--(int) { const std::unique_ptr<Money> Money::operator--(int) {
value--; value--;
return std::make_unique<Money>(this); return std::make_unique<Money>(*this);
} }
int Money::getValue() { int Money::getValue() {
@ -45,21 +39,3 @@ int Money::getValue() {
Currency Money::getCurrency() { Currency Money::getCurrency() {
return currency; return currency;
} }
std::unique_ptr<Money> Money::operator+(std::unique_ptr<Money> &a) {
if(currency == a->getCurrency()) {
return std::make_unique<Money>(Money(value + a->getValue()));
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::unique_ptr<Money> Money::operator-(std::unique_ptr<Money> &a) {
if(currency == a->getCurrency()) {
return std::make_unique<Money>(Money(value - a->getValue()));
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::unique_ptr<Money> Money::operator=(std::unique_ptr<Money> money) {
value = money->getValue();
currency = money->getCurrency();
return std::make_unique<Money>(Money(value, currency));
}

View File

@ -12,9 +12,8 @@ private:
public: public:
explicit Money(int value = 0, Currency currency = CurrencyValue::USD); explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
std::unique_ptr<Money> operator=(Money money); std::unique_ptr<Money> operator=(Money money);
std::unique_ptr<Money> operator=(std::unique_ptr<Money> money); std::unique_ptr<Money> operator+(Money &a);
std::unique_ptr<Money> operator+(std::unique_ptr<Money> &a); std::unique_ptr<Money> operator-(Money &a);
std::unique_ptr<Money> operator-(std::unique_ptr<Money> &a);
const std::unique_ptr<Money> operator++(int); const std::unique_ptr<Money> operator++(int);
const std::unique_ptr<Money> operator--(int); const std::unique_ptr<Money> operator--(int);

View File

@ -55,38 +55,42 @@ CashFactory *cashFactory;
void setup() { void setup() {
std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(Bank("Bank 1"));
std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(Bank("Bank 2"));
std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(Bank("Bank 3"));
std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(new Bank("Bank 1")); std::shared_ptr<Person> max = std::make_shared<Person>(Person("Max Müller"));
std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(new Bank("Bank 2")); std::shared_ptr<Person> marius = std::make_shared<Person>(Person("Marius Müller"));
std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(new Bank("Bank 3")); std::shared_ptr<Person> alex = std::make_shared<Person>(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(max);
owners.insert(marius); owners.insert(marius);
owners.insert(alex); owners.insert(alex);
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 1"))); bank1->addAccount(std::make_unique<BankAccount>(BankAccount(max, "Konto 1")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 2"))); bank2->addAccount(std::make_unique<BankAccount>(BankAccount(max, "Konto 2")));
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "privat"))); bank3->addAccount(std::make_unique<BankAccount>(BankAccount(alex, "privat")));
bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 1"))); bank1->addAccount(std::make_unique<BankAccount>(BankAccount(alex, "geschäftlich 1")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 2"))); bank2->addAccount(std::make_unique<BankAccount>(BankAccount(alex, "geschäftlich 2")));
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 3"))); bank3->addAccount(std::make_unique<BankAccount>(BankAccount(alex, "geschäftlich 3")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 1"))); bank1->addAccount(std::make_unique<BankAccount>(BankAccount(marius, "Konto 1")));
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 2"))); bank2->addAccount(std::make_unique<BankAccount>(BankAccount(marius, "Konto 2")));
bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 3"))); bank3->addAccount(std::make_unique<BankAccount>(BankAccount(marius, "Konto 3")));
banks.insert(bank1); banks.insert(std::move(bank1));
banks.insert(bank2); banks.insert(std::move(bank2));
banks.insert(bank3); banks.insert(std::move(bank3));
cashFactory = new CashFactory(); cashFactory = new CashFactory();
} }
void forBankAccount(std::unique_ptr<BankAccount> bankAccount) { void simulate() {
for(auto && bank : banks) {
std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl;
for(auto && bankAccount : bank->getAccounts()) {
for (int i = 0; i < 10000; ++i) { for (int i = 0; i < 10000; ++i) {
int rnd = distribution(generator); int rnd = distribution(generator);
std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD); std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
@ -102,34 +106,12 @@ void forBankAccount(std::unique_ptr<BankAccount> bankAccount) {
} }
std::cout << bankAccount.get() << 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() { void tearDown() {
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();
});
} }