import java.util.*;
import java.net.*;
import java.io.*;
/** An instance gives a course, in terms of name-number, title, and A&S designation */
public class Course {
    
    private String course; // The course, e.g. "ECON 3440"
    private String title; // Everything about the course except what is in course
    private String category; // The category. Possibilities are given in LibStudies.categories
                             // The category is surrounded by ( )
    
    /* Constructor: an instance with course s, title t, and category cat */
    public Course(String c, String t, String cat) {
        course= c;
        title= t;
        category= cat;
    }
    
    /** Constructor: an instance for a course given by s. s is of the form given by

            <tr><td nowrap><a href="...">ECON&nbsp;3270</a></td><td>title etc.</td></tr>

       where the link can be ignored, the course is given before the </a> tag, and the title contains
       category cat.
       Note: if s is not proper, course, title and category will contain null.
       */
    public Course(String s, String cat) {
        if (!s.startsWith("<tr><td nowrap><a ")) {
            return;
        }
        
        // Remove from s everything before the course name
        int k= s.indexOf("\">");
        if (k < 0)
            return;
        s= s.substring(k+2);
        
        k= s.indexOf("</a>");
        if (k < 0)
            return;
        String courseName= s.substring(0, k);
        
        // Remove from s all things before the title etc.
        s= s.substring(k + "</a></td><td>".length());
        if (k < 0)
            return;
        
        k= s.indexOf("</td></tr>");
        if (k < 0)
            return;
        
        String t= s.substring(0, k);
        
        course= courseName;
        title= t;
        category= cat;
    }
    
    /** = String representation of the course */
    public String toString() {
        return course + " " + title;
    }
    
    /** =  the course */
    public String getCourse() {
        return course;   
    }
    
    /** =  the title */
    public String getTitle() {
        return title;   
    }
    
    /** =  the category */
    public String getCategory() {
        return category;   
    }

    /** = the category, without enclosing parentheses and without -AS.
        = null if category is null. */
    public String getCategoryWO() {
        if (category == null) {
            return "null";
        }
        String s= category.substring(1, category.length()-1);
        int k= s.indexOf("-");
        if (k > 0) {
            s= s.substring(0,k);
        }
        return s;
    }
    
}
