← Back to Streams
Question 668
Bank Ledger File
Bank Ledger File
Build a classLedgerthat 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.00for a deposit,W 20.00for a withdrawal. Write amounts withfixed << setprecision(2)(include<iomanip>). Implement: -Ledger(const string& filename): stores the filename. The test creates the empty file first. -void deposit(double amount): appends aDline (open inios::out | ios::app). -bool withdraw(double amount): if the balance is at leastamount, appends aWline and returnstrue; otherwise appends nothing and returnsfalse. -double balance() const: opens inios::in, readsletter >> amountpairs 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 printsbalance()withfixed << setprecision(2),withdrawresults astrue/false, andtransactions(), one per line. Fordeposit(100),withdraw(30), thenbalance(),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
Sign in to use hintsTopics:
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.