Withdraw Logic
Part of the Object Oriented Programming section of Coddy's C journey — lesson 25 of 61.
Challenge
EasyYour Bank Account is growing! Now let's add the ability to withdraw funds. Unlike deposits, withdrawals have an extra constraint—you can't take out more money than you have. Your withdraw function needs to check the balance before allowing the transaction.
Building on your existing Bank Account project, you'll extend the module with withdrawal functionality. This is where encapsulation really shines—your function can access the hidden balance to verify sufficient funds, something external code could never do directly.
You'll work with three files:
account.h: Add a new function prototype:withdraw. This function takes an Account pointer and adoubleamount, returning anint(1 for success, 0 if the withdrawal fails). Keep all your existing declarations forcreate_account,destroy_account, anddeposit.account.c: Implement thewithdrawfunction. A withdrawal should only succeed if two conditions are met: the amount must be positive (greater than zero), AND the account must have sufficient funds (balance >= amount). If both conditions pass, subtract the amount from the balance and return 1. Otherwise, return 0 without modifying the balance.main.c: Demonstrate your complete deposit and withdraw workflow. Create an account, make a deposit to establish a balance, then test withdrawals—both successful and unsuccessful ones.
You will receive four inputs: the account ID (an integer), a deposit amount (to establish initial funds), a successful withdrawal amount (less than or equal to the deposited amount), and a failed withdrawal amount (more than the remaining balance).
In your main file, create an account and print a confirmation. Deposit the initial funds and print the result. Then perform the successful withdrawal and print whether it succeeded. Finally, attempt the withdrawal that should fail (insufficient funds) and print that result. Destroy the account and confirm cleanup.
Print the output in this format:
Account 1001 created
Deposit 500.00: 1
Withdraw 200.00: 1
Withdraw 400.00: 0
Account destroyedWhere 1001 is the account ID, 500.00 is the deposit, 200.00 is the successful withdrawal, and 400.00 is the failed withdrawal (because only 300.00 remains after the first withdrawal). Format all amounts with two decimal places using %.2f.
Your withdraw function demonstrates the power of encapsulation—it can check the hidden balance internally to make decisions, while external code has no way to bypass this validation or peek at the balance directly.
Try it yourself
#include <stdio.h>
#include "account.h"
int main() {
int account_id;
scanf("%d", &account_id);
double valid_amount;
scanf("%lf", &valid_amount);
double invalid_amount;
scanf("%lf", &invalid_amount);
Account* account = create_account(account_id);
printf("Account %d created\n", account_id);
int result_invalid = deposit(account, invalid_amount);
printf("Deposit %.2f: %d\n", invalid_amount, result_invalid);
int result_valid = deposit(account, valid_amount);
printf("Deposit %.2f: %d\n", valid_amount, result_valid);
destroy_account(account);
printf("Account destroyed\n");
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