/*
 *        Names: Sheila Marie George, Mang-Fai Ma, Jun Shen
 *               Yu Su, Xiyou Wang, John Woods,
 *               Nelson Zhan, Zhengrong Zuo
 *
 *       Course: IS 2470, Interactive System Design
 *
 *      Project: Course Scheduler (Group 2)
 *
 *          Due: June 18, 1998
 *
 */

/**
 * An applet includes 3 applets to help students/advisors in the
 * Department of Information Science choose courses. This applet
 * accesses course offerings, course descriptions, and course
 * requirement from school webpages and provide an interactive
 * planning aid for class scheduling.
 *
 * @version 1.0 18 June 1998
 */

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*; 

// This is the main applet for Course Scheduler
public class CourseScheduler extends Applet
{
   // Use CardLayout to switch from one window to another
   CardLayout card_layout = new CardLayout();
   Applet a1 = new Window1();
   Applet a2 = new Window2();
   Applet a3 = new Window3();
   Panel windows = new Panel();
   
   public int[] selectedlist1;
   public int[] selectedlist2;
   public int[] vtorIndex = new int[50];
   public List courselist1;
   public List courselist2;
   public List crnlist;
   public List prereqlist1;
   public int validcoursecount;
   public Checkbox cb1, cb2, cb3, cb4;
   public Vector coursevtor = new Vector();
   
   // Initialization for CourseScheduler applet class
   public void init()
   {
      // Layout manager for CourseScheduler is BorderLayout
      setLayout(new BorderLayout());
      
      windows.setLayout(card_layout);
      // Display a message when data is loading from webpages
      MsgDialog d = new MsgDialog(getFrame());
      d.MsgDisplay(true, "Loading... Please wait...");
      a1.init();   // Initialize the first window
      a2.init();   // Initialize the second window
      a3.init();   // Initialize the third window
      windows.add("Window 1", a1);
      windows.add("Window 2", a2);
      windows.add("Window 3", a3);
      add("Center", windows);
      d.MsgDisplay(false, "");
   }

   // An inner class to define the structure of Course Information
   class CourseInfo {
      public String id;               // Course number
      public String instructor;       // Instructor
      public String title;            // Course title
      public String timecategory;     // Categorize the time
      public String begintime;        // Course begin time
      public String endtime;          // Course end time
      public String description;      // Description about the course
      public String crn;              // CRN of the course
      public String link;             // Link to department's webpage
      public String days;             // Days for the class
      public String place;            // Place for the class
      public String prerequirement;   // Prerequisite for the class
   }

   // An inner class to pop up a dialog window of error
   class ErrorDialog extends Dialog
   {
      private Frame thisFrame;
     
      public ErrorDialog(Frame f, String info)
      {
         super(f, "Course Scheduler", false);
        
         thisFrame = f;
        
         this.resize(300, 200);
        
         Panel info_panel = new Panel();
         Panel ok_panel = new Panel();
        
         info_panel.setLayout(new FlowLayout());
         info_panel.add(new Label(info, Label.CENTER));
         add("Center", info_panel);
        
         ok_panel.add(new Button("OK"));
         add("South", ok_panel);
        
         thisFrame.disable();
         this.pack();
         this.move(512, 384);
         this.show();
      }
     
      public boolean action(Event evt, Object arg)
      {
         if (evt.target instanceof Button)
         {
            thisFrame.enable();
            this.hide();
            this.dispose();
            return true;
         }
         return false;
      }
   }
  
   // An inner class for pop-up messages
   class MsgDialog extends Dialog
   {
      private Frame thisFrame;
     
      public MsgDialog(Frame f)
      {
         super(f, "Course Scheduler", false);
        
         thisFrame = f;
        
         this.resize(300, 200);
      }
        
