CompareTo方法和运行时间协助

时间:2017-05-19 01:07:15

标签: java time compareto

所以,我试图实现compareTo方法来与学生进行比较并收集时间,但是我无法成功获得该方法就是我拥有的代码

很多人都在说它是一个类似的问题,但它并不是因为我在底部解释它会给我一个错误,因为你不能" compareTo(学生)未定义类型学生& #34;

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+")";
    }

}

上面的代码是遵循此注册类

的Student类
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();
    }
}

所以截至目前,我不知道如何实施compareTo方法,我已经用Google搜索并尝试使用

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

我收到一个错误,它说明:方法compareTo(Student)未定义为Student类型 我不确定如何解决这个问题!请帮我理解! 提前谢谢。

0 个答案:

没有答案