diff --git a/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.cpp b/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.cpp index 474c6c8..48ebbd0 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.cpp +++ b/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.cpp @@ -24,6 +24,29 @@ void Wallet::insertCurrency(std::string type, double amount) currencies[type] = balance; } +bool Wallet::removeCurrency(std::string type, double amount) +{ + double balance; + if(amount < 0) + { + return false; + } + if(currencies.count(type) == 0) // currency not in wallet + { + return false; + } + else // is there, do we have enough? + { + if(containsCurrency(type, amount)) + { + currencies[type] -= amount; + return true; + } + else // didn't have enough + return false; + } +} + bool Wallet::containsCurrency(std::string type, double amount) { if(currencies.count(type) == 0) // not in wallet yet @@ -44,4 +67,4 @@ std::string Wallet::toString() s += currency + " : " + std::to_string(amount) + "\n"; } return s; -} \ No newline at end of file +} diff --git a/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.h b/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.h index 3c68252..b147aae 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.h +++ b/CM2005 Object Oriented Programming/Topic 5/5.1.9/Wallet.h @@ -10,6 +10,9 @@ class Wallet /** insert currency to the wallet */ void insertCurrency(std::string type, double amount); + /** remove currency from the wallet */ + bool removeCurrency(std::string type, double amount); + /** check if the wallet contains at least this much currency */ bool containsCurrency(std::string type, double amount); diff --git a/CM2005 Object Oriented Programming/Topic 5/5.1.9/a.exe b/CM2005 Object Oriented Programming/Topic 5/5.1.9/a.exe index d08a97e..e7a47ed 100644 Binary files a/CM2005 Object Oriented Programming/Topic 5/5.1.9/a.exe and b/CM2005 Object Oriented Programming/Topic 5/5.1.9/a.exe differ diff --git a/CM2005 Object Oriented Programming/Topic 5/5.1.9/main.cpp b/CM2005 Object Oriented Programming/Topic 5/5.1.9/main.cpp index 45cbe4e..303e1e5 100644 --- a/CM2005 Object Oriented Programming/Topic 5/5.1.9/main.cpp +++ b/CM2005 Object Oriented Programming/Topic 5/5.1.9/main.cpp @@ -13,7 +13,9 @@ int main() Wallet wallet; wallet.insertCurrency("BTC", 10); wallet.insertCurrency("USDT", 100.83); - std::cout << "Wallet has BTC " << wallet.containsCurrency("USDT", 100.50) << std::endl; + std::cout << wallet.toString() << std::endl; + //std::cout << "Wallet has BTC " << wallet.containsCurrency("USDT", 100.50) << std::endl; + wallet.removeCurrency("USDT", 10); std::cout << wallet.toString() << std::endl; //CSVReader::readCSV("20200317.csv");