Module 10 – Final Project

අවසාන ව්‍යාපෘතිය: ශිෂ්‍ය කළමනාකරණ පද්ධතිය

සුබ පැතුම්! ඔබ අවසානයට පැමිණ ඇත!

මෙම පාඨමාලාව පුරාවට ඔබ ලබාගත් දැනුම ප්‍රායෝගිකව යොදා ගැනීමට කාලයයි. අපි දැන් සරල නමුත් ඉතා ප්‍රයෝජනවත් Command-Line (Terminal) එකේ ක්‍රියාත්මක වන Student Management System එකක් නිර්මාණය කරමු.

මෙම ව්‍යාපෘතියෙන් ඔබ භාවිතා කරන සංකල්ප:
  • OOP (Classes and Objects): `Student` object නිර්මාණය කිරීමට.
  • ArrayList: `Student` objects ගබඩා කිරීමට.
  • Methods: කේතය සංවිධානය කිරීමට.
  • Loops: ශිෂ්‍ය ලැයිස්තුව පෙන්වීමට සහ මෙනුව ක්‍රියාත්මක කිරීමට.
  • Control Flow (If/Else, Switch): පරිශීලකයාගේ තේරීම් කළමනාකරණය කිරීමට.
  • User Input (`Scanner` class): පරිශීලකයාගෙන් දත්ත ලබා ගැනීමට.

ව්‍යාපෘතිය සකසා ගන්නා ආකාරය (Project Setup)

මෙම වැඩසටහන නිවැරදිව ක්‍රියාත්මක කිරීමට, ඔබගේ ගොනු නිවැරදිව සකසා තිබීම වැදගත් වේ.

  1. නව ෆෝල්ඩරයක් සාදන්න: ඔබගේ පරිගණකයේ කැමති තැනක `StudentSystem` වැනි නමකින් නව ෆෝල්ඩරයක් සාදන්න.
  2. VS Code විවෘත කරන්න: Visual Studio Code විවෘත කර, "File" > "Open Folder..." වෙත ගොස් ඔබ දැන් සෑදූ `StudentSystem` ෆෝල්ඩරය තෝරන්න.
  3. නව ගොනු දෙකක් සාදන්න: VS Code හි Explorer pane එකේ, `StudentSystem` ෆෝල්ඩරය තුළ නව ගොනු දෙකක් සාදන්න:
    • Student.java
    • Main.java
වැදගත්: මෙම `Student.java` සහ `Main.java` යන ගොනු දෙකම එකම ෆෝල්ඩරය තුළ තිබිය යුතුය.

අපේ යෙදුම ක්‍රියාත්මක වන ආකාරය

මෙම වැඩසටහන ක්‍රියාත්මක කළ විට, Terminal එකේ මෙවැනි මෙනුවක් දිස්වනු ඇත:

Student Management System
1. Add Student
2. View All Students
3. Exit
Enter your choice:

ව්‍යාපෘතියේ සම්පූර්ණ කේතය සහ පැහැදිලි කිරීම

1. `Student.java` ගොනුව

මෙම class එක තනි ශිෂ්‍යයෙකුගේ දත්ත (ID, නම, ශ්‍රේණිය) ගබඩා කිරීමේ සැලසුම (blueprint) ලෙස ක්‍රියා කරයි. මෙහි ඇති දත්ත `private` ලෙස යෙදීමෙන්, ඒවාට පිටතින් ඍජුවම පිවිසීම වළක්වා, `public` methods හරහා පමණක් පිවිසීමට සැලැස්වීම (Encapsulation) සිදු කරයි.

// File: Student.java
public class Student {
    // Attributes for a student, marked as 'private' for good practice (Encapsulation)
    private int id;
    private String name;
    private int grade;

    // Constructor: This method is called automatically when a new Student object is created.
    // It sets the initial values for the attributes.
    public Student(int id, String name, int grade) {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    // "Getter" methods to access the private attributes from outside the class.
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getGrade() {
        return grade;
    }

    // This method provides a user-friendly string representation of the object.
    // @Override indicates that we are changing the default behavior of the toString() method.
    @Override
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Grade: " + grade;
    }
}

2. `Main.java` ගොනුව

මෙය අපගේ යෙදුමේ ප්‍රධාන කොටසයි. මෙනුව පෙන්වීම, පරිශීලක දත්ත ලබා ගැනීම, සහ අනෙකුත් සියලුම ක්‍රියාකාරකම් මෙහි අඩංගු වේ.

// File: Main.java

// We need to import classes we want to use from Java's standard library.
import java.util.ArrayList; // For using a resizable array
import java.util.Scanner;   // For getting user input from the console

public class Main {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the keyboard.
        Scanner scanner = new Scanner(System.in);
        // Create an ArrayList to store all our Student objects.
        ArrayList<Student> students = new ArrayList<>();
        int nextId = 1; // This variable will help us auto-assign student IDs.

        // An infinite 'while' loop to keep the menu running until the user chooses to exit.
        while (true) {
            System.out.println("\nStudent Management System");
            System.out.println("1. Add Student");
            System.out.println("2. View All Students");
            System.out.println("3. Exit");
            System.out.print("Enter your choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // This is important! It consumes the leftover newline character 
                                // after reading the integer, preventing issues with the next string input.

            // A 'switch' statement to perform actions based on the user's choice.
            switch (choice) {
                case 1:
                    // --- Add a new student ---
                    System.out.print("Enter student name: ");
                    String name = scanner.nextLine();
                    System.out.print("Enter student grade: ");
                    int grade = scanner.nextInt();
                    
                    // Create a new Student object using the constructor
                    Student newStudent = new Student(nextId, name, grade);
                    // Add the newly created object to our ArrayList
                    students.add(newStudent);
                    
                    nextId++; // Increment the ID for the next student
                    System.out.println("Student added successfully!");
                    break; // Exit the switch statement
                case 2:
                    // --- View all students ---
                    System.out.println("\n--- All Students ---");
                    if (students.isEmpty()) {
                        System.out.println("No students found.");
                    } else {
                        // Use a for-each loop to go through each Student object in the ArrayList
                        for (Student s : students) {
                            // This will automatically call the .toString() method of the Student object
                            System.out.println(s);
                        }
                    }
                    break; // Exit the switch statement
                case 3:
                    // --- Exit the program ---
                    System.out.println("Exiting the application. Goodbye!");
                    scanner.close(); // Good practice to close the scanner when done.
                    return; // This exits the main method, ending the program.
                default:
                    System.out.println("Invalid choice. Please enter 1, 2, or 3.");
            }
        }
    }
}

කේතය ක්‍රියාත්මක කරන ආකාරය

  1. ඉහත කේත දෙක, ඔබ සෑදූ `Student.java` සහ `Main.java` ගොනු වලට copy කර paste කරන්න.
  2. ඔබගේ Terminal (VS Code තුළ ඇති Terminal එක භාවිතා කළ හැක: "Terminal" > "New Terminal") විවෘත කරන්න.
  3. පළමුව, ගොනු දෙකම compile කරන්න. Terminal එකේ javac Student.java Main.java ලෙස ටයිප් කර Enter ඔබන්න.
  4. Compile වීම සාර්ථක නම් (කිසිදු error පණිවිඩයක් නොපෙන්වයි නම්), වැඩසටහන ක්‍රියාත්මක කිරීමට Terminal එකේ java Main ලෙස ටයිප් කර Enter ඔබන්න.
  5. දැන් ඔබට මෙනුව දිස්වනු ඇත. 1, 2, හෝ 3 ටයිප් කර Enter එබීමෙන් වැඩසටහන භාවිතා කළ හැක.