package course;

public class CourseComparable implements Comparable <CourseComparable>{

	    // Fields:  Listing of private data
	    String subject;     // the subject or department name
	    double number;     // the course number
	    int credits;    // number of credits
	    String title;       // name of the course

	    // Methods
	    // Constructors

	    /* Build a course, based on subject
	     */
	    CourseComparable (String subj) {
	        subject = subj;
	        number = 0;
	        credits = 0;
	        title = "";
	    }

	    /* Build a course, based on subject, course number, credits, 
	       and title
	     */
	    CourseComparable (String subj, double num, int cred, String name) {
	        subject = subj;
	        number = num;
	        credits = cred;
	        title = name;
	    }
	        
	    // Extractors
	    /* One extractor is defined to return each field in turn
	     */
	    public String getSubject () {
	        return subject;
	    }
	    
	    public double getNumber () {
	        return number;
	    }

	    public int getCredits () {
	        return credits;
	    }

	    public String getTitle () {
	        return title;
	    }

	    // Modifers
	    /* Specify course number and title
	     */
	    public void setNumTitle (int numb, String name) {
	        number = numb;
	        title = name;
	    }

	    /* Specify number of credits for the course
	     */
	    public void setCredits (int cr) {
	        credits = cr;
	    }
	    
	    // implement comparable
	    // equals method for another course
	    public boolean equals (CourseComparable otherCourse) {
	        return (subject.equals(otherCourse.subject)
	                && (number == otherCourse.number));
	    }

	    // comparable method for two courses
	    public int compareTo (CourseComparable otherCourse) {
	        if (this.equals(otherCourse))
	            return 0;
	        
	        int subjectComparison = subject.compareTo(otherCourse.subject);
	        
	        if (subjectComparison != 0)
	        	return subjectComparison;
	        else 
	        	// if subject/departments the same, compare course numbers
	        	return (int) Math.round(number - otherCourse.number);
	    }

	    // Data conversion
	    /* Formulate a string of the fields, in a format suitable for 
	       printing
	     */
	    public String toString () {
	        return (subject + " " + number + ":  Credits:  " + credits 
	        		+ ", " + title + "\n"); 
	    }

	    // method for testing
	    public static void main (String[] args) 
	        throws Exception {

	        // create two courses, using the two constructors
	        CourseComparable myCourse = new CourseComparable ("CSC", 207, 4, 
					      "Algorithms and Object-oriented Design");
	        CourseComparable yourCourse = new CourseComparable ("Math");

	        // print results of initialization
	        /* first use toString, then use extractors */
	        System.out.println ("Initialization");
	        System.out.println ("My course is ");
	        System.out.println (myCourse.toString());
	        System.out.println ("Your course is ");
	        System.out.print(yourCourse.getSubject() + " ");
	        System.out.println (yourCourse.getNumber() + ":  " + yourCourse.getTitle());
	        System.out.println ("     your credits:  " + yourCourse.getCredits());

	        // set other fields of yourCourse
	        yourCourse.setNumTitle (131, "Calculus I");
	        yourCourse.setCredits (4);

	        // print results of modifications
	        System.out.println ("\nAfter modification");
	        System.out.println ("Your course is \n" + yourCourse);
	    }

	} // class CourseComparable

