# Java using getters and setters to manage a list of student

**  
PRE REQUISITES:**

\[1\] [Data Structure (thirdygayares.com)](https://software-engineer.thirdygayares.com/data-structure-again)

\[2\] [OOP Concepts (Using Getters and Setters) (thirdygayares.com)](https://software-engineer.thirdygayares.com/oop-concepts-using-getters-and-setters)

simple implementation in Java using **getters** and **setters** to manage a list of student information based on this [Data Structure (thirdygayares.com)](https://software-engineer.thirdygayares.com/data-structure-again). We will create a `Student` class to encapsulate the data, and we will use an `ArrayList` to store multiple students.

```java
public class Student {
    private int id;
    private String name;
    private String section;
    private int age;
    private String email;

    // Getter and Setter for id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // Getter and Setter for name
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Getter and Setter for section
    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    // Getter and Setter for age
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // Getter and Setter for email
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // Display Student Information
    public String toString() {
        return "ID: " + id + ", Name: " + name + ", Section: " + section + ", Age: " + age + ", Email: " + email;
    }
}
```

```java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
       
        // List to store Student objects
        List<Student> students = new ArrayList<>();

        // Creating Student objects and setting values using setters
        Student student1 = new Student();
        student1.setId(1);
        student1.setName("Juan Carlos");
        student1.setSection("BSIT-4B");
        student1.setAge(22);
        student1.setEmail("juan@gmail.com");

        Student student2 = new Student();
        student2.setId(2);
        student2.setName("Jose Rizal");
        student2.setSection("BSIT-2A");
        student2.setAge(21);
        student2.setEmail("jose@gmail.com");
       
        students.add(student1);
        students.add(student2);

        // Display all students' information using getters
        for (Student student : students) {
            System.out.println(student.toString());
        }
    }
}
```

* **Student Class**: We define private fields for `id`, `name`, `section`, `age`, and `email`. For each field, we have a corresponding getter and setter method to manage the data.
    
* **Main Class**: We create several `Student` objects, set their data using the setter methods, and store them in an `ArrayList`. Finally, we loop through the list and print each student's information using the `toString()` method
    

### Output:

```java
ID: 1, Name: Juan Carlos, Section: BSIT-4B, Age: 22, Email: juan@gmail.com
ID: 2, Name: Jose Rizal, Section: BSIT-2A, Age: 21, Email: jose@gmail.com
```
