Question:
Now Zara has another requirement. Since there are many numbers of students, Zara would like to obtain the information of Students of a particular school. Create a java application to implement the above requirement. You are provided with the Student class that has the below attributes:
- int studId
- String studName
- String schoolName
Your are provided with getters and setters. You are also provided with a method called retrieveStudentInfo(ArrayList<Student>,String schoolName) inside TestMain class. This method should return all the student names as a ArrayList<String> who are studying in the given school.Use the given main method to test the code
CODE:–
TESTmain.java
import java.util.*; public class TestMain{ public static ArrayList<String> retrieveStudentInfo(ArrayList<Student> al,String schoolName) { // implement the required business logic ArrayList<String> str=new ArrayList<String>(); for(int i=0;i<al.size();i++){ Student s=al.get(i); if(s.getSchoolName().equalsIgnoreCase(schoolName)) { str.add(s.getStudName()); } } return str; } public static void main(String[] args) { Student s1=new Student(); s1.setStudId(1); s1.setStudName("John"); s1.setSchoolName("ZEE"); Student s2=new Student(); s2.setStudId(2); s2.setStudName("Tom"); s2.setSchoolName("ZEE"); Student s3=new Student(); s3.setStudId(3); s3.setStudName("Peter"); s3.setSchoolName("BEE"); Student s4=new Student(); s4.setStudId(4); s4.setStudName("Rose"); s4.setSchoolName("OX-FO"); Student s5=new Student(); s5.setStudId(5); s5.setStudName("Alice"); s5.setSchoolName("ZEE"); //invoke the retrieveStudentInfo method and display the result ArrayList<Student>studentInfo=new ArrayList<Student>(); studentInfo.add(s1); studentInfo.add(s2); studentInfo.add(s3); studentInfo.add(s4); studentInfo.add(s5); ArrayList<String>retrieveStudent=retrieveStudentInfo(studentInfo,"BEE"); for(int i=0;i<retrieveStudent.size();i++) { System.out.println(retrieveStudent.get(i)); } } }
student.java
public class Student { private int studId; private String studName; private String schoolName; public int getStudId() { return studId; } public void setStudId(int studId) { this.studId = studId; } public String getStudName() { return studName; } public void setStudName(String studName) { this.studName = studName; } public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } }