CJCoding With Joseph
15free / daySign inCreate account

Bank Ledger File

Build a class Ledger that records bank transactions in a text file and recomputes the balance from that file every time it is asked — nothing is cached.

File format: one transaction per line, a letter, a space, then the amount with exactly two decimals: D 50.00 for a deposit, W 20.00 for a withdrawal. Write amounts with fixed << setprecision(2) (include <iomanip>).

Implement:
- Ledger(const string& filename): stores the filename. The test creates the empty file first.
- void deposit(double amount): appends a D line (open in ios::out | ios::app).
- bool withdraw(double amount): if the balance is at least amount, appends a W line and returns true; otherwise appends nothing and returns false.
- double balance() const: opens in ios::in, reads letter >> amount pairs until reading fails, and returns deposits minus withdrawals (0 for an empty file).
- int transactions() const: the number of lines in the file.

The test prints balance() with fixed << setprecision(2), withdraw results as true/false, and transactions(), one per line. For deposit(100), withdraw(30), then balance(), withdraw(100), transactions() it prints the output shown in Expected Output.

Do NOT write a main function.

Expected Output:

70.00
false
2
true
0.00
3
D 20.50
D 4.25
W 24.75

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Streamsfstream
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.