Ledger: Withdraw Inventory
Lesson 44 of 48 in Coddy's C for Rust Developers course.
Challenge
MediumContinue the same modules. Add TAKE name quantity for nonnegative quantities. Take succeeds only for an existing name with sufficient stock; rejection leaves state unchanged. Zero take succeeds only for an existing name. Retain zero-balance entries. Update report.c: stage 1 keeps its old format; stages 2 and 3 report totalUnits:accepted:rejected. Inputs here are valid ADD or TAKE commands. First line is the stage number shown in each test. Later lines are commands. Keep the same files and public header through all four steps. The locked driver is identical in every step and tests independent ledgers directly. There are at most 16 distinct names, each 1 to 15 lowercase ASCII letters, and at most 64 command lines shorter than 128 bytes. All accumulated totals fit int.
Edit the unlocked source and header files. Keep the declared public API compatible with the visible locked main.c, which calls it directly. Do not add another main. All numeric task fixtures are small enough to avoid overflow unless the task explicitly checks a range. Print only the specified output; the driver supplies input parsing and output when shown.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){
Ledger probe,other;ledger_init(&probe);ledger_init(&other);Command sample={"ADD","probe",2};
if(!ledger_apply(&probe,&sample) || probe.count!=1 || probe.items[0].quantity!=2 || other.count!=0)return 3;
int stage;if(scanf("%d",&stage)!=1 || stage<1 || stage>4)return 1;
int ch;while((ch=getchar())!='\n' && ch!=EOF){}
Ledger ledger;ledger_init(&ledger);int accepted=0,rejected=0;char line[128];
while(fgets(line,sizeof line,stdin)){line[strcspn(line,"\n")]=0;Command command;
if(parse_command(line,&command) && ledger_apply(&ledger,&command))accepted++;else rejected++;
}
char output[1024];Ledger before=ledger;
if(!ledger_report(&ledger,stage,accepted,rejected,output,sizeof output))return 2;
if(ledger.count!=before.count)return 3;
for(size_t i=0;i<ledger.count;i++)if(strcmp(ledger.items[i].name,before.items[i].name)!=0 || ledger.items[i].quantity!=before.items[i].quantity)return 3;
puts(output);return 0;
}
All lessons in C for Rust Developers
1Compile and Compute
Run C Across FilesPrinting and CommentsTypes and InitializationInteger Division and RangeFloating ArithmeticRecap: Receipt Total4Functions and Interfaces
Function Arguments Are ValuesHeaders and LinkageMultiple Results via PointersConst Views and MutationRecap: Price Breakdown2Decisions and Loops
Booleans and BranchesLogical OperatorsSwitch and Enumerated CasesLoop Bounds and ProgressRecap: Admission Rules3Arrays and Text
Arrays Need LengthsBounds Before IndexesStrings and TerminatorsString Content and IdentityRecap: Reading Statistics6Validation and Text Buffers
Parsing with Status ResultsRange Checks and strtolCapacity and Safe FormattingLookup Without a DictionaryRecap: Validate Bounded Text9Project: Stock Ledger
Ledger: Add InventoryLedger: Withdraw InventoryPractice on your own: Online C compiler