      public void MsgDisplay(boolean on, String info)
      {
         if (on)
         {
            Panel info_panel = new Panel();
        
            info_panel.setLayout(new FlowLayout());
            info_panel.add(new Label(info, Label.CENTER));
            add("Center", info_panel);
           
            thisFrame.disable();
            this.pack();
            this.move(512, 384);
            this.show();
         }
         else
         {
            thisFrame.enable();
            this.hide();
            this.dispose();
         }
      }
   }
  
   // This method helps to get right frame to display dialog windows
   public Frame getFrame()
   {
      Component c = (Component)this;
      Frame theFrame = null;
      Component parent = (Component)this;
      while (parent != null)
      {
         if (parent instanceof Frame)
         {
            theFrame = (Frame)parent;
            break;
         }
         parent = parent.getParent();
      }
      return theFrame;
   }

   // An applet for the first window
   class Window1 extends Applet
   {
      Socket c;
      DataInputStream in;
      PrintStream out;
      
      public void init()
      { 
         try {
            // Create a socket to communicate with the course server
            c = new Socket(InetAddress.getLocalHost(), 7147);
//            c = new Socket("www2.sis.pitt.edu", 7147);
            in = new DataInputStream(c.getInputStream());
            out = new PrintStream(c.getOutputStream());

            // Layout manager for the first window is BorderLayout
            setLayout(new BorderLayout(20,0));
         
            Label l1 = new Label();
            l1.setText(" Please choose courses you have taken.");
            l1.setFont(new Font("TimesRoman", Font.BOLD, 14));
            add("North", l1);
         
            courselist1 = new List(30, true);
            courselist1.setFont(new Font("Courier", Font.PLAIN, 12));
            add("Center", courselist1);

            Panel p2 = new Panel();
            p2.setLayout(new GridLayout(16,1,3,2));
            for (int i = 0; i < 3; i++)
               p2.add(new Label());
            p2.setFont(new Font("TimesRoman", Font.BOLD, 14));
            Label l2 = new Label();
            l2.setText("Your available time");
            p2.add(l2);
            Label l3 = new Label();
            l3.setText("next term");
            p2.add(l3);
            p2.add(new Label());

            cb1 = new Checkbox();
            cb1.setLabel("Morning");
            cb1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            p2.add(cb1);
            cb2 = new Checkbox();
            cb2.setLabel("Afternoon");
            cb2.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            p2.add(cb2);
            cb3 = new Checkbox();
            cb3.setLabel("Evening");
            cb3.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            p2.add(cb3);
            cb4 = new Checkbox();
            cb4.setLabel("Weekend");
            cb4.setFont(new Font("TimesRoman", Font.PLAIN, 14));
            p2.add(cb4);
            for (int i = 0; i < 6; i++)
               p2.add(new Label());
            add("East", p2);
         
            Panel p3 = new Panel();
            p3.setLayout(new GridLayout(1, 14, 3, 3));
            for (int i = 0; i < 6; i++)
               p3.add(new Label());
            Button b1 = new Button();
            Button b2 = new Button();
            b2.setFont(new Font("TimesRoman", Font.BOLD, 16));
            b2.setLabel("Next >");
            p3.add(b2);
            for (int i = 0; i < 3; i++)
               p3.add(new Label());
            b1.setFont(new Font("TimesRoman", Font.BOLD, 16));
            b1.setLabel("Clear");
            p3.add(b1);
            for (int i = 0; i < 3; i++)
               p3.add(new Label());
            add("South", p3);

            // Extract course information from the course description
            // webpage of the department of Information Science
            InitCourse();         
         }
         catch(IOException e) 
         {
            System.out.println("Error" + e);
         }
      }

