A Purchase That Changes Nothing When It Fails
A Purchase That Changes Nothing When It Fails
A Wallet holds coins and gems, and a purchase costs some of each. The rule is the strong guarantee: when a purchase fails, the wallet must look exactly as it did before, so every check runs BEFORE anything is subtracted.
- InsufficientFundsError inherits from std::runtime_error; its explicit constructor takes a const std::string& message and passes it on
- void Wallet::buy(int coinCost, int gemCost): when coinCost is more than the coins, throw InsufficientFundsError("not enough coins"); when gemCost is more than the gems, throw InsufficientFundsError("not enough gems"); only when both checks pass, subtract both costs
The tests read the starting coins and gems, then pairs coinCost gemCost. After each attempt they print bought or failed: with what(), then wallet: , the coins, coins, , the gems and gems. For the first test the input is 10 1 followed by 5 2, 5 1 and 6 0; the first purchase has enough coins but not enough gems, and the coins must still be 10 afterwards.
Do NOT write a main function.
Expected Output:
failed: not enough gems wallet: 10 coins, 1 gems bought wallet: 5 coins, 0 gems failed: not enough coins wallet: 5 coins, 0 gems
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...