Module 5 ยท A real program, end to end Lesson 13 of 15
A console app with layers
Split the program into a model class, a repository that reads and writes the file, and a menu that talks to the user. Three small pieces are easier to test and to change than one long main.
The concept
The separation is not ceremony; it is what lets you swap the CSV for a database later without touching the menu, and it is what interviewers probe when they ask how you would structure it.
Keep main thin: parse input, call the service, print the result. A two-hundred-line main is a main nobody can test.
public class Main {
public static void main(String[] args) {
StudentRepository repo = new CsvStudentRepository("students.csv");
StudentService service = new StudentService(repo);
service.toppers(3).forEach(System.out::println);
}
}Worked example
Main parses the menu choice, StudentService holds the rules, StudentRepository reads the CSV - each class explainable in one sentence.
Your practice task
Restructure your file-reading program into three classes with one job each.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
The separation is not ceremony; it is what lets you swap the CSV for a database later without touching the menu, and it is what interviewers probe when they ask how you would structure it.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task โ do them and the final project brief writes itself.