      // Detect the action from the user
      public boolean action(Event evt, Object arg)
      {
         if (arg instanceof String)
         {
            String s = (String) arg;

            if (arg.equals("Clear")) 
            {
            // Clear all information in this page
               cb1.setState(false);
               cb2.setState(false);
               cb3.setState(false);
               cb4.setState(false);
               selectedlist1 = courselist1.getSelectedIndexes();
               for (int i = 0; i < selectedlist1.length; i++)
                  courselist1.deselect(selectedlist1[i]);
            }
            else if (arg.equals("Next >")) 
            {
               // Check if the user choose as least one time constraint
               if (!(cb1.getState() || cb2.getState() 
                   || cb3.getState() || cb4.getState()))
               {
                  ErrorDialog ed = new ErrorDialog(getFrame(),
                           "Please select at least one time constraint");
               }
               else   
               {
                  // Jump to the second window
                  selectedlist1 = courselist1.getSelectedIndexes();
                  a2.start();
                  card_layout.show(windows, "Window 2");
               }
            }
            return super.action(evt, arg);
         }
         return true;
      }

      // Extract course information from department's webpage
      public void InitCourse() 
      {
         try 
         {
            // Send an URL string to course server
            out.println("http://www.sis.pitt.edu/~isdept/isgradcrs.html");
      
            String line,coursenumber,coursetitle;
            int i,j;
      
            // Analyze each line in the HTML file
            while (!(line = in.readLine()).equals("+++"))  
            {
               i = line.indexOf("INFSCI2",0);
               j = line.indexOf("INFSCI3",0);
               coursenumber="";
               coursetitle="";
               if (( i>-1)) 
               {
                  coursenumber=line.substring(i, i+10);
                  if (line.indexOf("/B",0)<0) 
                  {
                     coursetitle=line.substring(line.indexOf("/A")+3, 
                                                line.length());
                     line=in.readLine();
                     if (line.indexOf("/B",0)>-1) 
                     {
                        coursetitle = coursetitle + " " 
                                      + line.substring(0, 
                                           line.indexOf("/B",0)-1);
                     }
                     else 
                     {
                        coursetitle = coursetitle + " "
                                      + line.substring(0, line.length());
                     }
                  }
                  else 
                  {
                     coursetitle = line.substring(line.indexOf("/A")+3, 
                                    line.indexOf("/B",0)-1);
                  } 

                  if (coursetitle.indexOf("INFSCI 2",0)>-1) 
                  {
                     coursetitle = 
                        coursetitle.substring(
                           coursetitle.indexOf("INFSCI 2",0), 
                           coursetitle.indexOf("INFSCI 2",0)+6)
                        + coursetitle.substring(
                           coursetitle.indexOf("INFSCI 2",0)+7, 
                           coursetitle.length());
                  }
                  else 
                  {
                     coursetitle=coursenumber+" "+coursetitle;
                  }
                  courselist1.addItem(coursetitle);
               }
               if( j>-1) 
               {
                  coursenumber=line.substring(j, j+10);
                  if (line.indexOf("/B",0)<0) 
                  {
                     coursetitle = line.substring(line.indexOf("/A")+3, 
                                   line.length());
                     line = in.readLine();
                     if (line.indexOf("/B",0)>-1) 
                     {
                        coursetitle = coursetitle + " "
                           + line.substring(0, line.indexOf("/B",0)-1);
                     }
                     else 
                     {
                        coursetitle = coursetitle + " "
                           + line.substring(0, line.length());
                     }
                  }
                  else 
                  {
                     coursetitle = line.substring(line.indexOf("/A")+3, 
                        line.indexOf("/B",0)-1);
                  } 

                  if (coursetitle.indexOf("INFSCI 3",0)>-1) 
                  {
                     coursetitle = 
                        coursetitle.substring(
                           coursetitle.indexOf("INFSCI 3",0), 
                           coursetitle.indexOf("INFSCI 3",0)+6)
                        + coursetitle.substring(
                           coursetitle.indexOf("INFSCI 3",0)+7, 
                           coursetitle.length());
                  }
                  else 
                  {
                     coursetitle=coursenumber+" "+coursetitle;
                  }
                  if (coursetitle.indexOf("3951",0)==-1) 
                     courselist1.addItem(coursetitle);
               }
            } 
         }
         catch(IOException e)    
         {
            System.out.println("Error: " + e.getMessage());
         }
      }
   }

