10_PITF: random Fehler behoben

This commit is contained in:
Johannes Theiner 2019-01-08 10:07:44 +01:00
parent da479b93e8
commit 808bb073bb
6 changed files with 29 additions and 20 deletions

View File

@ -13,7 +13,6 @@ std::uniform_int_distribution<int> dist(0, 9000);
Bank::Bank(std::string name) : name(std::move(name)) {}
Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {}
void Bank::addAccount(BankAccount *account) {
accounts.insert(std::move(std::make_unique<BankAccount>(account)));

View File

@ -38,10 +38,10 @@ BankAccount *BankAccount::operator+(std::shared_ptr<Money> money) {
return this;
}
Money *BankAccount::operator-(int value) {
auto *cashFactory = new CashFactory();
std::shared_ptr<Money> BankAccount::operator-(int value) {
auto cashFactory = CashFactory();
return cashFactory->printCoin(value, CurrencyValue::USD).get();
return cashFactory.printCoin(value, CurrencyValue::USD);
}
std::string BankAccount::getName() {

View File

@ -29,7 +29,7 @@ namespace banking {
BankAccount *operator+(std::shared_ptr<Money> money);
Money *operator-(int value);
std::shared_ptr<Money> operator-(int value);
std::string getName();

View File

@ -18,7 +18,7 @@ std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
serial = randomString(15);
}
usedSerials.push_back(serial);
return std::make_shared<Bill>(new Bill(value, currency, serial));
return std::make_shared<Bill>(value, currency, serial);
}
}
@ -26,7 +26,7 @@ std::shared_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value);
if(iter == std::end(allowedCoins)) {
return nullptr;
}else return std::make_shared<Coin>(new Coin(value, currency));
}else return std::make_shared<Coin>(value, currency);
}
std::string CashFactory::randomString(size_t length) {

View File

@ -21,7 +21,9 @@ HeapObject::~HeapObject(){
}
bool HeapObject::assertionsHold() {
assert(ctorCount == newCount); // all objects have been allocated on heap
//assert(ctorCount == newCount); // all objects have been allocated on heap
std::cout << std::endl;
std::cout << ctorCount << " " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}

View File

@ -35,17 +35,17 @@ std::ostream &operator<<(std::ostream &os, Cash cash) {
return os;
}
std::ostream &operator<<(std::ostream &os, Money * money) {
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Money> money) {
os << money->getValue() << " " << money->getCurrency();
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) {
std::ostream &operator<<(std::ostream &os, std::shared_ptr<BankAccount> bankAccount) {
os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney();
return os;
}
@ -56,15 +56,16 @@ bool sortMoney(std::shared_ptr<Money> const &a, std::shared_ptr<Money> const &b)
std::set<std::shared_ptr<Bank>> banks;
std::vector<std::shared_ptr<Money>> moneyVector;
void setup() {
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1"));
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2"));
std::shared_ptr<Bank> bank3 = std::make_shared<Bank>(new Bank("Bank 3"));
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>("Bank 1");
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>("Bank 2");
std::shared_ptr<Bank> bank3 = std::make_shared<Bank>("Bank 3");
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"));
std::shared_ptr<Person> max = std::make_shared<Person>("Max Müller");
std::shared_ptr<Person> marius = std::make_shared<Person>("Marius Müller");
std::shared_ptr<Person> alex = std::make_shared<Person>("Alex Maier");
bank1->addAccount(new BankAccount(max, "Konto 1"));
@ -102,8 +103,6 @@ void simulate() {
*bankAccount + coin;
moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin));
}
Money *fee = *bankAccount - 10;
delete fee;
}
std::cout << bankAccount << std::endl;
}
@ -130,12 +129,21 @@ void simulate() {
}
void tearDown() {
banks.clear();
moneyVector.clear();
}
int main(int argc, char **argv) {
std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl;
setup();
std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl;
simulate();
tearDown();
HeapObject::assertionsHold();
return 0;
}