CJCoding With Joseph
15free / daySign inCreate account

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.

Hints4 available
Topics:
Exceptions
📥 Auto-Input:
10 1\n5 2\n5 1\n6 0\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.