අවසාන ව්යාපෘතිය: ශිෂ්ය කළමනාකරණ පද්ධතිය
මෙම පාඨමාලාව පුරාවට ඔබ ලබාගත් දැනුම ප්රායෝගිකව යොදා ගැනීමට කාලයයි. අපි දැන් සරල නමුත් ඉතා ප්රයෝජනවත් Command-Line (Terminal) එකේ ක්රියාත්මක වන Student Management System එකක් නිර්මාණය කරමු.
මෙම වැඩසටහන නිවැරදිව ක්රියාත්මක කිරීමට, ඔබගේ ගොනු නිවැරදිව සකසා තිබීම වැදගත් වේ.
Student.javaMain.javaමෙම වැඩසටහන ක්රියාත්මක කළ විට, Terminal එකේ මෙවැනි මෙනුවක් දිස්වනු ඇත:
මෙම 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;
}
}
මෙය අපගේ යෙදුමේ ප්රධාන කොටසයි. මෙනුව පෙන්වීම, පරිශීලක දත්ත ලබා ගැනීම, සහ අනෙකුත් සියලුම ක්රියාකාරකම් මෙහි අඩංගු වේ.
// 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.");
}
}
}
}
javac Student.java Main.java ලෙස ටයිප් කර Enter ඔබන්න.java Main ලෙස ටයිප් කර Enter ඔබන්න.