Example #1
0
  public User(Integer userID) throws Exception {
    this.Connect();
    Connection c = this.getConn();
    ResultSet resultset;
    PreparedStatement preparedset = null;

    String sqlOption = "SELECT * FROM users WHERE id=? LIMIT 1";
    preparedset = c.prepareStatement(sqlOption);
    preparedset.setInt(1, userID);

    resultset = preparedset.executeQuery();
    if (resultset.next()) {
      this.setLogin(resultset.getString("login"));
      this.setRole(resultset.getInt("role_id"));
      this.setUserID(resultset.getInt("id"));
    }
  }
  void profedit(String cid) throws Exception {

    try {
      Connection conn = DBConnection.ConnectDB();

      while (true) {

        // System.out.println(cid);
        // list all exercises in this course
        PreparedStatement stmt0 =
            conn.prepareStatement("SELECT eid, hwname FROM exercise WHERE cid = ?");
        stmt0.setString(1, cid);
        ResultSet rs0 = stmt0.executeQuery();
        int i = 1;
        String[] profselect = new String[20];
        int[] alleid = new int[20];
        while (rs0.next()) {
          String hwname = rs0.getString("hwname");
          alleid[i] = rs0.getInt("eid");
          profselect[i] = hwname; // professor's selection is stored
          System.out.printf(" %d. %s\n", i, hwname);
          i++;
        }

        System.out.println(" 0. Back");

        Scanner scan = new Scanner(System.in);
        int Choice = -1;
        // get user choice of which Exercise
        System.out.println("Please enter the corresponding number:");

        while (true) {
          Choice = scan.nextInt();
          if (checkint(Choice, 0, i - 1)) break;
        }

        if (Choice == 0) {
          System.out.print('\u000C');
          break; // go back
        } else {
          // display chosen Exercise
          System.out.print('\u000C');
          String chosename = profselect[Choice];
          System.out.println("You chose to update " + chosename);

          int eid = alleid[Choice];

          PreparedStatement stmt1 = conn.prepareStatement("SELECT * FROM Exercise WHERE eid=?");
          stmt1.setInt(1, eid);
          ResultSet rs1 = stmt1.executeQuery();
          rs1.next();
          String startdate = rs1.getString("startdate");
          String enddate = rs1.getString("enddate");
          int newallowattempt = rs1.getInt("allowattempt");
          int newscorescheme = rs1.getInt("scorescheme");
          int newnumofquestion = rs1.getInt("numofquestion");
          int newcorrectpoint = rs1.getInt("correctpoint");
          int newincorrectpoint = rs1.getInt("incorrectpoint");
          int newbasedon = rs1.getInt("basedon");

          while (true) {
            // display options the professor can modify
            System.out.println(
                "Choose what to update:\n  1. Start date\n  2. End date\n  3. Number of attempts\n  "
                    + "4. Score selection\n  5. Question numbers\n  6. Correct answer points\n  7. Incorrect answer points\n  8. Based on\n  0. Back");
            Choice = -1;
            while (true) {
              Choice = scan.nextInt();
              if (checkint(Choice, 0, 8)) break;
            }

            /*
                	 	1. Start date
            2. End date
            3. Number of attempts
            4. Score selection
            5. Question numbers
            6. Correct answer points
            7. Incorrect answer points
            8. Based on
                	 */

            Scanner scan1 = new Scanner(System.in);

            if (Choice == 0) {
              System.out.print('\u000C');
              break;
            } else if (Choice == 1) {
              System.out.printf(
                  "Origionally Start Date:%s\nPlease enter new Start Date(yyyy-mm-dd):", startdate);
              while (true) {
                startdate = scan1.nextLine();
                if (checkdate(startdate)) break;
              }
              startdate = startdate + " 00:00:00.0";

            } else if (Choice == 2) {
              System.out.printf(
                  "Origionally End Date:%s\nPlease enter new End Date(yyyy-mm-dd):", enddate);
              while (true) {
                enddate = scan1.nextLine();
                if (checkdate(enddate)) break;
              }
              enddate = enddate + " 00:00:00.0";

            } else if (Choice == 3) {
              System.out.printf(
                  "Origionally Number of attempts:%d\nPlease enter new Number of attempts:",
                  newallowattempt);
              newallowattempt = scan1.nextInt();
            } else if (Choice == 4) {
              String[] sele = new String[5];
              sele[1] = "first attempt";
              sele[2] = "last attempt";
              sele[3] = "average";
              sele[4] = "max";
              System.out.printf(
                  "Origionally Score selection:%s\nPlease enter new Score selection(1.first attempt 2.last attempt 3.average 4.max):",
                  sele[newscorescheme]);
              while (true) {
                newscorescheme = scan.nextInt();
                if (checkint(newscorescheme, 1, 4)) {
                  // give professor choice whether to update all the scores of students
                  System.out.println(
                      "Do you want to update all the students' scores? 1. Yes 2. No");
                  int updatechoice = scan.nextInt();
                  if (checkint(updatechoice, 1, 2)) {
                    if (updatechoice == 1) {
                      ProfUpdateScore pupdate = new ProfUpdateScore();
                      pupdate.profupdate(eid, newscorescheme);
                      break;
                    }
                  }
                  break;
                }
              }
            } else if (Choice == 5) {
              System.out.printf(
                  "Origionally Question numbers:%d\nPlease enter new Question numbers:",
                  newnumofquestion);
              newnumofquestion = scan1.nextInt();
            } else if (Choice == 6) {
              System.out.printf(
                  "Origionally Correct answer points:%d\nPlease enter new Correct answer points:",
                  newcorrectpoint);
              newcorrectpoint = scan1.nextInt();
            } else if (Choice == 7) {
              System.out.printf(
                  "Origionally Incorrect answer points:%d\nPlease enter new Incorrect answer points:",
                  newincorrectpoint);
              newincorrectpoint = scan1.nextInt();
            } else if (Choice == 8) {
              String[] basedt = new String[4];
              if (cid.equals("CSC440")) {
                basedt[1] = "Database Fundamentals";
                basedt[2] = "ER Design";
                basedt[3] = "Security and Authorization";
                System.out.printf(
                    "Origionally Based on: %s\nPlease enter new Base on:\n  1. Database Fundamentals\n  2. ER Design\n  3. Security and Authorization\n",
                    basedt[newbasedon]);
              } else {
                basedt[1] = "Binary search trees and Btrees";
                basedt[2] = "Hashing";
                basedt[3] = "Files and indexing and other topics";
                System.out.printf(
                    "Origionally Based on: %s\nPlease enter new Base on:\n  1. Binary search trees and Btrees\n  2.Hashing  3.Files and indexing and other topics\n",
                    basedt[newbasedon]);
              }
              while (true) {
                newbasedon = scan.nextInt();
                if (checkint(newbasedon, 1, 3)) break;
              }
            }

            // System.out.println(enddate);

            String query =
                "UPDATE Exercise SET startdate = timestamp'"
                    + startdate
                    + "', enddate = timestamp'"
                    + enddate
                    + "',"
                    + " correctpoint = "
                    + newcorrectpoint
                    + ",incorrectpoint = "
                    + newincorrectpoint
                    + ", scorescheme = "
                    + newscorescheme
                    + ", "
                    + "allowattempt = "
                    + newallowattempt
                    + ", numofquestion = "
                    + newnumofquestion
                    + ", basedon = "
                    + newbasedon
                    + " WHERE eid = "
                    + eid
                    + " ";

            // edit homework
            Statement stmt3 = conn.createStatement();

            // debug
            // System.out.println("query:"+query);

            if (stmt3.executeUpdate(query) == 1) {
              // System.out.print('\u000C');
              System.out.println("Update Successful!");

            } else {
              // System.out.print('\u000C');
              System.out.println("Update Failure!Please try again.");
            }
          }
        }
      }
      conn.close();

    } catch (Exception e) {
      System.out.println("" + e.getMessage());
    }
  }