1/Write java application with the following information:
- Class Name ; Student
- Method1: AddStudent
- Method2: Print Students
- Method 3: totalStudent
- Method 4: Delete Student
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
StudentsCollection studentsCollection = new StudentsCollection();
Student student1 = new Student("John", "Smith");
Student student2 = new Student("Thomas", "Anderson");
Student student3 = new Student("Angelina", "Jolie");
System.out.println("Add student: " + student1);
studentsCollection.addStudent(student1);
System.out.println("Add student: " + student2);
studentsCollection.addStudent(student2);
System.out.println("Add student: " + student3);
studentsCollection.addStudent(student3);
System.out.println("");
System.out.println("Total of students: " + studentsCollection.totalStudents());
System.out.println("\tStudents:");
System.out.println("\t" + studentsCollection.printStudents());
System.out.println("");
System.out.println("Student John Smith is removed.");
studentsCollection.deleteStudents(student1);
System.out.println("");
System.out.println("Total of students: " + studentsCollection.totalStudents());
System.out.println("\tStudents:");
System.out.println("\t" + studentsCollection.printStudents());
}
public static class Student {
private final String firstName;
private final String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
public static class StudentsCollection {
private final ArrayList<Student> studentArrayList = new ArrayList<>();
public ArrayList<Student> addStudent(Student student) {
studentArrayList.add(student);
return studentArrayList;
}
public String printStudents () {
return studentArrayList.toString();
}
public int totalStudents () {
return studentArrayList.size();
}
public void deleteStudents(Student student) {
studentArrayList.remove(student);
}
}
}
Comments
Leave a comment