Update implemented removeCurrency

This commit is contained in:
Lev
2021-06-12 20:46:33 -05:00
parent 40750a8d28
commit 016d72d795
4 changed files with 30 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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");