0

So, I'm trying to implement a compareTo method to compare to the student and gather the time, but I'm unable to succeed in getting that method here is the code that I have

Many people are saying its a similar question, but it isn't because again, like i explained in the bottom it gives me an error because you CANNOT "compareTo(Student) is undefined for the type Student"

import java.util.ArrayList;
import java.util.List;

public class Student implements Student_Interface {

    private String studentID;
    private String name;
    private List<Course> myEnrolledCourses;
    private List<Course> myWaitlist;
    private int courseCoins;

    /**
     * Constructor that populates the instance variables with parameters passed
     * @param id StudentID
     * @param name Name of the student
     * @param coins Course Coins
     */
    public Student(String id, String name, int coins) {
        this.studentID = id;
        this.name = name;
        this.courseCoins = coins;

        myEnrolledCourses = new ArrayList<>();
        myWaitlist = new ArrayList<>();
    }

    /**
     * Adds course c to the list of enrolled courses
     * Also removes c from the waitlisted courses
     * @param c Course to be enrolled in
     */
    @Override
    public void enrollCourse(Course c) {
        myEnrolledCourses.add(c);
        myWaitlist.remove(c);

    }

    /**
     * Adds course c to the waitlist
     * @param c course to be waitlisted
     */
    @Override
    public void waitlistCourse(Course c) {
        myWaitlist.add(c);

    }


    /**
     * Getter for name
     * @return name - Name of the student
     */
    @Override
    public String getStudentName() {

        return name;
    }

    /**
     * Getter for Student ID
     * @return studentID - Student ID
     */
    @Override
    public String getStudentID() {

        return studentID;
    }

    /**
     * Returns a list of all enrolled courses
     * @return List of enrolled courses
     */
    @Override
    public List<Course> getmyEnrolledCourses() {

        return myEnrolledCourses;
    }

    /**
     * Returns a list of all waitlisted courses
     * @return List of waitlisted courses
     */
    @Override
    public List<Course> getmyWaitlist() {

        return myWaitlist;
    }

    /**
     * Getter for course coins
     * @return course coins
     */
    @Override
    public int getCoins() {

        return courseCoins;
    }
    /**
     * Deducts numCoins from coursecoins
     * @param numCoins Number of coins to be deducted
     */
    @Override
    public void deductCoins(int numCoins) {
        numCoins = numCoins - courseCoins;

    }
    /**
     * Returns a string representation of the Student that includes
     * the name and the studentID
     * @return String representation of the student
     */
    @Override
    public String toString() {
        return this.name + "("+this.studentID+")";
    }

}

The code above is the Student class that follows this Register Class

public class Registration implements Comparable<Registration> {

    private Student student;
    private Course course;
    private int coins;
    private long timestamp;

    public Registration(Student student, Course course, int coins) {
        this.student = student;
        this.course = course;
        this.coins = coins;
        setTimestamp();
    }
    /** Gets the students name **/
    public Student getStudent() {
        return student; 
    }
    /** Gets the course name **/
    public Course getCourse() {
        return course; 
    }
    /** Gets the coins **/
    public int getCoins() {
        return coins; 
    }
    /**
     * Compares this Student with another Student, by comparing their 
     * course coins/timestamps
     * If the coins of this is less, returns a negative integer. If the
     * coins of the Student received is less, returns a positive integer.
     * If the number of coins is same, use the timestamp comparison to ensure FCFS.
     * (You may want to check the implementation of System.nanoTime to ensure
     * correctness)
     * @param o Student to be compared with
     * @return Result of the comparison
     */
    @Override
    public int compareTo(Registration o) { 
     //This is the method I need help with      if(this.getStudent().equals(o.getStudent())){
            if(this.getStudent().getCoins() == o.getStudent().getCoins()){
                System.nanoTime(); // how would i use this??

            }
            if(this.getStudent().getCoins() < o.getStudent().getCoins()){
                return -1;
            }
            if(this.getStudent().getCoins() > o.getStudent().getCoins()){
                return 1;
            }
        }

        return 1; //XXX-CHANGE-XXX

    }

    /**
     * Sets the timestamp inside this registration to be the current time in nano seconds.
     */
    public void setTimestamp() {
        timestamp = System.nanoTime();
    }
}

So As of right now, I have no idea how to implement the compareTo Method, I've googled and tried to use

this.getStudent().compareTo(o.getStudent()); 

I get an Error and it states: method compareTo(Student) is undefined for the type Student I'm not sure in how to go about this! Please help me understand! Thanks in advance.

  • suppose you have to write a `comapreTo()` method in student class. this [example](http://stackoverflow.com/questions/10017381/compareto-method-java) is a good to get stared. – Rajith Pemabandu May 19 '17 at 01:14
  • in what attribute(property) do you want to compare two students? `public class Student implements Student_Interface, Comparable { public int compareTo(Student student){ //insert the logic based on a property }}` – Rajith Pemabandu May 19 '17 at 01:23

0 Answers0