/*************************************************** Title: Course Registration Applet Author: Carl Altenderfer Date: April 1998 File: sched.java Language: Java 1.0 ***************************************************/ /* This applet is a tool to develop a weekly class schedule. * The schedule is developed in a different frame from the * main applet screen. Courses are selected from the menu * at the top of the frame. Selecting a course subject * downloads a raw text file containing class selections * from the server. The courses are displayed in a separate * frame. Select a course by single clicking the test line. * The course is added to the schedule and a list is * maintained for all selected courses with the total * amount of credit hours displyed. You may not select more * than 18 credits, and you may not select classes that * conflict on the schedule; warning messages will pop up * in either case explaining the problem. *********************************************************/ /******************************************************* * Server program: FileReader.class * Course file format: class credits days time general-info *********************************************************/ import java.awt.*; import java.applet.*; import java.lang.*; import java.util.*; import java.io.*; import java.net.*; public class sched extends Applet { mainframe mf; String message; public void init () { message = "The course scheduling program is running."; } //end init public void paint(Graphics g){ Dimension myAppletDim = size(); setBackground(Color.green); g.drawString(message, 30, (myAppletDim.height/2)); } //end paint public void start(){ mf = new mainframe(this); } //end start public void stop(){ mf.removeFrame(); while (mf.cf_frames.size() > 0){ coursesframe temp_f; temp_f = (coursesframe)mf.cf_frames.firstElement(); temp_f.removeFrame(); } message = "Course Scheduling program stopped."; } //end stop public void distroy(){ mf.removeFrame(); mf.course_frame.removeFrame(); } //end distroy } //end class sched extends Applet // MAINFRAME CLASS ****************************************** class mainframe extends Frame { sched sched; Image img; // MU logo coursesframe course_frame; Vector courses = new Vector(); Vector cf_frames = new Vector(); //a vector to track all course frames; int MIN_SELECT, MAX_SELECT; int cell_h = 15; int credits = 0; textcell time[] = new textcell[13]; textcell day[] = new textcell[8]; textcell table[][] = new textcell[15][15]; textcell title_line, direction1, direction2; String tokens[]; String menu_items[] = { "Art", "Communications", "English", "Foreign Languages", "Humanities", "Music", "Biology", "Chemistry", "Computer Science", "Earth Science", "Mathematics", "Nursing", "Physics", "Anthropology", "Business Admin", "Economics", "Education", "Geography", "History", "Political Science", "Psychology", "Sociology", "Special Education", "Health", "Physical Education", }; String frame_names[] = {"Art Classes", "Communications Classes", "English Classes", "Foreign Language Classes", "Humanities Classes", "Music Classes", "Biology Classes", "Chemistry Classes", "Computer Science Classes", "Earth Science Classes", "Mathematics Classes", "Nursing Classes", "Physics Classes", "Anthropology Classes", "Business Admin Classes", "Economics Classes", "Education Classes", "Geography Classes", "History Classes","Political Science Classes", "Psychology Classes", "Sociology Classes", "Special Education Classes", "Health Classes", "Physical Education Classes", }; String file_names[] = {"art.txt", "comm.txt", "engl.txt", "lang.txt", "hum.txt", "music.txt", "biology.txt", "chemistry.txt", "compsci.txt", "earthsci.txt", "math.txt", "nurse.txt", "physics.txt", "anth.txt","business.txt", "econ.txt", "edu.txt", "geo.txt", "history.txt","polysci.txt", "psych.txt", "socio.txt", "spcled.txt", "health.txt", "pe.txt", }; public mainframe(sched schedule) { super( "Millersville University Scheduling Program" ); sched = schedule; framemenu menu_bar = new framemenu(this); this.setBackground(Color.white); this.addCells(); MediaTracker t = new MediaTracker(this); img = sched.getImage(sched.getCodeBase(), "mu_logo.gif"); t.addImage(img, 1); try { t.waitForID(1); } catch(Exception e){ popupwindow window = new popupwindow("Problems loading MU logo", e.toString()); } resize(700, 600); show(); } // end mainframe constructor //ADDCELLS public void addCells(){ String t1 = " "; String t2 = "STUDENT CLASS SCHEDULE PROGRAM "; String t3 = "written by Carl Altenderfer"; String d1 = " "; String d2 = " Select Courses From Menu Above"; String days[] = { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", }; String times[] = {"TIME", " 8:00", " 9:00", "10:00", "11:00", "12:00", " 1:00", " 2:00", " 3:00", " 4:00", " 5:00", " 6:00", }; title_line = new textcell(655, cell_h, "", Color.white); title_line.setCellText(t1 + t2 + t3); title_line.setBorderColor(Color.white); direction1 = new textcell(655, cell_h, "", Color.white); direction1.setCellText(d1 + d2); direction1.setBorderColor(Color.red); direction2 = new textcell(655, cell_h, "", Color.white); for (int i=0; i<6; i++) { day[i] = new textcell(100, 15); day[i].setCellText(days[i]); } for (int i=0; i<12; i++) { time[i] = new textcell(40, 15, times[i]); for (int j=0; j<6; j++) { table[i][j] = new textcell(100, 15); } } } // end addCells method //MAINFRAME PAINT public void paint(Graphics g){ int x, y, start_x, start_y; int v_spacing = 102; int h_spacing = 17; start_x = x = 10; start_y = y = 180; try{ //draw MU logo g.drawImage( img, 150, 75, 376, 100, this); } catch(Exception e){ popupwindow window = new popupwindow("Problems Drawing MU logo", e.toString()); } try{ //add title and direction lines title_line.paint(g, x, y); y += 17; direction1.paint(g, x, y); } catch(Exception e){ popupwindow window = new popupwindow("Problems Red Line", e.toString()); } try{ //add "time" top/left x = start_x; y = start_y + 40; time[0].paint(g, x, y); } catch(Exception e){ popupwindow window = new popupwindow("Problems w/ first time", e.toString()); } try{ //add days of week on top x = x+45; for (int i=0; i<6; i++) { day[i].paint(g, x, y); x = x + v_spacing; } } catch(Exception e){ popupwindow window = new popupwindow("Problems in Day Loop", e.toString()); } try{ //add times left column x = start_x; y = start_y + 60; for (int i=1; i<12; i++) { time[i].paint(g, x, y); y = y + h_spacing; } } catch(Exception e){ popupwindow window = new popupwindow("Problems multi time", e.toString()); } try{ //add table cells x = start_x + 45; y = start_y + 60; for (int i=0; i<11; i++) { for (int j=0; j<6; j++) { table[i][j].paint(g, x, y); x = x + v_spacing; } x = start_x + 45; y = y + h_spacing; } } catch(Exception e){ popupwindow window = new popupwindow("Problems painting table", e.toString()); } try{ // add course list at bottom of page if (courses.size()>0) { x = start_x; MIN_SELECT = y = y+17; String d1 = Integer.toString(credits); String d2 = " Credit Hours Scheduled "; String d3 = "Click on Course Below to Remove From Schedule"; direction2.setCellText(d1 + d2 + d3); direction2.setBorderColor(Color.red); direction2.paint(g, x, y); y += h_spacing; for (int i=0; iMIN_SELECT) && (y= c.y) ) { // System.out.println("Up Event: "+ c.getText()); parse_line(c.getText()); int start_time = get_start_time(); String course = (String)tokens[0]; //Get each day and convert to an integer String days = (String)tokens[2]; int day_num[] = new int[days.length()]; for (int j = 0; j < days.length(); j++){ if ( days.charAt(j) == 'M' ) day_num[j] = 0; else if ( days.charAt(j) == 'T' ) day_num[j] = 1; else if ( days.charAt(j) == 'W' ) day_num[j] = 2; else if ( days.charAt(j) == 'H' ) day_num[j] = 3; else if ( days.charAt(j) == 'F' ) day_num[j] = 4; else if ( days.charAt(j) == 'S' ) day_num[j] = 5; } //SUBTRACT CREDITS fm main frame int tmp_credits = (int)tokens[1].charAt(0); tmp_credits -= 48; credits -= tmp_credits; //REMOVE COURSE TO TABLE for (int j = 0; j < days.length(); j++){ table[start_time][day_num[j]].setCellText(""); } //change color of cell and remove from list c.setCellColor(Color.white); courses.removeElementAt(i); repaint(); } } //end for } //end if return true; } //end mouseUp //EVENT HANDLER public boolean handleEvent( Event e ) { if (e.id == Event.WINDOW_DESTROY ) { removeFrame(); return true; } return super.handleEvent(e); } //end handleEvent //PARSE_LINE public void parse_line(String s){ //want first four elements in string //1-class, 2-credits, 3-days. 4-time tokens = new String[5]; StringTokenizer t = new StringTokenizer(s); for (int i=0; i<4; i++) { String token = t.nextToken(); tokens[i] = token; } } // end parse_line //GET_START_TIME public int get_start_time() { int start_time = (int)tokens[3].charAt(0); start_time -= 48; if (tokens[3].length() > 4){ start_time *= 10; int temp_n = (int)tokens[3].charAt(1); temp_n -= 48; start_time += temp_n; } //adjust number to fit table layout if (start_time > 7) start_time -= 8; else start_time += 4; return start_time; } //REMOVEFRAME public void removeFrame() { dispose(); } //end removeFrame } // end class mainframe // FRAMEMENU CLASS ******************************************* class framemenu{ MenuBar mb = new MenuBar(); Menu art = new Menu("Humanities & Art", true); Menu math = new Menu("Science & Math", true); Menu soc = new Menu("Social Science", true); Menu pe = new Menu("Health & PE", true); Menu submit = new Menu("Submit Schedule"); Menu help = new Menu( "About" ); public framemenu(Frame f) { art.add(new MenuItem ("Art")); art.add(new MenuItem ("Communications")); art.add(new MenuItem ("English")); art.add(new MenuItem ("Foreign Languages")); art.add(new MenuItem ("Humanities")); art.add(new MenuItem ("Music")); math.add(new MenuItem ("Biology")); math.add(new MenuItem ("Chemistry")); math.add(new MenuItem ("Computer Science")); math.add(new MenuItem ("Earth Science")); math.add(new MenuItem ("Mathematics")); math.add(new MenuItem ("Nursing")); math.add(new MenuItem ("Physics")); soc.add(new MenuItem ("Anthropology")); soc.add(new MenuItem ("Business Admin")); soc.add(new MenuItem ("Economics")); soc.add(new MenuItem ("Education")); soc.add(new MenuItem ("Geography")); soc.add(new MenuItem ("History")); soc.add(new MenuItem ("Political Science")); soc.add(new MenuItem ("Psychology")); soc.add(new MenuItem ("Sociology")); soc.add(new MenuItem ("Special Education")); pe.add(new MenuItem ("Health")); pe.add(new MenuItem ("Physical Education")); submit.add(new MenuItem ("Submit")); help.add(new MenuItem ("About")); mb.add(art); mb.add(math); mb.add(soc); mb.add(pe); mb.add(submit); mb.add(help); f.setMenuBar(mb); f.show(); } // end constructor } // end class framemenu // COURSESFRAME CLASS ********************************** class coursesframe extends Frame{ public static final int PORT = 8424; Vector cf_courses = new Vector(); String tokens[]; int MIN_SELECT, MAX_SELECT; int cell_h = 15; textcell direction; mainframe mf; public coursesframe(String title, mainframe f, String file) { super(title); mf = f; this.setBackground(Color.blue); resize(700, 300); Socket s; DataInputStream in; PrintStream out; String line; String d1 = " "; String d2 = "Click on Course Below to Add to Schedule"; direction = new textcell(655, cell_h, d1 + d2, Color.blue); direction.setFontColor(Color.white); try { s = new Socket(mf.sched.getCodeBase().getHost(), PORT); in = new DataInputStream(s.getInputStream()); out = new PrintStream(s.getOutputStream()); //sending file name to server out.println(file); // Reading data sent from server while ( (line = in.readLine() ) != null) { add_course(line); } //end while loop in.close(); } // end try catch (Exception e) { popupwindow window = new popupwindow("Problem reading file", e.toString()); } this.show(); } //ADD_COURSE public void add_course(String S){ textcell c = new textcell(655, cell_h, S, Color.white); cf_courses.addElement(c); } //PAINT public void paint(Graphics g){ int x, y, start_x, start_y; int v_spacing = 102; int h_spacing = 17; start_x = x = 10; start_y = y = 30; try{ MIN_SELECT = y = y+20; direction.paint(g, x, y); y += h_spacing; for (int i=0; iMIN_SELECT) && (y= c.y) ) { parse_line(c.getText()); start_time = get_start_time(); String course = (String)tokens[0]; String days = (String)tokens[2]; int day_num[] = new int[days.length()]; for (int j = 0; j < days.length(); j++){ if ( days.charAt(j) == 'M' ) day_num[j] = 0; else if ( days.charAt(j) == 'T' ) day_num[j] = 1; else if ( days.charAt(j) == 'W' ) day_num[j] = 2; else if ( days.charAt(j) == 'H' ) day_num[j] = 3; else if ( days.charAt(j) == 'F' ) day_num[j] = 4; else if ( days.charAt(j) == 'S' ) day_num[j] = 5; } //TEST TABLE FOR CELL OCCUPANCY for (int j = 0; j < days.length(); j++){ String temp = mf.table[start_time][day_num[j]].getText(); if (temp.length() > 0) { popupwindow window = new popupwindow("Problem Time Conflict", "You already have a class scheduled at this time"); conflict = true; break; } } if (conflict) break; //Stop process if time conflict //ADD CREDITS to main frame int credits = (int)tokens[1].charAt(0); credits -= 48; int temp_c = mf.credits + credits; //Do not permit more than 18 hours on sked if (temp_c > 18){ popupwindow window = new popupwindow("Problem Maximum Credits", "This class gives you over 18 credits"); conflict = true; } if (conflict) break; //Stop process if exceeding max credits //ADD CREDITS TO SCHEDULE mf.credits += credits; //ADD COURSE TO TABLE for (int j = 0; j < days.length(); j++){ mf.table[start_time][day_num[j]].setCellText(course); } c.setCellColor(Color.green); mf.courses.addElement(c); mf.repaint(); this.repaint(); break; //end for loop // System.out.println("Start time: "+ Integer.toString(start_time)); } //Checking selected cells if ( (y <= (c.y + cell_h)) && (y >= c.y) ) { parse_line(c.getText()); start_time = get_start_time(); String course = (String)tokens[0]; String days = (String)tokens[2]; int day_num[] = new int[days.length()]; for (int j = 0; j < days.length(); j++){ if ( days.charAt(j) == 'M' ) day_num[j] = 0; else if ( days.charAt(j) == 'T' ) day_num[j] = 1; else if ( days.charAt(j) == 'W' ) day_num[j] = 2; else if ( days.charAt(j) == 'H' ) day_num[j] = 3; else if ( days.charAt(j) == 'F' ) day_num[j] = 4; else if ( days.charAt(j) == 'S' ) day_num[j] = 5; } //SUBTRACT CREIDTS fm main frame int credits = (int)tokens[1].charAt(0); credits -= 48; mf.credits -= credits; //REMOVE COURSE TO TABLE for (int j = 0; j < days.length(); j++){ mf.table[start_time][day_num[j]].setCellText(""); } c.setCellColor(Color.white); mf.courses.removeElement(c); mf.repaint(); this.repaint(); break; // end for loop } } //end for } //end if return true; } //end mouseUp //PARSE_LINE public void parse_line(String s){ //want first four elements in string //0-class, 1-credits, 2-days. 3-time tokens = new String[5]; StringTokenizer t = new StringTokenizer(s); for (int i=0; i<4; i++) { String token = t.nextToken(); tokens[i] = token; } } //GET_START_TIME public int get_start_time() { int start_time = (int)tokens[3].charAt(0); start_time -= 48; if (tokens[3].length() > 4){ start_time *= 10; int temp_n = (int)tokens[3].charAt(1); temp_n -= 48; start_time += temp_n; } //adjust number to fit table layout if (start_time > 7) start_time -= 8; else start_time += 4; return start_time; } // 1.0 EVENT HANDLER public boolean handleEvent( Event e ) { if (e.id == Event.WINDOW_DESTROY ) { removeFrame(); return true; } return super.handleEvent(e); } //end handleEvent //REMOVEFRAME public void removeFrame() { dispose(); mf.cf_frames.removeElement(this); } //end removeFrame } //end class coursesframe // TEXTCELL CLASS ****************************************** class textcell extends Object { int cellWidth, cellHeight, x, y; String cellText = ""; Color cellColor = Color.yellow; Color borderColor = Color.black; Color fontColor = Color.black; public textcell() { super(); this.cellWidth = 10; this.cellHeight = 15; } public textcell(int W, int H) { super(); this.cellWidth = W; this.cellHeight = H; } public textcell(int W, int H, String S) { super(); this.cellWidth = W; this.cellHeight = H; this.cellText = S; } public textcell(int W, int H, String S, Color C) { super(); this.cellWidth = W; this.cellHeight = H; this.cellText = S; this.cellColor = C; } public void setSize(int W, int H) { this.cellWidth = W; this.cellHeight = H; } public void setPosition (int X, int Y){ this.x = X; this.y = Y; } public void setCellColor(Color c) { this.cellColor = c; } public void setFontColor(Color c) { this.fontColor = c; } public void setBorderColor(Color c) { this.borderColor = c; } public void setCellText(String s) { this.cellText = s; } public String getText(){ return this.cellText; } public void paint(Graphics g, int x, int y) { g.setColor(cellColor); g.fillRect(x, y, cellWidth, cellHeight); g.setColor(borderColor); g.drawRect(x, y, cellWidth, cellHeight); g.setColor(fontColor); g.drawString(cellText, x + 5, y + (cellHeight / 2) + 5); } } //end class textcell //POP UP WINDOW class ****************************************** class popupwindow extends Frame { public Panel buttonPanel; public Button close; public popupwindow(String title, String mesg){ //Instantiate variables super(title); Label return_message = new Label(mesg); buttonPanel = new Panel(); close = new Button("OK"); //Add button to panel and panel to window setLayout(new FlowLayout()); buttonPanel.setLayout(new GridLayout(2,1)); buttonPanel.add(return_message); buttonPanel.add(close); add(buttonPanel); //Size window to preferred size this.reshape(100, 100, 400, 150); move(150,300); close.requestFocus(); show(); // super.resize(); show(); }//end popupwindow(String, String) constructor // uses the jdk1.0.2 event handler... public boolean action(Event e, Object arg){ if(e.target instanceof Button){ if(e.target == close){ if(this.isShowing()) { dispose(); } } } return true; }//end action(Event, Object) } //end class POP UP WINDOW