Deposit Logic
Part of the Object Oriented Programming section of Coddy's C journey — lesson 24 of 61.
Challenge
EasyTime to add the first real banking operation to your Account module—the ability to deposit funds! A deposit function needs to be smart: it should only accept positive amounts, protecting your account from invalid transactions.
Building on your existing Bank Account project, you'll extend the module with deposit functionality. Your deposit function must validate the input before modifying the hidden balance—this is encapsulation working to protect your data's integrity.
You'll work with three files:
account.h: Extend your existing header by adding a new function prototype:deposit. This function should take an Account pointer and adoubleamount, returning anint(1 for success, 0 if the amount is invalid). Keep your existing declarations forcreate_accountanddestroy_account.account.c: Implement thedepositfunction alongside your existing constructor and destructor. The deposit should only succeed if the amount is positive (greater than zero). If valid, add the amount to the hidden balance and return 1. If the amount is zero or negative, reject it by returning 0 without changing the balance.main.c: Demonstrate your deposit functionality by creating an account and performing multiple deposit operations—some valid, some invalid—to show that your validation works correctly.
You will receive three inputs: the account ID (an integer), a valid deposit amount (a positive number), and an invalid deposit amount (zero or negative).
In your main file, create an account with the given ID and print a confirmation. Then attempt to deposit the invalid amount first—print whether it succeeded and note that the balance should remain unchanged. Next, deposit the valid amount—print the success result. Finally, destroy the account and confirm cleanup.
Print the output in this format:
Account 1001 created
Deposit -50.00: 0
Deposit 200.00: 1
Account destroyedWhere 1001 is the account ID, -50.00 is the invalid amount, and 200.00 is the valid amount. Format all amounts with two decimal places using %.2f.
Your deposit function acts as a gatekeeper—it ensures only valid transactions can modify the account's hidden balance. Even though you can't see the balance yet (we'll add a getter in a later lesson), you can trust that invalid deposits are being rejected properly.
Try it yourself
#include <stdio.h>
#include "account.h"
int main() {
int account_id;
scanf("%d", &account_id);
Account* account = create_account(account_id);
printf("Account %d created\n", account_id);
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