   // An applet for the second window
   class Window2 extends Applet
   {
      Socket c;
      DataInputStream in;
      PrintStream out;
   
      // Initialization for the second window
      public void init()
      { 
         try 
         {
            // Create a socket to the course server
            c = new Socket(InetAddress.getLocalHost(), 7147);
//            c = new Socket("www2.sis.pitt.edu", 7147);
            in = new DataInputStream(c.getInputStream());
            out = new PrintStream(c.getOutputStream());

            // Use BorderLayout as layout manager for the second window
            setLayout(new BorderLayout());
          
            Panel p1 = new Panel();
            p1.setLayout(new GridLayout(2,1));
            Label l1 = new Label();
            l1.setText(" Please select courses you wish to take next term.");
            l1.setFont(new Font("TimesRoman", Font.BOLD, 14));
            p1.add(l1);
            Label l2 = new Label();
            l2.setText(" Note: this list does not include"
                       + " the courses you've already taken.");
            l2.setFont(new Font("TimesRoman", Font.BOLD, 14));
            p1.add(l2);
            add("North", p1);
         
            courselist2 = new List(30, true);
            courselist2.setFont(new Font("Courier", Font.PLAIN, 12));
            add("Center", courselist2);

            Panel p2 = new Panel();
            p2.setLayout(new GridLayout(1, 14, 3, 3));
            for (int i = 0; i < 5; i++)
               p2.add(new Label());
            Button b1 = new Button();
            b1.setFont(new Font("TimesRoman", Font.BOLD, 16));
            b1.setLabel("< Back");
            p2.add(b1);
            Button b3 = new Button();
            b3.setFont(new Font("TimesRoman", Font.BOLD, 16));
            b3.setLabel("Next >");
            p2.add(b3);
            for (int i = 0; i < 3; i++)
               p2.add(new Label());
            Button b2 = new Button();
            b2.setFont(new Font("TimesRoman", Font.BOLD, 16));
            b2.setLabel("Clear");
            p2.add(b2);
            for (int i = 0; i < 3; i++)
               p2.add(new Label());
            add("South", p2);

            crnlist = new List();      // List for CRN numbers
            prereqlist1 = new List();  // List for prerequisite
            // Extract class schedule from University of Pittsbrugh's
            //   class schedule webpage
            InitCourse();              
         }
         catch(IOException e) 
         {
            System.out.println("Error" + e);
         }
      }

      // This start method is called by the first and third windows
      public void start()
      {
         ListCourse();
      }
   
      // Detect the action from the user
      public boolean action(Event evt, Object arg)
      {
         if (arg instanceof String)
         {
            String s = (String) arg;

            if (arg.equals("< Back")) 
            {
               // Go back to the previous page
               card_layout.show(windows, "Window 1");
            }
            else if (arg.equals("Clear")) 
            {
               // Reset all information in this page
               selectedlist2 = courselist2.getSelectedIndexes();
               for (int i = 0; i < selectedlist2.length; i++)
                  courselist2.deselect(selectedlist2[i]);
            }
            else if (arg.equals("Next >")) 
            {
               selectedlist2 = courselist2.getSelectedIndexes();
               if (selectedlist2.length == 0)
               {
                  // Ask the user to choose at least one course 
                  //   from this window. Otherwise, there is no
                  //   reason to do any analysis or schedule course
                  ErrorDialog ed = new ErrorDialog(getFrame(),
                            "Please select at least one course");
               }
               else
               {
                  // Display a message to ask the user to wait
                  //   before the third window is displayed
                  MsgDialog d = new MsgDialog(getFrame());
                  d.MsgDisplay(true, "Analyzing... Please wait...");
                  getPrereq();
                  a3.start();
                  d.MsgDisplay(false, "");
                  card_layout.show(windows, "Window 3");
                  if (validcoursecount > 5)
                  {
                     ErrorDialog ed = new ErrorDialog(getFrame(), 
                        "Warning: You have chosen more than 5 courses");
                  }
               }
            }
            return super.action(evt, arg);
         }
         return true;
      }

