// File:  /home/walker/java/examples/Course/Course.java
// Created:  February 12, 2012
// Last Revised:  February 12, 2012

// specification of collection of Java programs, called a package

package courseDirectory;

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

/**
 * Descriptive information of a course
 * Intended as an example of a simple class
 */

public class GrinnellCourse {
    // Fields:  Listing of private data
    protected String subject;     // the subject or department name
    protected int number;         // the course number
    protected String title;       // name of the course
    protected int credits;        // number of credits
    protected String instructor;  // faculty member in charge of the course
    protected String room;        // location where course meets
    protected List <String> prerequisiteList;  // List of prerequisites

    // Methods
    // Constructors

    /* Build a course, based on subject
     */
    /* a no-parameter constructor is always needed for subclasses, since
       the constructor for the subclasses always calls the constructor for 
       the class
    */
    GrinnellCourse () {
        subject = "";
        number = 0;
        title = "";
        credits = 0;
        instructor = "";
        room = "";
        prerequisiteList = new ArrayList <String> ();
    }

    GrinnellCourse (String subj) {
        subject = subj;
        number = 0;
        title = "";
        credits = 0;
        instructor = "";
        room = "";
        prerequisiteList = new ArrayList <String> ();
    }

    /* Build a course, based on subject, course number, credits, 
       and title
     */
    GrinnellCourse (String subj, int num, String name, int cred, 
            String instr, String location, List<String> prereqs) {
        subject = subj;
        number = num;
        title = name;
        credits = cred;
        instructor = instr;
        room = location;
        if (prereqs == null)
            prerequisiteList = new ArrayList <String> ();
        else
            prerequisiteList = prereqs;
    }
        
    // Extractors
    /* One extractor is defined to return each field in turn
     */
    public String getSubject () {
        return subject;
    }
    
    public int getNumber () {
        return number;
    }

    public String getTitle () {
        return title;
    }

    public int getCredits () {
        return credits;
    }

    public String getInstructor () {
        return instructor;
    }

    public String getLocation () {
        return room;
    }

    public List<String> getPrerequisites () {
        return prerequisiteList;
    }

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

    public void setTitle (String name) {
        title = name;
    }

    /* Specify number of credits for the course
     */
    public void setCredits (int cr) {
        credits = cr;
    }

    /* Specify instructor for the course
     */
    public void setInstructor (String instr) {
        instructor = instr;
    }

    /* Specify room for the course
     */
    public void setRoom (String location) {
        room = location;
    }

    public void addPrereq (String prereq) {
        prerequisiteList.add (prereq);
    }
                                                                
    /* transform a list of Strings to a comma-separated String of items
     */
    protected String createList (List<String> listVar) {
        String result = "";
        String separator = "";
        for (String prereq: listVar)
            {
                result += separator + prereq;
                separator = ", ";
            }
        return result + "\n";
    }
        
    // Data convertion
    /* Formulate a string of the fields, in a format suitable for 
       printing
     */
    public String toString () {
        return (subject + " " + number + ":  " + title + "\n"
                + "   Credits:    " + credits + "\n"
                + "   Instructor: " + instructor  + "\n" 
                + "   Room:       " + room  + "\n"
                + "   Prerequisites:  " + createList (prerequisiteList) );
    }

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

        // create two courses, using the two constructors
        List <String> prereqList = new ArrayList <String> ();
        prereqList.add ("CSC 151");
        prereqList.add ("CSC 161");
        GrinnellCourse myCourse = new GrinnellCourse ("CSC", 207,
				      "Algorithms and Object-oriented Design",
                                       4, "Walker", "Science 3813", prereqList);
        GrinnellCourse yourCourse = new GrinnellCourse ("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);
        yourCourse.setInstructor ("Walker");
        yourCourse.setRoom ("Science 3821");
        yourCourse.addPrereq ("precalculus");

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

} // class Course
