Object-Oriented Programming with Java, part I – Exercise 88

This exercise implements the class Student and then a main that adds each Student into an ArrayList that can be entered and searched.

My Code

Main

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write here the main program
        Scanner input = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<Student>();

        System.out.print("name: ");
        String name = input.nextLine();

        while (name.length() > 0) {
            System.out.print("studentnumber: ");
            String number = input.nextLine();

            Student student = new Student(name, number);
            list.add(student);

            System.out.print("name: ");
            name = input.nextLine();

        }

        for (Student student : list) {
            System.out.println(student);
        }
        
        System.out.print("Give search term: ");
        String search = input.nextLine();
        System.out.println("Result: ");
        
        for (Student student : list) {
            if (student.getName().contains(search)){
            System.out.println(student);
            }
        }
        
    }
}

Student

public class Student {
    
    private String name;
    private String studentNumber;
    
    public Student(String name, String studentNumber){
        this.name = name;
        this.studentNumber = studentNumber;
        
    }
    
    public String getName(){
        return this.name;
    }
    
    public String getStudentNumber(){
        return this.studentNumber;
    }
    
    public String toString() {
        return getName() + " (" + getStudentNumber() + ")";
    }
    
}

Model Code

Main

import java.util.ArrayList;

import java.util.Scanner;

 

public class Main {

 

    public static void main(String[] args) {

        // write here the main program

        ArrayList<Student> list = new ArrayList<Student>();

        Scanner reader = new Scanner(System.in);

        while (true) {

            System.out.print("name: ");

            String name = reader.nextLine();

            if (name.isEmpty()) {

                break;

            }

            System.out.print("studentnumber: ");

            String number = reader.nextLine();

            list.add(new Student(name, number));

        }

 

        for (Student student : list) {

            System.out.println(student);

        }

 

        System.out.println();

 

        System.out.print("Give search term: ");

        String searchTerm = reader.nextLine();

        System.out.println("Result:");

 

        for (Student student : list) {

            if (student.getName().contains(searchTerm)) {

                System.out.println(student);

            }

        }

    }

}

Student

public class Student {

 

    private String name;

    private String studentNumber;

 

    public Student(String name, String studentNumber) {

        this.name = name;

        this.studentNumber = studentNumber;

    }

 

    public String getName() {

        return name;

    }

 

    public String getStudentNumber() {

        return studentNumber;

    }

 

    public String toString() {

        return name + " (" + studentNumber + ")";

    }

}

Comments

That went well.

Leave a comment