1

i really need help with my program. I'm new to Arraylist and I have no idea how can i search and display an object by its ID number.

So the program has 4 class: Main, Person (parent class) Student and Employee (both are child of Person)

Now my main class has a menu that will do the following:

  1. Add Student ( ID must be unique for each stud )
  2. Add Employee ( ID must be unique for each emp )
  3. Search Student ( By Student ID then display it)
  4. Search Employee ( By Employee No then display it)
  5. Display All ( Display All Stud and Employee )
  6. Quit

I have done this before with array searching and displaying but now we have to use Arraylist which is better but im new to it.

Any help and suggestion is highly appreciated. Thank you!

Here's my code:

Main Class:

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

public class Main {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args){
        ArrayList<Student> stud = new ArrayList<Student>();
        ArrayList<Employee> emp = new ArrayList<Employee>();
        while (true) {
            int select;
            System.out.println("Menu");
            System.out.println("1. Add Student");
            System.out.println("2. Add Employee");
            System.out.println("3. Search Student");
            System.out.println("4. Search Employee");
            System.out.println("5. Display All");
            System.out.println("6. Quit");
            select = sc.nextInt();

            switch (select) {
                case 1:
                    addStud(stud);
                    break;
                case 2:
                    addEmp(emp);
                    break;
                case 3:
                    srchStud();
                    break;
                case 4:
                    srchEmp();
                case 5:
                    displayAll(stud, emp);
                    break;
                case 6:
                    return;
                default:
                    System.out.println("Invalid Option");
            }
        }
    }

    public static void addStud(ArrayList<Student> stud) {

        String name, address, course;
        int age, cNum, studNum, year;

        int addMore;
        System.out.println("ADD STUDENT");
        do {
            System.out.println("Student No: ");
            studNum = sc.nextInt();
            sc.nextLine();
            for(int x = 0; x < stud.size(); x++){ // Please help fix, this accepts another ID if already existing to prevent duplicate
                if(studNum == stud[x].getStudNum()) { // this code works with array of object but not on arraylist,
                    System.out.println("The Student ID: " +studNum+ " already exist.\nEnter New Student ID: ");
                    studNum = sc.nextInt();
                    sc.nextLine();
                    x = -1;
                }
            }
            System.out.println("Name: ");
            name = sc.nextLine();
            System.out.println("Age: ");
            age = sc.nextInt();
            sc.nextLine();
            System.out.println("Address: ");
            address = sc.nextLine();
            System.out.println("Contact No: ");
            cNum = sc.nextInt();
            sc.nextLine();
            System.out.println("Course: ");
            course = sc.nextLine();
            System.out.println("Year: ");
            year = sc.nextInt();
            sc.nextLine();

            stud.add(new Student(name, address, age, cNum, studNum, year, course));

            System.out.println("To add another Student Record Press 1 [any] number to stop");
            addMore = sc.nextInt();
            sc.nextLine();
        } while (addMore == 1);

    }

    public static void addEmp(ArrayList<Employee> emp) {

         String name, address, position;
         int age, cNum, empNum;
         double salary;

        int addMore;
        System.out.println("ADD Employee");
        do {
            System.out.println("Employee No: ");
            empNum = sc.nextInt();
            sc.nextLine();
            for(int x = 0; x < emp.size(); x++){
                if(empNum == emp[x].getEmpNum()) {
                    System.out.println("The Employee ID: " +empNum+ " already exist.\nEnter New Student ID: ");
                    empNum = sc.nextInt();
                    sc.nextLine();
                    x = -1;
                }
            }
            System.out.println("Name: ");
            name = sc.nextLine();
            System.out.println("Age: ");
            age = sc.nextInt();
            sc.nextLine();
            System.out.println("Address: ");
            address = sc.nextLine();
            System.out.println("Contact No: ");
            cNum = sc.nextInt();
            sc.nextLine();
            System.out.println("Position: ");
            position = sc.nextLine();
            System.out.println("Salary: ");
            salary = sc.nextInt();
            sc.nextLine();

            emp.add(new Employee(name, address, age, cNum, empNum, salary, position));

            System.out.println("To add another Student Record Press 1 [any] number to stop");
            addMore = sc.nextInt();
            sc.nextLine();
        } while (addMore == 1);

    }

    public static void displayAll(ArrayList<Student> stud, ArrayList<Employee> emp){ // Definitely not working with Arraylist
        System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
        for (int x = 0; x < stud.size(); x++) {

            System.out.println(stud[x].getStudNum() + "\t\t\t\t" + stud[x].getName() + "\t\t\t\t" + stud[x].getCourse() + "\t\t\t\t" + stud[x].getYear());

        }
    }      
}

Person Class :

public class Person {

    private String name, address;
    private int age, cNum;

    public Person() {
    }

    public Person(String name, String address, int age, int cNum) {
        this.name = name;
        this.address = address;
        this.age = age;
        this.cNum = cNum;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

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

    public int getcNum() {
        return cNum;
    }

    public void setcNum(int cNum) {
        this.cNum = cNum;
    }
}

Student Class :

public class Student extends Person{

    private int studNum, year;
    private String course;

    public Student(String name, String address, int age, int cNum, int studNum, int year, String course) {
        super(name, address, age, cNum);
        this.studNum = studNum;
        this.year = year;
        this.course = course;
    }

    public int getStudNum() {
        return studNum;
    }

    public void setStudNum(int studNum) {
        this.studNum = studNum;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }
}

Employee Class :

public class Employee extends Person {

    private int empNum;
    private double salary;
    private String position;

    public Employee(String name, String address, int age, int cNum, int empNum, double salary, String position) {
        super(name, address, age, cNum);
        this.empNum = empNum;
        this.salary = salary;
        this.position = position;
    }

    public int getEmpNum() {
        return empNum;
    }

    public void setEmpNum(int empNum) {
        this.empNum = empNum;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}
Trushit Shekhda
  • 563
  • 7
  • 18
lance2k
  • 357
  • 1
  • 4
  • 14
  • Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – rubeonline Jan 20 '20 at 19:53

2 Answers2

1

If you are not limited to use ArrayList, better performance will be achieved using a Map.

Map<Integer, Employee>  employeeIndex = new HashMap<>();
    //put
    employeeIndex.put(employeeId, empployee);
    //get
    employeeIndex.get(employeeId);

In case, you need to use ArrayList, you can use filter:

List<Student> students = new ArrayList<Student>();

        List<Student> filteredStudent = students.stream()
            .filter(student -> student.getStudNum() == student_id)
            .collect(Collectors.toList());
Shafiul
  • 1,452
  • 14
  • 21
  • i her about it, but we can only use Arraylist, condition, loop, etc. Anyway, thanks for the suggestions ; ) – lance2k Jan 20 '20 at 17:44
0

just this way if you are searching for a student the id is 12

ArrayList<Integer> students;
students.forEach(stu -> {if ( stu.getStudNum() == 2) System.out.println(stu.getName());});
kevin ternet
  • 4,514
  • 2
  • 19
  • 27