      // This method is to list all courses that satisfied the time
      //   constraint from the first window. This list also does not
      //   include any courses the student have already been taken.
      public void ListCourse()
      {
         courselist2.clear();
         String time = "";
         CourseInfo ci = new CourseInfo();
         Vector cid = new Vector();
         boolean addcourse;
         int i, j, k;
      
         k = 0;

         if (cb1.getState()) 
            time = time + "A";
         if (cb2.getState()) 
            time = time + "P";
         if (cb3.getState()) 
            time = time + "E";
         if (cb4.getState()) 
            time = time + "W";
            
      // List all the courses the student can take.
         for (i = 0; i < coursevtor.size(); i++) 
         {
            addcourse = true;
            ci = (CourseInfo) coursevtor.elementAt(i);
            for (j = 0; j < selectedlist1.length; j++)
            {
               if (courselist1.getItem(
                      selectedlist1[j]).substring(6, 10).equals(ci.id))
                  addcourse = false;
            }

            if ((!ci.timecategory.equals("BY APP")) 
                && (time.indexOf(ci.timecategory) == -1))
               addcourse = false;
            
            if (addcourse)
            {
               vtorIndex[k] = i;
               k++;
               crnlist.addItem(ci.crn);
               courselist2.addItem("INFSCI "
                           + ci.id + "  "
                           + ci.title + "  "
                           + ci.days + "  "
                           + ci.begintime + "  "
                           + ci.endtime + "  "
                           + ci.place + "  "
                           + ci.instructor);
            }
         }
      }
    
