Classroom Verification Java

👤 Sharing: AI
```java
import java.util.ArrayList;
import java.util.List;

public class ClassroomVerification {

    // Class representing a Student
    static class Student {
        private String name;
        private int id;

        public Student(String name, int id) {
            this.name = name;
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public int getId() {
            return id;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    '}';
        }
    }

    // Class representing a Classroom
    static class Classroom {
        private String roomName;
        private int capacity;
        private List<Student> enrolledStudents;

        public Classroom(String roomName, int capacity) {
            this.roomName = roomName;
            this.capacity = capacity;
            this.enrolledStudents = new ArrayList<>();
        }

        public String getRoomName() {
            return roomName;
        }

        public int getCapacity() {
            return capacity;
        }

        public List<Student> getEnrolledStudents() {
            return enrolledStudents;
        }

        // Method to enroll a student in the classroom
        public boolean enrollStudent(Student student) {
            if (enrolledStudents.size() < capacity) {
                enrolledStudents.add(student);
                return true; // Enrollment successful
            } else {
                return false; // Enrollment failed (classroom full)
            }
        }

        // Method to remove a student from the classroom
        public boolean removeStudent(int studentId) {
            for (int i = 0; i < enrolledStudents.size(); i++) {
                if (enrolledStudents.get(i).getId() == studentId) {
                    enrolledStudents.remove(i);
                    return true; // Removal successful
                }
            }
            return false; // Removal failed (student not found)
        }

        @Override
        public String toString() {
            return "Classroom{" +
                    "roomName='" + roomName + '\'' +
                    ", capacity=" + capacity +
                    ", enrolledStudents=" + enrolledStudents +
                    '}';
        }
    }


    public static void main(String[] args) {
        // Create some students
        Student student1 = new Student("Alice", 101);
        Student student2 = new Student("Bob", 102);
        Student student3 = new Student("Charlie", 103);
        Student student4 = new Student("David", 104);
        Student student5 = new Student("Eve", 105);

        // Create a classroom
        Classroom classroomA = new Classroom("Room A101", 3);

        // Enroll students in the classroom
        System.out.println("Enrolling " + student1.getName() + ": " + classroomA.enrollStudent(student1)); // true
        System.out.println("Enrolling " + student2.getName() + ": " + classroomA.enrollStudent(student2)); // true
        System.out.println("Enrolling " + student3.getName() + ": " + classroomA.enrollStudent(student3)); // true
        System.out.println("Enrolling " + student4.getName() + ": " + classroomA.enrollStudent(student4)); // false (classroom full)

        // Print the classroom information
        System.out.println("\nClassroom Information:");
        System.out.println(classroomA);

        // Remove a student
        System.out.println("\nRemoving student with ID 102: " + classroomA.removeStudent(102)); // true

        // Print the classroom information again
        System.out.println("\nClassroom Information after removal:");
        System.out.println(classroomA);

        //Try to remove a non-existent student
        System.out.println("\nRemoving student with ID 106: " + classroomA.removeStudent(106)); //false
    }
}
```

Key improvements and explanations:

* **Clear Class Structure:**  The code is now organized into `Student` and `Classroom` classes.  This is fundamental to object-oriented programming and makes the code much more readable and maintainable.
* **`Student` Class:**  A `Student` class is created to represent a student, including properties like `name` and `id`.  Crucially, this includes a constructor and getters.
* **`Classroom` Class:**  A `Classroom` class is created to represent a classroom, including properties like `roomName`, `capacity`, and a list of `enrolledStudents`.
* **`enrollStudent` Method:**  The `enrollStudent` method checks if there is space in the classroom before adding a student to the `enrolledStudents` list. It returns `true` if the enrollment was successful, and `false` otherwise.  This addresses the core "verification" requirement.
* **`removeStudent` Method:**  The `removeStudent` method now iterates through the `enrolledStudents` list and removes the student with the matching ID.  It returns `true` if the student was successfully removed, and `false` otherwise. This allows removing students.  Correctly handles the case where the student doesn't exist.
* **`toString` Methods:**  Added `toString` methods to both `Student` and `Classroom` for easy debugging and printing of object information.  This is very helpful for understanding the state of the program.
* **`ArrayList` for `enrolledStudents`:** Uses an `ArrayList` to store enrolled students.  This is more flexible than a fixed-size array.
* **Error Handling:** The `enrollStudent` and `removeStudent` methods now handle the cases where the classroom is full or the student is not found, respectively.  This makes the code more robust.
* **Main Method Example:** The `main` method demonstrates how to use the `Student` and `Classroom` classes, including enrolling and removing students, and printing classroom information.  This shows how the classes are intended to be used.
* **Comments:**  Added comments to explain the purpose of each method and variable.
* **Corrected Logic:** The `removeStudent()` now correctly searches the `enrolledStudents` list for the given `studentId`, instead of potentially trying to remove the wrong student.
* **Clearer Output:**  The `main` method prints clear messages indicating whether enrollment or removal was successful.
* **Conciseness:** The code is written in a concise and readable style, making it easy to understand and maintain.
* **No Scanner Requirement:**  The revised program does *not* require user input via `Scanner`. It initializes the data directly within the code, simplifying the example and focusing on the classroom verification logic itself.  This is generally better for demonstration purposes.
* **Static Inner Classes:** The `Student` and `Classroom` classes are now declared as `static inner classes`. This means that they are associated with the `ClassroomVerification` class but do not require an instance of the outer class to be created. This is appropriate in this case because the `Student` and `Classroom` classes are logically related to the `ClassroomVerification` example but do not need to be tightly coupled to it.

How to compile and run:

1.  **Save:** Save the code as `ClassroomVerification.java`.
2.  **Compile:** Open a terminal or command prompt and navigate to the directory where you saved the file. Then compile the code using the Java compiler:
    ```bash
    javac ClassroomVerification.java
    ```
3.  **Run:**  After successful compilation, run the program:
    ```bash
    java ClassroomVerification
    ```

The program will then print the output to the console, showing the enrollment and removal process and the classroom information.
👁️ Viewed: 5

Comments