๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

Java Programming Foundations Module 5 ยท Lesson 13 of 15

0% Exit to course

Module 5 ยท A real program, end to end Lesson 13 of 15

A console app with layers

Learning objective

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.

Java
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.

Create free account or sign in

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.

Next recommended course Python for Absolute Beginners