Menu
Coddy logo textTech

Project Finale: Main Loop

Part of the Object Oriented Programming section of Coddy's C journey — lesson 27 of 61.

challenge icon

Challenge

Easy

Congratulations—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 opaque Account type and all function prototypes: create_account, destroy_account, deposit, withdraw, and get_balance.
  • account.c: Your complete implementation with the hidden struct containing id and balance, 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:

  1. Create the account and announce it
  2. Show the starting balance
  3. Make the first deposit and show the new balance
  4. Withdraw some funds and show the updated balance
  5. Make another deposit and show the balance
  6. Attempt a withdrawal that exceeds the balance—this should fail
  7. Show the final balance (unchanged after the failed withdrawal)
  8. 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 closed

Where 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