Area 03 · File handling
Supply inventory in a CSV file
Keep an office-supply inventory in items.csv so that it survives between runs, with add, change and delete commands.
- Spec no.
- HO-06
- Level
- Exam-level
- Time box
- 2 h
- Reference
- Java · C#
- Grading
- Self-assessed · rubric is ours, not DICT's
01 Coverage
| Area | Sub-skills practised | Role |
|---|---|---|
| 01 Data manipulation | Validation, String | Also |
| 03 File handling | Input/output validation, Create, Read, Update, Delete | Primary |
Sign up to start coding practice
Read the task freely. An account is required before editing and running a solution. Code runs also need an internet connection.
Sign up or sign in02 Requirements
- On start, load items.csv if it exists: code,name,qty,price per line. Code is two capital letters and three digits; name is 1 to 30 letters, digits, - or _; qty and price are not negative.
- Skip any malformed or duplicate line with Warning: skipped line k.
- Commands: LIST, ADD code name qty price, UPDATE code delta, DELETE code, QUIT.
- ADD rejects invalid fields (Invalid item) and existing codes (Duplicate code X). Codes are case-sensitive: st004 is invalid.
- UPDATE adds a signed delta; reject a result below 0 with Rejected: X would go below 0.
- Save on QUIT (and when input ends) by writing to items.csv.tmp and then replacing items.csv, so a crash cannot leave half a file.
03 Input and output
- items.csv (given below) is in the working folder
- Commands on standard input
- Warnings while loading
- One line per command
- Saved n items on QUIT
- items.csv rewritten in insertion order
LIST
ADD FL003 Folder 50 6.00
ADD FL003 Folder 5 6.00
ADD st004 Stapler 2 90
UPDATE BD002 -20
UPDATE BD002 10
DELETE PN001
LIST
QUITWarning: skipped line 3
PN001 Ballpen 120 8.50
BD002 BondPaper 15 245.00
Added FL003
Duplicate code FL003
Invalid item
Rejected: BD002 would go below 0
Updated BD002 to 25
Deleted PN001
BD002 BondPaper 25 245.00
FL003 Folder 50 6.00
Saved 2 itemsPN001,Ballpen,120,8.50
BD002,BondPaper,15,245.00
xx9,Broken,1,1BD002,BondPaper,25,245.00
FL003,Folder,50,6.00Match the sample exactly first, then write at least two test inputs of your own.
04 Edge cases to test
- No items.csv at all
- A line with a bad code (xx9)
- UPDATE with a non-number
- DELETE of an unknown code
05 Starter outline and hints
load(): read lines → parse → map[code] or warnloop commands: LIST / ADD / UPDATE / DELETE / QUITsave(): write tmp, then move over items.csv
Hint 1
Split with a limit of -1 so trailing empty fields are not dropped.
Hint 2
An insertion-ordered map keeps the file order stable.
06 Practice rubric
| Criterion | Sub-skills | Look for | Pts |
|---|---|---|---|
| Read and validate | ReadInput/output validation | Loads good lines, warns on bad ones, survives a missing file. | 25 |
| Create / update / delete | CreateUpdateDelete | Every command behaves as specified and keeps codes unique. | 30 |
| Safe write | Update | Temp file then replace; file closed before the move. | 20 |
| Field validation | Validation | One parse/validate function used for both file lines and ADD. | 15 |
| Formatting | String | Prices with two decimals in the file and on screen. | 10 |
| Total | 100 | ||
Our own weighting for practice. It is not the DICT scoring scheme.
07 Self-check
Tick only what you have verified by running your own program.
Loading saved checks…
08 Reference solution
Both versions compiled and reproduced the sample output on Java 21 and .NET 8 when this site was built. Reference solutions are in Java and C#. The 2023 FAQ copy also lists C, C++ and Visual Basic 6.0 / .NET; the same structure carries over, but you will need your language's own file and database APIs.
Try the whole brief first. The reference is one correct approach, not the only one, and reading it early removes most of the practice value.
09 Exam-day habits
- Work without internet search to simulate exam conditions.
- Type the sample input yourself and compare your output line by line before you look at the reference.