Introduction
CRUD — create, read, update, delete — is the backbone of most business software. This project builds it in layers: an immutable Student record that validates itself, a StudentRepository that stores records and saves them to disk safely, and a command interface on top.
Problem statement
data/students.csv. On start-up load the file, skipping and reporting damaged lines. Support adding (rejecting duplicates and invalid data), showing, updating CGPA, promoting to the next year (not beyond year 4), deleting, listing sorted by roll, name or CGPA, filtering by branch, statistics per branch, saving and reloading. Warn when quitting with unsaved changes.Objectives
- Implement CRUD operations behind a repository interface
- Read and write CSV with java.nio.file
- Save atomically using a temporary file and a move
- Model updates on an immutable record with 'wither' methods
Features
- Validated, normalised records (branch upper-cased, names trimmed)
- Tolerant loading with warnings
- Sorting, filtering and per-branch statistics
- Atomic save, reload, and an unsaved-changes warning
Concepts demonstrated
- CRUD
- Repository pattern
- Persistence
- Atomic writes
- Immutability
System architecture
Student is a record whose compact constructor validates and normalises fields; toCsv/fromCsv convert to and from text, and withCgpa/promoted return modified copies. StudentRepository keeps a TreeMap<Integer, Student>; load reads the file and collects warnings for bad lines; save writes a header and every record to students.csv.tmp and then moves it over the real file, so an interrupted save can't corrupt the data. Main tracks whether there are unsaved changes.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Student.java
- The validated, immutable student record and its CSV conversion.
- src/StudentRepository.java
- In-memory storage keyed by roll number, tolerant loading and atomic saving.
- src/Main.java
- The command loop, listing, filtering and statistics.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
list add 106, Priya Shah, cse, 2, 8.55 add 101, Duplicate, CSE, 1, 5 add 107, Kiran, Civil Eng, 1, 7 add 108, Lina, IT, 5, 8 show 103 show 999 cgpa 102 8.10 cgpa 102 11 promote 105 promote 103 delete 106 delete 106 add 106, Priya Shah, CSE, 2, 8.60 list cgpa branch cse stats save cgpa 101 4.00 reload show 101 quit
Testing
- Run from the project folder with
tests/sample-input.txtand compare withtests/expected-output.txt. - The sample data file contains a damaged line (
104,broken line), which is reported and skipped. - Changing Asha's CGPA after
saveand then runningreloadrestores the saved value 8.90. - Note: running the sample session saves to data/students.csv; restore it from the download (or git) to rerun the test exactly.
Common errors
NumberFormatException while loading
Parse each line inside try/catch and report the line number, as load() does, instead of aborting.
The data file is empty after a crash
Write to a temporary file first and move it into place only when the write succeeded.
Possible improvements
- Add an
undoof the last change - Export reports as CSV
- Replace the file with an SQLite database via JDBC
Additional challenges
- 1Add search by name prefix.
- 2Keep a history of CGPA changes per student.
- 3Support importing another CSV file and merging duplicates.
Built and tested it yourself? Mark it to update your dashboard.