      // This method is to get the prerequisite of each courses that
      //   the user can take in next term. This information is taken
      //   from the department's fall term courses webpage
      public void getPrereq()
      {
         prereqlist1.clear();
         try
         {
            // Send an URL string to the course server
            out.println("http://www2.sis.pitt.edu/courses/distfall98.html");
            String line, prerequisite;
         
            for (int i = 0; i < selectedlist2.length; i++)
            {
               String courseNum = 
                  courselist2.getItem(selectedlist2[i]).substring(7, 11) 
                     + ":";
               // Search for the course number from the HTML file
               while ((line = in.readLine()).indexOf(courseNum) == -1) ;
               
               if (courseNum.equals("2905:"))
                  prereqlist1.addItem("2905 None");
               else
               {
                  // Search for the string "Pre//Co:"
                  while ((line = in.readLine()).indexOf("Pre//Co:") == -1) ;
                  prerequisite = line;
                  // Extract the prerequisite information
                  while (line.indexOf("") == -1)
                  {
                     line = in.readLine();
                     prerequisite = prerequisite + " " + line;
                  }
                  if (courseNum.equals("2540:"))
                     prereqlist1.addItem(courseNum.substring(0, 4) 
                     + prerequisite.substring(
                          prerequisite.indexOf("Pre//Co:")+9, 
                          prerequisite.indexOf("

"))); else prereqlist1.addItem(courseNum.substring(0, 4) + prerequisite.substring( prerequisite.indexOf("Pre//Co:")+8, prerequisite.indexOf(""))); } } while (!(line = in.readLine()).equals("+++")) ; } catch(IOException e) { System.out.println("Error: " + e.getMessage()); } } // This method is to extract class schedule information from // University of Pittsburgh's class schedule webpage public void InitCourse() { try { // Send an URL string to the course server out.println("http://www.pitt.edu/~srfsweb/tsch_fal/ts76.htm#infsci"); boolean hasInfo = false; String line; while (!(line = in.readLine()).equals("+++")) { // Search for the Graduate course offerings if (line.indexOf(">GRADUATE COURSE OFFERINGS") != -1) { hasInfo = true; break; } } if (!hasInfo) { System.out.println("There is not infomation available.\n"); } else { while (!(line = in.readLine()).equals("+++")) { CourseInfo courseinfo = new CourseInfo(); // Search for the string "INFSCI" if (line.indexOf("INFSCI") == -1) continue; else { // Extract information from the HTML file and // do the necessary analysis on each line courseinfo.id = line.substring(36, 40); courseinfo.title = line.substring(40, 72); line = in.readLine(); courseinfo.crn = line.substring(4,9); courseinfo.days = line.substring(10, 16); if(courseinfo.days.indexOf("BY APP") == -1) { courseinfo.begintime = line.substring(18, 24); courseinfo.endtime = line.substring(25, 31); } else { courseinfo.begintime = " "; courseinfo.endtime=" "; } String timecat = courseinfo.begintime.substring(5,6); if (courseinfo.begintime.equals(" ")) courseinfo.timecategory = "BY APP"; else { int hour = Character.digit(line.substring(19, 20).charAt(0), 10); if (courseinfo.days.charAt(5) == 'S') courseinfo.timecategory = "W"; else if (timecat.equals("A")) courseinfo.timecategory = "A"; else if (timecat.equals("P") && (hour > 4)) courseinfo.timecategory = "E"; else courseinfo.timecategory = "P"; } courseinfo.place = line.substring(32, 41); courseinfo.instructor = line.substring(62, 92); coursevtor.addElement(courseinfo); } } } } catch(IOException e) { System.out.println("Error: " + e.getMessage()); } } } // An applet for the third window class Window3 extends Applet { TextArea messagebox, validclasses; private int messageIndex; private int el, mff, csci, csys, st; private boolean[] validlist; public void init() { // The layout for this window is a BorderLayout setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new GridLayout(2,1)); Panel p0 = new Panel(); p0.setLayout(new BorderLayout()); Label l1 = new Label(); l1.setText("The Valid Selections Are:"); l1.setFont(new Font("TimesRoman", Font.BOLD, 14)); p0.add("North", l1); validclasses = new TextArea(15,40); validclasses.setEditable(false); validclasses.setFont(new Font("Courier", Font.PLAIN, 12)); p0.add("Center", validclasses); p.add(p0); Panel p1 = new Panel(); p1.setLayout(new BorderLayout()); Label l2 = new Label(); l2.setText("Messages:"); l2.setFont(new Font("TimesRoman", Font.BOLD, 14)); p1.add("North", l2); messagebox = new TextArea(15,40); messagebox.setEditable(false); messagebox.setFont(new Font("Courier", Font.PLAIN, 12)); p1.add("Center", messagebox); p.add(p1); add("Center", p); Panel p2 = new Panel(); p2.setLayout(new GridLayout(1, 14, 3, 3)); for (int i = 0; i < 5; i++) p2.add(new Label()); Button b1 = new Button(); b1.setFont(new Font("TimesRoman", Font.BOLD, 16)); b1.setLabel("< Back"); p2.add(b1); for (int i = 0; i < 3; i++) p2.add(new Label()); Button b2 = new Button(); b2.setFont(new Font("TimesRoman", Font.BOLD, 16)); b2.setLabel("Restart"); p2.add(b2); for (int i = 0; i < 4; i++) p2.add(new Label()); add("South", p2); } // Detect the action from the user public boolean action(Event evt, Object arg) { if (arg instanceof String) { String s = (String) arg; if (arg.equals("< Back")) { // Go back to the previous page card_layout.show(windows, "Window 2"); } else if (arg.equals("Restart")) { // Go to the first page and restart again cb1.setState(false); cb2.setState(false); cb3.setState(false); cb4.setState(false); for (int i = 0; i < selectedlist1.length; i++) courselist1.deselect(selectedlist1[i]); card_layout.show(windows, "Window 1"); } return super.action(evt, arg); } return true; } // This method is called from the second window public void start() { validlist = new boolean[selectedlist2.length]; for (int i = 0; i < selectedlist2.length; i++) validlist[i] = true; messageIndex = 1; el = mff = csci = csys = st = 0; validclasses.setText(" CRN " + " Course " + " Title " + " Day " + "B.Time " + "E.Time " + " Place " + " Instructor " + "\n"); messagebox.setText(""); CompareTime(); IsPreRequisiteTaken(); DisplayResults(); StudyPlan(); } // Compare two courses if they have time conflict public void CompareTime() { for (int i = 0; i < (selectedlist2.length - 1); i++) { String icoursedays = courselist2.getItem(selectedlist2[i]).substring(47, 53); if (!(icoursedays.equals("BY APP"))) { String icoursebegintime = courselist2.getItem(selectedlist2[i]).substring(55, 61); String icourseendtime = courselist2.getItem(selectedlist2[i]).substring(63, 69); for (int j = i + 1; j < selectedlist2.length; j++) { String jcoursedays = courselist2.getItem(selectedlist2[j]).substring(47, 53); if (!(jcoursedays.equals("BY APP"))) { String jcoursebegintime = courselist2.getItem( selectedlist2[j]).substring(55, 61); String jcourseendtime = courselist2.getItem( selectedlist2[j]).substring(63, 69); // if two classes are on the same day, check their time if (SameDate(icoursedays, jcoursedays)) { if (icoursebegintime.equals(jcoursebegintime) || icourseendtime.equals(jcourseendtime) || (TimeLessThan(icoursebegintime, jcoursebegintime) && TimeLessThan(jcoursebegintime, icourseendtime)) || (TimeLessThan(jcoursebegintime, icoursebegintime) && TimeLessThan(icoursebegintime, jcourseendtime))) { DisplayMessage( courselist2.getItem( selectedlist2[i]).substring(0, 11), courselist2.getItem( selectedlist2[j]).substring(0, 11), 1, ""); validlist[i] = false; validlist[j] = false; } } } } } } } // Return true if the 2 course strings have days in common public boolean SameDate(String A, String B) { for (int i = 0; i < 6; i++) { if ((A.charAt(i) != ' ') && (A.charAt(i) == B.charAt(i))) return true; } return false; } // Return true if time s is less than time t public boolean TimeLessThan(String s, String t) { // Time is in the format: hh:mmA/P where hh <= 12 char chs = s.charAt(5); char cht = t.charAt(5); if (chs == 'A' && cht == 'P') return true; else if (chs == 'P' && cht == 'A') return false; // Change the hours before comparing them int shour = Character.digit(s.charAt(0), 10) * 10 + Character.digit(s.charAt(1), 10); if ((chs == 'P') && (shour != 12)) shour += 12; int thour = Character.digit(t.charAt(0), 10) * 10 + Character.digit(t.charAt(1), 10); if ((cht == 'P') && (thour != 12)) thour += 12; if (shour < thour) return true; else if (shour > thour) return false; else if (s.substring(3,5).compareTo(t.substring(3,5)) <= 0) return true; else return false; } // A method to display a message in the message box public void DisplayMessage(String courseA, String courseB, int k, String m) { String message = null; if (k == 1) // Time conflict { message = messageIndex + ". Course " + courseA + " and course " + courseB + " have a time conflict\n"; } else if (k == 2) // Additional requirement { message = messageIndex + ". Course " + courseA + " has additional requirement\n" + " Pre//Co: " + m + "\n"; } else if (k == 3) // Prerequisite conflict { message = messageIndex + ". Course " + courseA + "'s pre-requisite has not been taken\n" + " Pre//Co: " + m + "\n"; } messagebox.appendText(message); messageIndex++; } // A method to check if the course's prerequisite was taken public void IsPreRequisiteTaken() { for (int i = 0; i < validlist.length; i++) { if (validlist[i]) { String iprerequisite = prereqlist1.getItem(i); int n = iprerequisite.indexOf("INFSCI"); if (n == -1) { if (!(iprerequisite.substring(5, 9).equals("None"))) DisplayMessage(iprerequisite.substring(0, 4), "", 2, iprerequisite.substring(5)); } else { n += 7; String precourse1 = iprerequisite.substring(n, n+4); if (iprerequisite.length() == (n+4)) { if (!TakenBefore(precourse1)) { DisplayMessage(iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } else { char ch1 = iprerequisite.charAt(n+4); if (ch1 == '/') { String precourse2 = iprerequisite.substring(n+5, n+9); if (!(TakenBefore(precourse1) || TakenBefore(precourse2))) { DisplayMessage(iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } else { if (!TakenBefore(precourse1)) { DisplayMessage(iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } if (validlist[i]) { if (ch1 == ',') { char ch2 = iprerequisite.charAt(n+6); if (Character.isDigit(ch2)) { String precourse2 = iprerequisite.substring(n+6, n+10); if (!TakenBefore(precourse2)) { DisplayMessage(iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } else { if (iprerequisite.lastIndexOf("INFSCI") > n) { String precourse2 = iprerequisite.substring( iprerequisite.lastIndexOf("INFSCI")+7, iprerequisite.lastIndexOf("INFSCI")+11); if (!TakenBefore(precourse2)) { DisplayMessage( iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } else { DisplayMessage(iprerequisite.substring(0, 4), "", 2, iprerequisite.substring(5)); } } } else { if (iprerequisite.lastIndexOf("INFSCI") > n) { String precourse2 = iprerequisite.substring( iprerequisite.lastIndexOf("INFSCI")+7, iprerequisite.lastIndexOf("INFSCI")+11); if (!TakenBefore(precourse2)) { DisplayMessage(iprerequisite.substring(0, 4), "", 3, iprerequisite.substring(5)); validlist[i] = false; } } else { DisplayMessage(iprerequisite.substring(0, 4), "", 2, iprerequisite.substring(5)); } } } } } } } } // A method to check if the course was taken by using // the information obtained from the first window public boolean TakenBefore(String course) { for (int i = 0; i < selectedlist1.length; i++) { if (courselist1.getItem( selectedlist1[i]).substring(6, 10).equals(course)) return true; } return false; } // A method to display the valid courses for the student public void DisplayResults() { validcoursecount = 0; for (int i = 0; i < validlist.length; i++) { if (validlist[i]) { validcoursecount++; String message = validcoursecount + ". " + crnlist.getItem(selectedlist2[i]) + " " + courselist2.getItem(selectedlist2[i]) + "\n"; validclasses.appendText(message); } } } // A method to calculate total number of courses taken // in each study area public void StudyPlan() { int i; String message; for (i = 0; i < selectedlist1.length; i++) CheckCourseCategory(courselist1.getItem( selectedlist1[i]).substring(6, 10)); for (i = 0; i < validlist.length; i++) if (validlist[i]) CheckCourseCategory(courselist2.getItem( selectedlist2[i]).substring(7, 11)); message = messageIndex + ". Including the above courses, you have:\n" + " " + mff + " Mathematical and Formal Foundations courses\n" + " " + csci + " Cognitive Science courses\n" + " " + csys + " Cognitive Systems courses\n" + " " + st + " Systems and Technology courses\n" + " " + el + " Electives\n"; messagebox.appendText(message); messageIndex++; } // A method to categorize the course public void CheckCourseCategory(String course) { String courseprefix = course.substring(0, 2); if (course.equals("2600") || course.equals("2901") || course.equals("2940") || course.equals("2950")) el++; else if (courseprefix.equals("20") || courseprefix.equals("21") || course.equals("2921") || course.equals("2931")) mff++; else if (courseprefix.equals("23") || course.equals("2923") || course.equals("2933")) csci++; else if (courseprefix.equals("24") || course.equals("2924") || course.equals("2934")) csys++; else if (courseprefix.equals("25") || courseprefix.equals("26") || courseprefix.equals("27") || courseprefix.equals("28") || course.equals("2925") || course.equals("2935") || course.equals("2926") || course.equals("2936") || course.equals("2927") || course.equals("2937") || course.equals("2928") || course.equals("2938")) st++; } } }