import java.io.*; import java.sql.*; import java.util.*; /** * The Processor takes the transcript of the student and the choices he/she made after loging into the SAS. * All requirements from the database are read, parsed and then the student record is evaluated agains those * requirements. A list of all requirements that are not met is generated. */ public class Processor { /** * Holds the chosen major */ private String p_major; // MAJOR CHOSEN private String p_minor; // MINOR CHOSEN private String p_sec_major; // SECOND MAJOR private String p_trans_credits; // NUMBER OF TRANSFERED CREDITS private String p_core_needed; // NUMBER OF CORE CREDITS TO SATISFY, IF TRANSFER private String p_total_credits; // TOTAL NUMBER OF CREDITS private int p_total_liberal_art; // TOTAL OF LIBERAL ART COURSES TAKEN private ArrayList p_courses_taken; // COURSE_LIST FROM TRANSCRIPT private ArrayList p_courses_to_take; // FINAL COURSE_LIST WITH COURSES TO TAKE private ArrayList p_courses_to_take_temp; // COURSE_LIST FOR PARTICULAR REQUIREMENT private ArrayList p_not_allowed; // COURSES ALREADY COUNTED ONCE private ArrayList p_not_allowed_temp; // COURSES ALREADY COUNTED ONCE FOR THIS ITERATION /** * Initializes a newly created Processor with an object of type Map as input. * The constructor also initializes the internal fields it needs and allocates memory for the ArrayList fields. */ public Processor(Map vars) { p_courses_taken = (ArrayList) vars.get("courses"); p_major = (String) vars.get("major"); p_minor = (String) vars.get("minor"); p_sec_major = (String) vars.get("secondmajor"); p_trans_credits = (String) vars.get("transfercredits"); p_core_needed = (String) vars.get("coreneeded"); p_total_credits = (String) vars.get("totalcredits"); p_total_liberal_art = 0; p_courses_to_take = new ArrayList(20); p_courses_to_take_temp = new ArrayList(20); p_not_allowed = new ArrayList(40); p_not_allowed_temp = new ArrayList(40); } /** * Top-most level function. Performs all necessary computations and gives the final result. */ public ArrayList go() { /* EXAMINE THE CORE REQUIREMENTS */ if( p_trans_credits.equals("no") || p_trans_credits.length()==1) computeCore(); else computeTransfer(); /* EXAMINE THE MAJOR REQUIREMENTS */ if( p_sec_major.equals("no") ) computeMajor(); else { computeMajor(); computeSecondMajor(); } /* EXAMINE THE MINOR REQUIREMENTS */ computeMinor(); /* EXAMINE THE COLLEGE WIDE REQUIREMENTS */ computeCollege_wide(); /* EXAMINE THE GENERAL REQUIREMENTS */ computeGeneral(); return p_courses_to_take; } /** * Parses the Expression into basic terms. * The result is a list of terms that is put in the ArrayList provided as input. */ public int parseExpression(String input, ArrayList list_of_str) { ArrayList courses_to_check = new ArrayList(15); boolean AND = true; boolean OR = true; if(input.indexOf( "L", 0)!= -1 ) // DEAL WITH EXPRESION "L" { ResultSet liberal_arts = Retriever.p_get_liberal_arts(); try{ while(liberal_arts.next()) list_of_str.add(liberal_arts.getString(1)); }catch(SQLException e){System.err.println("SQL Exception " + e.getMessage());} return 0; } if(input.indexOf( "^", 0)!= -1 ) // DEAL WITH EXPRESIONS LIKE ^223 { String level= ""+input.charAt(1); String displ= input.substring(2); ResultSet level_courses = Retriever.p_get_all_with_level(level,displ); try{ while(level_courses.next()) list_of_str.add(level_courses.getString(1)); }catch(SQLException e){System.err.println("SQL Exception " + e.getMessage());} return 0; } if(input.indexOf( "*(", 0)== -1 && input.indexOf(")*",0) == -1) // PARSE PARENS AND=false; if(input.indexOf( "|(", 0)== -1 && input.indexOf(")|",0) == -1) OR=false; if(AND && OR) { System.err.println("Inconsistent boolean expresion! Check specification in manual"); return -1; } else if(!AND && !OR) // PARSE EXPRESIONS WITHOUT PARENS { AND=true; OR=true; if(input.indexOf( "*", 0) == -1) AND=false; if(input.indexOf( "|", 0) == -1) OR=false; if(AND && OR) { System.err.println("Inconsistent2 boolean expresion! Check specification in manual"); return -1; } } String temp=""; // ACTUAL PARSING OF TERMS INTO A LIST if(AND) temp="*()"; else temp="|()"; StringTokenizer st2 = new StringTokenizer(input,temp,false); while(st2.hasMoreTokens()) {list_of_str.add(st2.nextToken());} if(AND) // RETURN INFORMATION ABOUT OPERATOR return 1; else return 0; } /** * Evaluates a term delimited with the | operator */ public ResultTerm eval_or_term(String input) { StringTokenizer st = new StringTokenizer(input,"/",false); int credits_in_term = 0; int credits_left=0; ArrayList left_in_term= new ArrayList(5); boolean satisfied=true; while(st.hasMoreTokens()) { String course_to_check =(String)st.nextToken(); /*BEGIN CHECKING IF THE COURSE IS ALREADY TAKEN */ if( is_it_allowed(course_to_check)) { /* CHECK IF THE COURSE IS TAKEN */ int flag = is_it_taken(course_to_check); if(flag > 0) { credits_in_term = credits_in_term + flag; System.out.println("the course " + course_to_check + " was taken"); p_not_allowed_temp.add(course_to_check); left_in_term.clear(); satisfied=true; ResultTerm res= new ResultTerm(credits_in_term,0,left_in_term,satisfied); return res; } else { System.out.println("the course " + course_to_check + " was NOT taken"); credits_left = credits_left+ Retriever.p_getCredits(course_to_check); left_in_term.add(course_to_check); satisfied=false; } } } ResultTerm res= new ResultTerm(credits_in_term,credits_left,left_in_term,satisfied); return res; } /** * Evaluates a term delimited with the * operator */ public ResultTerm eval_and_term(String input) { StringTokenizer st = new StringTokenizer(input,"*",false); int credits_in_term = 0; int credits_left=0; ArrayList left_in_term= new ArrayList(5); boolean satisfied=true; while(st.hasMoreTokens()) { String course_to_check =(String)st.nextToken(); /*BEGIN CHECKING IF THE COURSE IS ALREADY TAKEN */ if( is_it_allowed(course_to_check)) { /* CHECK IF THE COURSE IS TAKEN */ int flag = is_it_taken(course_to_check); if(flag > 0) { credits_in_term = credits_in_term + flag; System.out.println("the course " + course_to_check + " was taken"); p_not_allowed_temp.add(course_to_check); } else { System.out.println("the course " + course_to_check + " was NOT taken"); credits_left = credits_left+ Retriever.p_getCredits(course_to_check); left_in_term.add(course_to_check); satisfied=false; } } } ResultTerm res= new ResultTerm(credits_in_term,credits_left,left_in_term,satisfied); return res; } /** * Checks if a course is allowed to be considered for this requirement. */ public boolean is_it_allowed(String course) // IS THE COURSE ALLOWED TO BE CONSIDERED FOR THIS REQUIREMENT { for(int b = 0; b < p_not_allowed.size(); b++) if(course.compareTo(p_not_allowed.get(b))==0) return false; return true; } /** * Checks if a course has already been taken. * If not taken returns -1 if taken returns number of credits for this cou rse. */ public int is_it_taken(String course) // IS THE COURSE ALREADY TAKEN { for(int i=0;i=core_limit) { p_courses_to_take.add("you satisfied the Core requirements"); done=true; break; } } rs_core.close(); }catch(SQLException e){System.out.println("SQLException " + e.getMessage());} if(!done) { String temp ="You have to take a total of " + (core_limit - core_sofar) + " credits from the options below | "; p_courses_to_take.add(temp); for(int c=0; cla_credits) la_credits = la_credits_sec_major; } is_it_LiberalArt(); if( la_credits > p_total_liberal_art) { String temp = "you have to take " + (la_credits - p_total_liberal_art) + " more Liberal Arts credits"; p_courses_to_take.add(temp); } /* CHECK FOR TOTAL CREDITS SATISFIED */ int mysql_total_credits = Retriever.p_getTotalCredits(p_major); int transcript_total_credits = new Integer(p_total_credits).intValue(); if(p_sec_major.equals("no")) { if( mysql_total_credits > transcript_total_credits) { String temp = "you have to take " + (mysql_total_credits - transcript_total_credits) + " more credits as a whole"; p_courses_to_take.add(temp); } } else if(!p_sec_major.equals("no")) { mysql_total_credits = mysql_total_credits + 30; if(mysql_total_credits > transcript_total_credits) { String temp = "you have to take " + (mysql_total_credits - transcript_total_credits) + " more credits as a whole"; p_courses_to_take.add(temp); } } } /** * Evaluates a single Expression. */ public int calculate(int req_credits, String to_be_parsed) { ArrayList p_terms_to_check = new ArrayList(10); int credits_taken = req_credits; int operator = parseExpression(to_be_parsed,p_terms_to_check); /* PARSING THE REQ IN AN ARRAYLIST OF TERMS TO EVALUATE AND DETERMINING THE OPERATOR FOR TERMS*/ p_not_allowed_temp.clear(); ArrayList p_out_to_take = new ArrayList(15); if(operator== 1) { boolean sat=true; for(int q = 0; q < p_terms_to_check.size(); q++) { ResultTerm res = eval_or_term((String)p_terms_to_check.get(q)); if(res.is_it_satisfied()) req_credits = req_credits - res.get_credits_in_term(); else { sat=false; ArrayList temp = res.get_courses_left_in_term(); for(int t = 0; t < temp.size(); t++) p_out_to_take.add( (String)temp.get(t)); } } if(!sat) /* IF YOU DID NOT SATISFY THIS REQUIREMENT */ { output(p_out_to_take,req_credits); credits_taken = credits_taken-req_credits; } else { System.out.println("You satisfied this requirement"); } } else { for(int q = 0; q < p_terms_to_check.size(); q++) { ResultTerm res = eval_and_term((String)p_terms_to_check.get(q)); if(res.is_it_satisfied()) req_credits = req_credits - res.get_credits_in_term(); else { ArrayList temp = res.get_courses_left_in_term(); for(int t = 0; t < temp.size(); t++) p_out_to_take.add( (String)temp.get(t)); } } if(req_credits > 0) /* IF YOU DID NOT SATISFY THIS REQUIREMENT */ { output(p_out_to_take,req_credits); credits_taken = credits_taken-req_credits; } else { System.out.println("You satisfied this requirement"); } } // System.out.println(" The list of not allowed courses"); // for(int f = 0;f < p_not_allowed.size();f++) // System.out.print(p_not_allowed.get(f) + " "); // System.out.println(""); // SYNCRONIZE TEMP AND GLOBAL NOT ALLOWED LIST for(int g=0; g< p_not_allowed_temp.size(); g++) p_not_allowed.add(p_not_allowed_temp.get(g)); return credits_taken; } }