Anj26Pro
Anj26Pro

Reputation: 57

Java associations array list

Good day, I have an associations with courses and Student which student can enroll into a many courses and a course has many students. And the student class has an interface of comparable which compares the last names so I can use collections.sort() to sort the names into last names when outputted. So when i out put students from the Courses class using the method displayClassList() in the dirver class the output doesnt not show anything. Is there something missing in the code?

Here are the classes:

Driver:

import java.util.*;
import java.beans.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.beans.XMLDecoder;
import java.io.FileInputStream;
import java.util.*;
public class As3A_Driver
{
    public static void main(String[] args)
    throws IOException {
        // list of people
        Student harry = new Student("Harry", "Potter", 2, 3.65);
        Student ron = new Student("Ron", "Weasley", 2, 2.21);
        Student hermione = new Student("Hermione", "Granger", 2, 4.5);
        Student cho = new Student("Cho", "Chiang", 3, 4.05);
        Student ginny = new Student("Ginny", "Weasley", 1, 3.9);
        Instructor snape = new Instructor("Severus", "Snape", "M.Sc.");
        Instructor mcg = new Instructor("Minerva", "McGonigal", "Ph.D.");

        ArrayList<Person> people = new ArrayList<>();
        people.add(harry); people.add(ron); people.add(hermione); 
        people.add(ginny); people.add(cho);

        // courses and associations
        Course course1 = new Course("WIZ-2666", "Potions II");
        Course course2 = new Course("WIZ-2989", "Transfiguration");        
        course1.addInstructor(snape);
        harry.addCourse(course1);
        ron.addCourse(course1);
        hermione.addCourse(course1);
        cho.addCourse(course1);
        course2.addInstructor(mcg);
        harry.addCourse(course2);
        ron.addCourse(course2);
        hermione.addCourse(course2);
        ginny.addCourse(course2);

        // display class list
        course1.displayClassList();
        course2.displayClassList();


    }
}

Courses:

import java.util.*;
public class Course
{
    private String courseNumber; 
    private String courseName;
    private ArrayList<Instructor> instructor; 
    private ArrayList<Student> student; 

    public Course(){
        courseNumber = courseName = "Unknown"; 
    }

    public Course (String courseName, String courseNumber){
        this.courseNumber=courseNumber;
        this.courseName=courseName; 
        this.student=new ArrayList();
        this.instructor = new ArrayList();
    }

    public String getCourseNumber(){
        return courseNumber;
    }

    public String getCourseName(){
        return courseName;
    }

    public void setCourseNumber(){
        this.courseNumber=courseNumber;
    }

    public void setCourseName(){
        this.courseName=courseName;
    }

    public void setStudent(ArrayList<Student> student){
        this.student=student;
    }

    public ArrayList<Student> student(){
        return student; 
    }

    public void addStudent(Student students){
        student.add(students); 
    }   

    public void addInstructor(Instructor i){
        instructor.add(i); 
    }

    public void displayClassList(){
        System.out.println(getCourseName()+" "+ getCourseNumber());

        for(Instructor i: instructor){
            System.out.println("Instructor: "+i+", "+ i.getDegree()); 
        }

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

        for(Student s :student){
            Collections.sort(student); 
            System.out.println(s.toString()); 
        }
    }
}

Instructor:

import java.util.*;
public class Instructor extends Person
{
    private String degree; 
    public ArrayList <Course> courses;
    public Instructor instructor; 

    public Instructor(){  
       firstName= lastName = degree= "unknown"; 
    }

    public Instructor (String F ,String L,String degree){
        super(F,L); 
        this.degree=degree;  
        this.courses=new ArrayList();
    }

    public void setCourses(ArrayList<Course> courses){
        this.courses=courses;
    }
    public ArrayList<Course> courses(){
        return courses; 
    }

    public void addCourses (Course c){
        courses.add(c); 
    }

    public String toString(){
        return lastName+","+firstName; 
    }   

    public String getDegree(){
        return degree; 
    }
    public void setDegree(){
        this.degree=degree; 
    }

}

Person:

import java.util.*;
public abstract class Person
{
    protected String firstName;
    protected String lastName;

    public Person(){
        this.firstName=lastName = "Unknown";  
    }

    public Person(String F ,String L){
        firstName=F; 
        lastName=L;
    } 

    public void setFirstName(){
        this.firstName=firstName; 
    }
    public void setLastName(){
        this.lastName=lastName; 
    }

    public String getFirstName(){
        return firstName; 
    }
    public String getLastName(){
        return lastName; 
    }

    public abstract String toString(); 

    public String getName(){
        return firstName+" "+lastName; 
    }
}

Student:

import java.util.*;
public class Student extends Person  implements Comparable<Student>
{
    protected int year;
    protected double gpa;
    protected ArrayList<Course> course; 

    public Student(){
        year = 0; 
        gpa = 0.0; 
        firstName= lastName= "unknown"; 
    }

    public Student(String F ,String L,int y, double g){
        super(F,L);
        this.year=y; 
        this.gpa=g;
        course = new ArrayList();
    }

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

    public int getYear(){
        return year;
    }

    public void addCourse(Course c){
        course.add(c); 
    }

    public void setCourses(ArrayList<Course> courses){
        this.course=courses;
    }

    public ArrayList<Course> course(){
        return course; 
    }

    public void setGpa(){
        this.gpa=gpa;
    }

    public double getGpa(){
        return gpa; 
    }

    public String getInfo(){
        return  getName()+" "+year+" "+gpa; 
    }

    public String toString(){
        return lastName+","+firstName; 
    }

    public int compareTo(Student lt){
       int lastName = this.lastName.compareTo(lt.lastName);
       return lastName == 0 ? this.firstName.compareTo(lt.firstName ): lastName; 
    }
}

Upvotes: 0

Views: 482

Answers (1)

Nitin Bisht
Nitin Bisht

Reputation: 5351

You try this:

    // add students to courses
    course1.addStudent(harry);
    course1.addStudent(ron);
    course1.addStudent(hermione);
    course1.addStudent(cho);

    course2.addStudent(harry);
    course2.addStudent(ron);
    course2.addStudent(hermione);
    course2.addStudent(ginny);

    // display class list
    course1.displayClassList();
    course2.displayClassList();

Output:

enter image description here

Note: I have added students to courses. Apart from this, I have also modified displayClassList() method as shown below:

public void displayClassList(){
    System.out.println(getCourseName()+" "+ getCourseNumber());

    for(Instructor i: instructor){
        System.out.println("Instructor: "+i+", "+ i.getDegree()); 
    }

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

    Collections.sort(student);
    for(Student s :student){
        System.out.println(s.toString()); 
    }
}

Upvotes: 2

Related Questions