Created
November 6, 2012 02:08
-
-
Save bored-engineer/4022095 to your computer and use it in GitHub Desktop.
GroundGrades v1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.innoying; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.sql.Statement; | |
| /** | |
| * | |
| * @author innoying | |
| */ | |
| class Assignment { | |
| //The course id | |
| int id = 0; | |
| //Create a new class with id | |
| public Assignment(int assign_id){ | |
| //Setup the id | |
| id = assign_id; | |
| } | |
| //Create a new class with info | |
| public Assignment(String name, int maxScore, int course_id) throws SQLException { | |
| //Prepared Statement | |
| String prepSelect = "INSERT INTO `Assignment` (`id`,`Name`,`Course_id`,`Max_Score`) VALUES (NULL,?,?,?);"; | |
| //Create a new statement for checking the login | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect, Statement.RETURN_GENERATED_KEYS); | |
| //Set the variables | |
| stmt.setString(1, name); | |
| stmt.setInt(2, course_id); | |
| stmt.setInt(3, maxScore); | |
| //Get results | |
| stmt.executeUpdate(); | |
| //Setup the id for this class instance | |
| ResultSet results = stmt.getGeneratedKeys(); | |
| //Get next line | |
| results.next(); | |
| //Get the id | |
| id = results.getByte(1); | |
| } | |
| //Get the class name | |
| public String getName() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Name from `Assignment` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the name | |
| return results.getString(1); | |
| } | |
| //Get the class maxsize | |
| public int getCourse_id() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Course_id from `Assignment` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the maxSize | |
| return results.getInt(1); | |
| } | |
| //Get the teacher id | |
| public int getMaxScore() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Max_Score from `Assignment` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the teacher_id | |
| return results.getInt(1); | |
| } | |
| //Get the course id | |
| public int getId() throws SQLException{ | |
| //Return the course id | |
| return id; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.innoying; | |
| import java.sql.Date; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| /** | |
| * | |
| * @author innoying | |
| */ | |
| class Attendance { | |
| //The course id | |
| int course_id = 0; | |
| int student_id = 0; | |
| Date date = new Date(0,0,0); | |
| //Create a new class with id | |
| public Attendance(int astudent_id, int acourse_id, Date adate){ | |
| //Setup the ids | |
| student_id = astudent_id; | |
| course_id = astudent_id; | |
| date = adate; | |
| } | |
| //Create a new class with info | |
| public Attendance(int status, int astudent_id, int acourse_id, Date adate) throws SQLException { | |
| //Prepared Statement | |
| String prepSelect = "INSERT INTO `Attendance` (`Student_id`,`Course_id`,`Day`,`Status`) VALUES (?,?,?,?);"; | |
| //Create a new statement | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect); | |
| //Set the variables | |
| stmt.setInt(1, astudent_id); | |
| stmt.setInt(2, acourse_id); | |
| stmt.setDate(3, adate); | |
| stmt.setInt(4, status); | |
| //Get results | |
| stmt.executeUpdate(); | |
| //Setup the ids | |
| student_id = astudent_id; | |
| course_id = astudent_id; | |
| date = adate; | |
| } | |
| //Get the class name | |
| public int getStatus(){ | |
| //Catch row not existing | |
| try { | |
| //Prepared Statement | |
| String prepSelect = "SELECT Status from `Attendance` WHERE `Student_id`=? AND `Course_id`=? AND `Day`=?"; | |
| //Statement | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect); | |
| //Set data | |
| stmt.setInt(1, student_id); | |
| stmt.setInt(2, course_id); | |
| stmt.setString(3, ((date.getYear()+1900)+"-"+(date.getMonth())+"-"+(date.getDate()))); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(); | |
| //Select the row | |
| results.next(); | |
| //Return the name | |
| return results.getInt(1); | |
| } catch (SQLException ex) { | |
| //Status 0, not filled out yet | |
| return 0; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.innoying; | |
| import java.sql.SQLException; | |
| public class Calendar { | |
| //Get the day | |
| public static int day(int M, int D, int Y) { | |
| int y = Y - (14 - M) / 12; | |
| int x = y + y/4 - y/100 + y/400; | |
| int m = M + 12 * ((14 - M) / 12) - 2; | |
| int d = (D + x + (31*m)/12) % 7; | |
| return d; | |
| } | |
| // return true if the given year is a leap year | |
| public static boolean isLeapYear(int year) { | |
| //if year is divisible by 4 and not 100 | |
| if ((year % 4 == 0) && (year % 100 != 0)) { | |
| return true; | |
| } | |
| //If year is divisible by 400 | |
| if (year % 400 == 0) { | |
| return true; | |
| } | |
| //Not leap year, return false | |
| return false; | |
| } | |
| //Constructor | |
| public Calendar(Student student, Course course, int M, int Y) throws SQLException { | |
| Y += 1900; | |
| //Name of each month | |
| String[] months = { | |
| "", // leave empty so that months[1] = "January" | |
| "January", "February", "March", | |
| "April", "May", "June", | |
| "July", "August", "September", | |
| "October", "November", "December" | |
| }; | |
| //Number of days in month i | |
| int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | |
| //Check for leap year | |
| if (M == 2 && isLeapYear(Y)) days[M] = 29; | |
| //Print header | |
| System.out.println(" " + months[M] + " " + Y); | |
| System.out.println(" S M Tu W Th F S"); | |
| //Starting day | |
| int d = day(M, 1, Y); | |
| //Print the calendar | |
| for (int i = 0; i < d; i++) | |
| System.out.print(" "); | |
| for (int i = 1; i <= days[M]; i++) { | |
| //Create with status | |
| switch(new Attendance(student.getId(), course.getId(), new java.sql.Date(Y-1900, M, i)).getStatus()){ | |
| case 1: | |
| System.out.print("\u001B[32m"); | |
| break; | |
| case 2: | |
| System.out.print("\u001B[33m"); | |
| break; | |
| case 3: | |
| System.out.print("\u001B[31m"); | |
| break; | |
| default: | |
| break; | |
| } | |
| System.out.printf("%2d ", i); | |
| System.out.print("\u001B[0m"); | |
| if (((i + d) % 7 == 0) || (i == days[M])) System.out.println(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.innoying; | |
| import java.sql.Connection; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.sql.Statement; | |
| /** | |
| * | |
| * @author innoying | |
| */ | |
| class Course { | |
| //The course id | |
| int id = 0; | |
| //Create a new class with id | |
| public Course( int course_id ){ | |
| //Setup the id | |
| id = course_id; | |
| } | |
| //Create a new class with info | |
| public Course( String name, int maxSize, int teacher_id) throws SQLException { | |
| //Prepared Statement | |
| String prepSelect = "INSERT INTO `Course` (`id`,`Name`,`Teacher_id`,`Class_Size`) VALUES (NULL,?,?,?);"; | |
| //Create a new statement for checking the login | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect, Statement.RETURN_GENERATED_KEYS); | |
| //Set the variables | |
| stmt.setString(1, name); | |
| stmt.setInt(2, teacher_id); | |
| stmt.setInt(3, maxSize); | |
| //Get results | |
| stmt.executeUpdate(); | |
| //Setup the id for this class instance | |
| ResultSet results = stmt.getGeneratedKeys(); | |
| //Get next line | |
| results.next(); | |
| //Get the id | |
| id = results.getByte(1); | |
| } | |
| //Get the class name | |
| public String getName() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Name from `Course` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the name | |
| return results.getString(1); | |
| } | |
| //Get the class maxsize | |
| public int getMaxSize() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT maxSize from `Course` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the maxSize | |
| return results.getInt(1); | |
| } | |
| //Get the teacher id | |
| public int getTeacherId() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Teacher_id from `Course` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the teacher_id | |
| return results.getInt(1); | |
| } | |
| //Get the course id | |
| public int getId() throws SQLException{ | |
| //Return the course id | |
| return id; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.innoying; | |
| import java.sql.*; | |
| import java.util.Scanner; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| public class Main { | |
| //Get a util instace | |
| static Util util = new Util(); | |
| //Create a UI instance | |
| static UI ui = new UI(); | |
| //Create a public connection for use in other classes | |
| public static Connection con; | |
| static Scanner input; | |
| public static void main(String[] args) throws ClassNotFoundException, InterruptedException { | |
| //The global scanner object | |
| input = new Scanner(System.in); | |
| //Load the jdbc sql drivers | |
| Class.forName("com.mysql.jdbc.Driver"); | |
| //Setup URL for mysql server | |
| String url = "jdbc:mysql://mc.innoying.com:3306/GroundGrades"; | |
| //Make sure to catch a SQLException | |
| try { | |
| //Open a connection to the sql server | |
| con = DriverManager.getConnection( url, "GroundGrades", "QSJpMw2mj8yMsNbJ" ); | |
| //Keep running the application forever | |
| while(true){ | |
| //Prompt for username and password | |
| System.out.print("Username: "); | |
| String username = input.nextLine(); | |
| System.out.print("Password: "); | |
| String password = input.nextLine(); | |
| //If the login was a teacher | |
| Boolean teacher = false; | |
| //Attempt a teacher login | |
| int status = util.login( true, con, username, password ); | |
| //If a teacher login failed, try a student | |
| if(status == -1){ | |
| //Try student | |
| status = util.login( false, con, username, password); | |
| }else{ | |
| //The teacher login was successful | |
| teacher = true; | |
| } | |
| //If login successful | |
| if(status != -1){ | |
| if(teacher){ | |
| //Teacher | |
| teacherUI(status); | |
| }else{ | |
| //Student | |
| StudentUI(status); | |
| } | |
| }else{ | |
| //Login failed, loop and let them try again | |
| util.error("Invalid username/password!"); | |
| } | |
| } | |
| } catch (SQLException ex) { | |
| //Log the sql error | |
| Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); | |
| } | |
| } | |
| private static void teacherUI(int id) throws SQLException, InterruptedException{ | |
| //int to hold the users selection | |
| int selection = -1; | |
| //While the slection is not to quit, loop | |
| while(selection != 0){ | |
| //Get the user selection | |
| selection = util.waitForSelection(new String[]{"Logout", "Add Course", "Add Assignment/Assignment Grades", "Add Attendance"}); | |
| //Make the courses list global because multiple cases use it | |
| Course course; | |
| switch(selection){ | |
| case 3: | |
| //Select a course | |
| course = ui.selectCourse(id); | |
| //Take the attendance | |
| ui.takeAttendance(course); | |
| break; | |
| case 2: | |
| course = ui.selectCourse(id); | |
| int sub_option = -1; | |
| while(sub_option != 0){ | |
| //Get the user's selection | |
| sub_option = util.waitForSelection(new String[]{"Back to Main Menu", "Add an Assignment", "Add a Assignment Grade for a Student"}); | |
| switch(sub_option){ | |
| case 2: | |
| //Select an assignment | |
| Assignment assign = ui.selectAssignment(course); | |
| //Take the grades for the assignment | |
| ui.takeGrade(assign, course); | |
| break; | |
| case 1: | |
| //Create a new assignment | |
| ui.createAssignment(course); | |
| break; | |
| case 0: | |
| default: | |
| //Tell user we are returning | |
| System.out.println("Returning..."); | |
| break; | |
| } | |
| } | |
| break; | |
| case 1: | |
| //Create a new course | |
| ui.createCourse(id); | |
| break; | |
| case 0: | |
| default: | |
| //Tell user we are logging out | |
| System.out.println("Logging out..."); | |
| break; | |
| } | |
| } | |
| } | |
| private static void StudentUI(int id) throws SQLException { | |
| //Get an instance of the student | |
| Student student = new Student(id); | |
| //int to hold the users selection | |
| int selection = -1; | |
| //While the slection is not to quit, loop | |
| while(selection != 0){ | |
| //Get the user selection | |
| selection = util.waitForSelection(new String[]{"Logout", "Enroll in a class", "Show Grades", "Show Attendance"}); | |
| //Switch for each option | |
| switch(selection){ | |
| case 3: | |
| //Select a class first | |
| Course attendcourse = ui.selectCourse(student); | |
| ui.printCal(attendcourse, student); | |
| break; | |
| case 2: | |
| //Select a class first | |
| Course course = ui.selectCourse(student); | |
| //Show header | |
| System.out.println( "Student: " + student.getName() + " - GPA: " + student.getGPA() + " - Class: " + course.getName()); | |
| //Print the students grades | |
| ui.printGrades(student, course); | |
| break; | |
| case 1: | |
| //Enroll a student | |
| ui.enrollInCourse(student); | |
| break; | |
| case 0: | |
| default: | |
| //Tell user we are logging out | |
| System.out.println("Logging Out..."); | |
| break; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.innoying; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.sql.Statement; | |
| import java.util.ArrayList; | |
| /** | |
| * | |
| * @author innoying | |
| */ | |
| class Student { | |
| //The course id | |
| int id = 0; | |
| Util util; | |
| //Create a new class with id | |
| public Student(int student_id){ | |
| //Setup the id | |
| id = student_id; | |
| util = new Util(); | |
| } | |
| //Get the student Name | |
| public String getName() throws SQLException{ | |
| //Prepared Statement | |
| String prepSelect = "SELECT Name from `Student` WHERE `id` = '"+id+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the string | |
| return results.getString(1); | |
| } | |
| //Get the course id | |
| public int getId() throws SQLException{ | |
| //Return the course id | |
| return id; | |
| } | |
| public ArrayList<Course> getCourses() throws SQLException{ | |
| //Get courses for this student | |
| return util.getCourses(this); | |
| } | |
| public void addScore(Boolean update, Assignment assign, int score) throws SQLException{ | |
| String prepSelect; | |
| if(update){ | |
| //Prepared Statement | |
| prepSelect = "UPDATE `Assignment_Student` SET `Score`=? WHERE `Assignment_id`=? AND `Student_id`=?;"; | |
| }else{ | |
| //Prepared Statement | |
| prepSelect = "INSERT INTO `Assignment_Student` (`Score`,`Assignment_id`,`Student_id`) VALUES (?,?,?);"; | |
| } | |
| //Create a new statement for checking the login | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect); | |
| //Set the variables | |
| stmt.setInt(2, assign.getId()); | |
| stmt.setInt(3, id); | |
| stmt.setInt(1, score); | |
| //Get results | |
| stmt.executeUpdate(); | |
| } | |
| public int getScore(Assignment assign){ | |
| try { | |
| //Prepared Statement | |
| String prepSelect = "SELECT Score from `Assignment_Student` WHERE `Student_id` = '"+id+"' AND `Assignment_id` = '"+assign.getId()+"'"; | |
| //Statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Execute the select | |
| ResultSet results = stmt.executeQuery(prepSelect); | |
| //Select the row | |
| results.next(); | |
| //Return the string | |
| return results.getInt(1); | |
| } catch (SQLException ex) { | |
| //If not defined yet, it's a -1 (Don't count) | |
| return -1; | |
| } | |
| } | |
| //Enroll this student in a course | |
| public boolean enroll(Course course){ | |
| //Catch SQL Insert error | |
| try { | |
| String prepSelect; | |
| //Prepared Statement | |
| prepSelect = "INSERT INTO `Enrollment` (`Student_id`,`Course_id`) VALUES (?,?);"; | |
| //Create a new statement for checking the login | |
| PreparedStatement stmt = Main.con.prepareStatement(prepSelect); | |
| //Set the variables | |
| stmt.setInt(1, id); | |
| stmt.setInt(2, course.getId()); | |
| //Get results | |
| stmt.executeUpdate(); | |
| //Success! | |
| return true; | |
| } catch (SQLException ex) { | |
| //The enrollment already exists | |
| return false; | |
| } | |
| } | |
| public double getGPA() throws SQLException { | |
| //Get a list of all courses | |
| ArrayList<Course> courses = this.getCourses(); | |
| //For each max score | |
| int maxScore = 0; | |
| //For each score | |
| int score = 0; | |
| //For each course | |
| for( int j = 0; j < courses.size(); j++ ){ | |
| //Get a list of assignments | |
| ArrayList<Assignment> assignments = util.getAssignments(courses.get(j)); | |
| //For each assignment | |
| for( int i = 0; i < assignments.size(); i++ ){ | |
| //Add to max Score | |
| maxScore += assignments.get(i).getMaxScore(); | |
| //Add to total score | |
| score += getScore(assignments.get(i)); | |
| } | |
| } | |
| //Calculate the GPA | |
| int result = 4*Math.round(((float)score)/((float)maxScore)); | |
| //Return the result | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.innoying; | |
| import java.sql.Date; | |
| import java.sql.SQLException; | |
| import java.util.ArrayList; | |
| import java.util.InputMismatchException; | |
| import java.util.Scanner; | |
| class UI { | |
| //Global Scanner | |
| Scanner input; | |
| //Get the util | |
| Util util; | |
| //Setup the UI | |
| public UI(){ | |
| //Setup the Scanner | |
| input = new Scanner(System.in); | |
| //Setup the util instance | |
| util = new Util(); | |
| } | |
| public void enrollInCourse(Student student) throws SQLException{ | |
| //Define outside of loop so it's accessable | |
| Course course = null; | |
| //Boolean to decide if loop is done yet | |
| Boolean done = false; | |
| //While not done | |
| while(done != true){ | |
| try{ | |
| //Prompt | |
| String encodedKey = (String) util.prompt("String", "Class Enrollment Key"); | |
| //Decrypt the enrollment key | |
| int key = util.decodeId(encodedKey); | |
| //Create a new course instance with the given key | |
| course = new Course(key); | |
| //Try to run a course command to verify valid key | |
| course.getName(); | |
| done = true; | |
| }catch(SQLException e){ | |
| //No key matches | |
| done = false; | |
| util.error("Invalid Key!"); | |
| } | |
| } | |
| //Enroll the student | |
| if( student.enroll( course ) ){ | |
| //Success | |
| System.out.println("Successfully enrolled!"); | |
| }else{ | |
| //Failure | |
| util.error("You are already enrolled in this course!"); | |
| } | |
| } | |
| public Course selectCourse(int teacher_id) throws SQLException{ | |
| //Get a list of courses | |
| ArrayList<Course> courses = util.getCourses(teacher_id); | |
| //Start coursenum at -1 | |
| int coursenum = -1; | |
| while(coursenum <= 0 || coursenum > courses.size()){ | |
| //Print the number of courses | |
| System.out.println( courses.size() + " Courses:" ); | |
| //Allow the user to select | |
| for(int i=0; i<courses.size(); i++){ | |
| //Print the course key and name | |
| System.out.println((i+1)+".) \""+util.encodeId(courses.get(i).getId())+"\":\""+courses.get(i).getName()+"\""); | |
| } | |
| //Prompt | |
| System.out.print("Select a class: "); | |
| try{ | |
| //Get the coursenumber | |
| coursenum = input.nextInt(); | |
| }catch(InputMismatchException e){ | |
| util.error("Invalid selection"); | |
| } | |
| //Catch trailing newline | |
| input.nextLine(); | |
| } | |
| //Return the course | |
| return courses.get(coursenum-1); | |
| } | |
| public Assignment selectAssignment(Course course) throws SQLException{ | |
| int assignnum = -1; | |
| //Get all the assignments | |
| ArrayList<Assignment> assigns = util.getAssignments(course); | |
| //While invalid assignment selected | |
| while(assignnum <= 0 || assignnum > assigns.size()){ | |
| //Print num of assignments | |
| System.out.println("Found "+assigns.size()+" Assignments for the class \""+course.getName()+"\""); | |
| //Loop each assignment | |
| for(int i=0; i< assigns.size(); i++){ | |
| System.out.println((i+1)+".) \""+assigns.get(i).getName()+"\""); | |
| } | |
| System.out.print("Select an Assignment: "); | |
| //Read new line | |
| assignnum = input.nextInt(); | |
| //Catch newline | |
| input.nextLine(); | |
| } | |
| //Get the assignment | |
| return assigns.get(assignnum -1); | |
| } | |
| public void takeAttendance(Course course) throws SQLException{ | |
| System.out.println("1 - Present"); | |
| System.out.println("2 - Tardy"); | |
| System.out.println("3 - Absent"); | |
| //Get a list of students | |
| ArrayList<Student> students = util.getStudents(course); | |
| //For each student | |
| for(int i=0; i<students.size();i++){ | |
| boolean done = true; | |
| int status = 0; | |
| while(done){ | |
| //Print the student's name | |
| System.out.print(students.get(i).getName()+": "); | |
| status = input.nextInt(); | |
| input.nextLine(); | |
| //Status is valid | |
| if(status > 0 && status < 4){ | |
| //Move on | |
| done = false; | |
| } | |
| } | |
| //Get todays date with util Date | |
| java.util.Date temp = new java.util.Date(); | |
| //Create a new sql Date from today's date | |
| Date date = new Date(temp.getTime()); | |
| try{ | |
| //Create a new attendance record | |
| new Attendance(students.get(i).getId(), course.getId(), status, date); | |
| }catch(SQLException e){ | |
| util.error("Attendance already taken for today for that student!"); | |
| } | |
| } | |
| } | |
| public void takeGrade(Assignment assign, Course course) throws SQLException{ | |
| //Get a list of students | |
| ArrayList<Student> students = util.getStudents(course); | |
| //For each student | |
| for(int i=0; i<students.size();i++){ | |
| boolean success = true; | |
| while(success){ | |
| //Print the student's name | |
| System.out.print(students.get(i).getName()); | |
| //Temp store the score | |
| int score_temp = students.get(i).getScore(assign); | |
| //If a score was found, show it | |
| if(score_temp != -1){ | |
| System.out.print(" ("+score_temp+"): "); | |
| }else{ | |
| System.out.print(": "); | |
| } | |
| //Read the input | |
| String score = input.nextLine(); | |
| //If a score was provided | |
| if(!"\n".equals(score)){ | |
| //If larger than 150%; | |
| if(Integer.parseInt(score) > (assign.getMaxScore()*1.5)){ | |
| util.error("Score was more than the 150% of the max score for this assignment!"); | |
| }else{ | |
| //Add the score | |
| students.get(i).addScore(score_temp != -1, assign, Integer.parseInt(score)); | |
| //Move on | |
| success = false; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| public void createAssignment(Course course) throws SQLException { | |
| //Prompt | |
| System.out.print("Assignment Name: "); | |
| String name = input.nextLine(); | |
| System.out.print("Max Possible Score: "); | |
| int maxScore = input.nextInt(); | |
| input.nextLine(); | |
| //Create the assigment | |
| new Assignment(name, maxScore, course.getId()); | |
| //Status | |
| System.out.println("Assignment Created Successfully!"); | |
| } | |
| public void createCourse(int id) throws SQLException { | |
| //Prompt for class info | |
| System.out.print("Enter a name for the course: "); | |
| String name = input.nextLine(); | |
| System.out.print("Enter max number of students for the class: "); | |
| int maxSize = input.nextInt(); | |
| //Wait for enter after int | |
| input.nextLine(); | |
| //Create the new course | |
| Course temp = new Course(name, maxSize, id); | |
| //Print the class id | |
| System.out.println("A class has been created with the enrollment key of \""+util.encodeId(temp.getId())+"\""); | |
| } | |
| public Course selectCourse(Student student) throws SQLException { | |
| //Get a list of courses | |
| ArrayList<Course> courses = util.getCourses(student); | |
| //Start coursenum at -1 | |
| int coursenum = -1; | |
| while(coursenum <= 0 || coursenum > courses.size()){ | |
| //Print the number of courses | |
| System.out.println( courses.size() + " Courses:" ); | |
| //Allow the user to select | |
| for(int i=0; i<courses.size(); i++){ | |
| //Print the course key and name | |
| System.out.println((i+1)+".) \""+util.encodeId(courses.get(i).getId())+"\":\""+courses.get(i).getName()+"\""); | |
| } | |
| //Prompt | |
| System.out.print("Select a class: "); | |
| try{ | |
| //Get the coursenumber | |
| coursenum = input.nextInt(); | |
| }catch(InputMismatchException e){ | |
| util.error("Invalid selection"); | |
| } | |
| //Catch trailing newline | |
| input.nextLine(); | |
| } | |
| //Return the course | |
| return courses.get(coursenum-1); | |
| } | |
| public void printGrades(Student student, Course course) throws SQLException { | |
| //Get a list of assignments | |
| ArrayList<Assignment> assignments = util.getAssignments(course); | |
| //For each assignment | |
| for( int i = 0; i < assignments.size(); i++ ){ | |
| int percent = Math.round(100*((float)student.getScore(assignments.get(i))/(float)assignments.get(i).getMaxScore())); | |
| System.out.println("| " + assignments.get(i).getName() + "\t | " | |
| + student.getScore(assignments.get(i)) + "/" | |
| + assignments.get(i).getMaxScore() + "\t | " | |
| + percent + "%\t | " + | |
| util.getLetter(percent) + "\t |"); | |
| } | |
| } | |
| public void printCal(Course attendcourse, Student student) throws SQLException { | |
| //Get totday's date | |
| java.util.Date date = new java.util.Date(); | |
| //For each school month | |
| for( int m = 9; m <= 21; m++ ){ | |
| //Get the year and month | |
| int year = date.getYear(); | |
| int M = m; | |
| //If looped into new year | |
| if(m > 12){ | |
| year += 1; | |
| M -= 12; | |
| } | |
| //Create a new calendar | |
| new Calendar(student, attendcourse, M, year); | |
| //New line for spacing | |
| System.out.println(""); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.innoying; | |
| import java.sql.*; | |
| import java.util.ArrayList; | |
| import java.util.Scanner; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| /** | |
| * | |
| * @author innoying | |
| */ | |
| public class Util { | |
| //Global Scanner | |
| static Scanner input = new Scanner(System.in); | |
| //Check if a login is valid | |
| public int login(Boolean teacher, Connection con, String username, String password) { | |
| //Catch a SQL Exception and return false | |
| try { | |
| //Prepared Statement | |
| String prepSelect = "SELECT id, COUNT(*) FROM `" + ( teacher ? "Teacher" : "Student" ) + "` WHERE `Username` = ? and `Password` = ?"; | |
| //Create a new statement for checking the login | |
| PreparedStatement stmt = con.prepareStatement(prepSelect); | |
| //Load in the username and password fixing bad chars | |
| stmt.setString(1, username); | |
| stmt.setString(2, password); | |
| //Get the results | |
| ResultSet results = stmt.executeQuery(); | |
| //Select the count row | |
| results.next(); | |
| //If student not found | |
| if(results.getInt(2) != 1){ | |
| //No user found | |
| return -1; | |
| }else{ | |
| //Found student user | |
| return results.getInt(1); | |
| } | |
| } catch (SQLException ex) { | |
| //Print the error | |
| Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); | |
| //Return bad login | |
| return -1; | |
| } | |
| } | |
| public String encodeId(int id) { | |
| //Seed string | |
| String seed = "ABCDEFGHIJK"; | |
| //Result String | |
| String result = ""; | |
| //Create a array from the data | |
| char[] ids = (id+"").toCharArray(); | |
| //Loop for each char of id | |
| for( int i = 0; i < ids.length; i++ ){ | |
| //Create a temp id | |
| char temp = ids[i]; | |
| //Add the char at the id | |
| result += seed.charAt(Integer.parseInt(temp+"")); | |
| } | |
| //Return the result | |
| return result; | |
| } | |
| public int decodeId(String string){ | |
| //Seed string | |
| String seed = "ABCDEFGHIJK"; | |
| //Final Result | |
| int finalresult = 0; | |
| //For each char | |
| for( int i = 0; i < string.length(); i++ ){ | |
| //Add result to the string | |
| finalresult += (int) (Math.pow(10, string.length()-1-i) * seed.indexOf(string.toUpperCase().charAt(i))); | |
| } | |
| //Return the final id | |
| return finalresult; | |
| } | |
| public ArrayList<Course> getCourses(int teacher_id) throws SQLException{ | |
| //Create a ArrayList | |
| ArrayList<Course> list = new ArrayList<Course>(); | |
| //Create a statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Get the courses | |
| ResultSet results = stmt.executeQuery("SELECT id FROM `Course` WHERE `Teacher_id` = '"+teacher_id+"'"); | |
| //While there are more courses | |
| while(results.next()){ | |
| //Generate a new course with the given id | |
| list.add(new Course(results.getInt(1))); | |
| } | |
| //Return the courses | |
| return list; | |
| } | |
| public ArrayList<Assignment> getAssignments(Course course) throws SQLException{ | |
| //Create a ArrayList | |
| ArrayList<Assignment> list = new ArrayList<Assignment>(); | |
| //Create a statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Get the courses | |
| ResultSet results = stmt.executeQuery("SELECT id FROM `Assignment` WHERE `Course_id` = '"+course.getId()+"'"); | |
| //While there are more courses | |
| while(results.next()){ | |
| //Generate a new course with the given id | |
| list.add(new Assignment(results.getInt(1))); | |
| } | |
| //Return the assignments | |
| return list; | |
| } | |
| public ArrayList<Student> getStudents(Course course) throws SQLException{ | |
| //Create a ArrayList | |
| ArrayList<Student> list = new ArrayList<Student>(); | |
| //Create a statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Get the courses | |
| ResultSet results = stmt.executeQuery("SELECT Student_id FROM `Enrollment` WHERE `Course_id` = '"+course.getId()+"'"); | |
| //While there are more courses | |
| while(results.next()){ | |
| //Generate a new course with the given id | |
| list.add(new Student(results.getInt(1))); | |
| } | |
| //Return the students | |
| return list; | |
| } | |
| public ArrayList<Course> getCourses(Student student) throws SQLException{ | |
| //Create a ArrayList | |
| ArrayList<Course> list = new ArrayList<Course>(); | |
| //Create a statement | |
| Statement stmt = Main.con.createStatement(); | |
| //Get the courses | |
| ResultSet results = stmt.executeQuery("SELECT Course_id FROM `Enrollment` WHERE `Student_id` = '"+student.getId()+"'"); | |
| //While there are more courses | |
| while(results.next()){ | |
| //Generate a new course with the given id | |
| list.add(new Course(results.getInt(1))); | |
| } | |
| //Return the courses | |
| return list; | |
| } | |
| //Generate/Print a list of options | |
| public void generateList(String[] options){ | |
| //For each option | |
| for( int i = 0; i < options.length; i++ ){ | |
| //Print that option in "i.) option" format | |
| System.out.println( ( i + 1 ) + ".) " + options[ i ] ); | |
| } | |
| } | |
| //Wait for a selection from the options array | |
| public int waitForSelection(String[] options){ | |
| //The option selected | |
| int selection = -1337; | |
| //While the selction is invalid | |
| while(selection < 1 || selection > options.length){ | |
| //If selection is not first time | |
| if(selection != -1337){ | |
| //Tell user they made an error | |
| System.err.println("Invalid Selection!"); | |
| } | |
| //Print the options | |
| generateList(options); | |
| //Prompt | |
| System.out.print("Please make a selection: "); | |
| //Get the user selection | |
| selection = input.nextInt(); | |
| //Catch the trailing newline and throw it away | |
| input.nextLine(); | |
| } | |
| //Return the selection made adjusting for array indexs | |
| return selection - 1; | |
| } | |
| //Prompt the user for input | |
| public Object prompt(String type, String promptText){ | |
| //Print the prompt text | |
| System.out.print( promptText + ": " ); | |
| //If the type is String | |
| if( type.equals( "String" ) ){ | |
| //Return the next inputted text | |
| return input.nextLine(); | |
| }else if( type.equals( "int" ) ){ | |
| //Get the int from the user | |
| int temp = input.nextInt(); | |
| //Catch the trailing newline | |
| input.nextLine(); | |
| //Return the temp int | |
| return temp; | |
| }else{ | |
| //Invalid type, just return null | |
| return null; | |
| } | |
| } | |
| public void error(String string){ | |
| //Print the error | |
| System.err.println(string); | |
| //Try catch for interupted exception | |
| try { | |
| //Wait to reduce odd terminal issues | |
| Thread.sleep(500); | |
| } catch (InterruptedException ex) { } | |
| } | |
| //Get a letter grade for a percent | |
| public String getLetter(int result) { | |
| if(result >= 97){ | |
| return ("A+"); | |
| }else if(result < 97 && result >= 93){ | |
| return ("A"); | |
| }else if(result < 93 && result >= 90){ | |
| return ("A-"); | |
| }else if(result < 90 && result >= 87){ | |
| return ("B+"); | |
| }else if(result < 87 && result >= 83){ | |
| return ("B"); | |
| }else if(result < 83 && result >= 80){ | |
| return ("B-"); | |
| }else if(result < 80 && result >= 77){ | |
| return ("C+"); | |
| }else if(result < 77 && result >= 73){ | |
| return ("C"); | |
| }else if(result < 73 && result >= 70){ | |
| return ("C-"); | |
| }else if(result < 70 && result >= 67){ | |
| return ("D+"); | |
| }else if(result < 67 && result >= 63){ | |
| return ("D"); | |
| }else if(result < 63 && result >= 60){ | |
| return ("D-"); | |
| }else{ | |
| return ("F"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment