Project Finale: Main Loop
Part of the Object Oriented Programming section of Coddy's C journey — lesson 27 of 61.
Challenge
EasyCongratulations—you've built a complete Bank Account library! Now it's time to switch roles. Instead of being the library creator, you'll become the library user. Your task is to write a main program that puts your Account module through its paces.
Think of this as a customer walking into a bank and performing a series of transactions. You'll create an account, make deposits and withdrawals, check the balance along the way, and finally close the account when you're done.
You'll work with three files:
account.h: Your complete header file with the opaqueAccounttype and all function prototypes:create_account,destroy_account,deposit,withdraw, andget_balance.account.c: Your complete implementation with the hidden struct containingidandbalance, plus all the function implementations you've built throughout this project.main.c: This is where you shine! Write a program that demonstrates the full lifecycle of a bank account through a realistic sequence of transactions.
You will receive five inputs: the account ID (an integer), a first deposit, a withdrawal, a second deposit, and an attempted overdraft (a withdrawal that should fail due to insufficient funds).
Your main program should tell the story of this account's life:
- Create the account and announce it
- Show the starting balance
- Make the first deposit and show the new balance
- Withdraw some funds and show the updated balance
- Make another deposit and show the balance
- Attempt a withdrawal that exceeds the balance—this should fail
- Show the final balance (unchanged after the failed withdrawal)
- Close the account
Print the output in this format:
Account 2001 created
Balance: 0.00
Deposited 1000.00
Balance: 1000.00
Withdrew 250.00
Balance: 750.00
Deposited 500.00
Balance: 1250.00
Withdrawal failed: insufficient funds
Balance: 1250.00
Account 2001 closedWhere 2001 is the account ID, and the amounts reflect your inputs. Format all monetary values with two decimal places using %.2f. The failed withdrawal message should print when withdraw returns 0.
This finale demonstrates everything you've learned about encapsulation—your main program interacts with the Account purely through its public interface, never touching the hidden internal data directly.
Try it yourself
#include <stdio.h>
#include "account.h"
int main() {
// TODO: Read account_id, deposit1, withdraw_amount, deposit2, and overdraft from input
// TODO: Create the account and print "Account <id> created"
// TODO: Print the starting balance
// TODO: Make the first deposit, print "Deposited <amount>" and the new balance
// TODO: Withdraw funds, print "Withdrew <amount>" and the updated balance
// TODO: Make the second deposit, print "Deposited <amount>" and the new balance
// TODO: Attempt the overdraft withdrawal; if it fails, print "Withdrawal failed: insufficient funds"
// TODO: Print the final balance
// TODO: Print "Account <id> closed" and destroy the account
return 0;
}
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy