Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. /* This is the driving engine of the program. It parses the command-line
  2. * arguments and calls the appropriate methods in the other classes.
  3. *
  4. * You should edit this file in two ways:
  5. * 1) Insert your database username and password in the proper places.
  6. * 2) Implement the three functions getInformation, registerStudent
  7. * and unregisterStudent.
  8. */
  9. import java.sql.*; // JDBC stuff.
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.Properties;
  13. import java.util.Scanner;
  14. import java.io.*; // Reading user input.
  15.  
  16. public class StudentPortal
  17. {
  18. /* TODO Here you should put your database name, username and password */
  19. static final String DATABASE = "jdbc:postgresql://ate.ita.chalmers.se/";
  20. static final String USERNAME = "tda357_028";
  21. static final String PASSWORD = "u5ZiHkPv";
  22.  
  23. /* Print command usage.
  24. * /!\ you don't need to change this function! */
  25. public static void usage () {
  26. System.out.println("Usage:");
  27. System.out.println(" i[nformation]");
  28. System.out.println(" r[egister] <course>");
  29. System.out.println(" u[nregister] <course>");
  30. System.out.println(" q[uit]");
  31. }
  32.  
  33. /* main: parses the input commands.
  34. * /!\ You don't need to change this function! */
  35. public static void main(String[] args) throws Exception
  36. {
  37. try {
  38. Class.forName("org.postgresql.Driver");
  39. String url = DATABASE;
  40. Properties props = new Properties();
  41. props.setProperty("user",USERNAME);
  42. props.setProperty("password",PASSWORD);
  43. Connection conn = DriverManager.getConnection(url, props);
  44.  
  45. String student = args[0]; // This is the identifier for the student.
  46.  
  47. Console console = System.console();
  48. // In Eclipse. System.console() returns null due to a bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429)
  49. // In that case, use the following line instead:
  50. // BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
  51. usage();
  52. System.out.println("Welcome!");
  53. while(true) {
  54. System.out.print("? > ");
  55. String mode = console.readLine();
  56. String[] cmd = mode.split(" +");
  57. cmd[0] = cmd[0].toLowerCase();
  58. if ("information".startsWith(cmd[0]) && cmd.length == 1) {
  59. /* Information mode */
  60. getInformation(conn, student);
  61. } else if ("register".startsWith(cmd[0]) && cmd.length == 2) {
  62. /* Register student mode */
  63. registerStudent(conn, student, cmd[1]);
  64. } else if ("unregister".startsWith(cmd[0]) && cmd.length == 2) {
  65. /* Unregister student mode */
  66. unregisterStudent(conn, student, cmd[1]);
  67. } else if ("quit".startsWith(cmd[0])) {
  68. break;
  69. } else usage();
  70. }
  71. System.out.println("Goodbye!");
  72. conn.close();
  73. } catch (SQLException e) {
  74. System.err.println(e);
  75. System.exit(2);
  76. }
  77. }
  78.  
  79. /* Given a student identification number, ths function should print
  80. * - the name of the student, the students national identification number
  81. * and their issued login name (something similar to a CID)
  82. * - the programme and branch (if any) that the student is following.
  83. * - the courses that the student has read, along with the grade.
  84. * - the courses that the student is registered to. (queue position if the student is waiting for the course)
  85. * - the number of mandatory courses that the student has yet to read.
  86. * - whether or not the student fulfills the requirements for graduation
  87. */
  88. static void getInformation(Connection conn, String student) throws SQLException
  89. {
  90. PreparedStatement pstmt;
  91. System.out.println("Creating statement...");
  92.  
  93.  
  94. String ssn =null;
  95. String name=null;
  96. String login=null;
  97.  
  98. String program=null;
  99. String branch=null;
  100.  
  101. String readCourses=null;
  102. String grade=null;
  103. LinkedList<String> regCourses = new LinkedList<String>();
  104. LinkedList<String> waitingCourses = new LinkedList<String>();
  105. LinkedList<String> gradeCourses = new LinkedList<String>();
  106. int noOfMandCoursesLeft = 100;
  107. String canGrad=null;
  108.  
  109. System.out.println("1");
  110.  
  111. pstmt = conn.prepareStatement("SELECT * FROM Student WHERE ssn = ? ");
  112. ResultSet rs;
  113. pstmt.setString(1,student);
  114. rs = pstmt.executeQuery();
  115. while(rs.next()) {
  116.  
  117. //Retrieve by column name
  118. ssn = rs.getString("ssn");
  119. name = rs.getString("name");
  120. login = rs.getString("login");
  121. }
  122. rs.close();
  123. pstmt.close();
  124.  
  125. PreparedStatement pstmt2 = conn.prepareStatement("SELECT * FROM BelongsTo WHERE student = ? ");
  126.  
  127. pstmt2.setString(1,student);
  128. rs = pstmt2.executeQuery();
  129. while(rs.next()) {
  130. //Retrieve by column name
  131. branch = rs.getString("branch");
  132. program = rs.getString("program");
  133. }
  134.  
  135. rs.close();
  136. pstmt2.close();
  137.  
  138. PreparedStatement pstmt3 = conn.prepareStatement("SELECT * FROM Registrations WHERE student = ? AND status = ? ");
  139.  
  140. String statusString = "registered";
  141. pstmt3.setString(1,student);
  142. pstmt3.setString(2,statusString);
  143. rs = pstmt3.executeQuery();
  144. while(rs.next()) {
  145. //Retrieve by column name
  146. regCourses.add(rs.getString("course"));
  147. }
  148.  
  149. rs.close();
  150. pstmt3.close();
  151.  
  152. Iterator registered =regCourses.iterator();
  153.  
  154. PreparedStatement pstmt4 = conn.prepareStatement("SELECT * FROM CourseQueuePositions WHERE student = ? ");
  155.  
  156. pstmt4.setString(1,student);
  157. rs = pstmt4.executeQuery();
  158. while(rs.next()) {
  159. //Retrieve by column name
  160. waitingCourses.add(rs.getString("course") + " Position:" + rs.getString("place"));
  161. }
  162.  
  163. rs.close();
  164. pstmt4.close();
  165.  
  166. Iterator waitingIt =waitingCourses.iterator();
  167.  
  168. PreparedStatement pstmt5= conn.prepareStatement("SELECT * FROM FinishedCourses WHERE student = ? ");
  169.  
  170. pstmt5.setString(1,student);
  171. rs = pstmt5.executeQuery();
  172. while(rs.next()) {
  173. //Retrieve by column name
  174. gradeCourses.add(rs.getString("course") + " Grade:" + rs.getString("grade"));
  175. }
  176.  
  177. rs.close();
  178. pstmt5.close();
  179.  
  180. Iterator gradeIt =gradeCourses.iterator();
  181.  
  182. //Display values
  183. System.out.println("SSN: " + ssn);
  184. System.out.println("Name: " + name);
  185. System.out.println("Login: " + login);
  186. System.out.println("Branch: " + branch);
  187. System.out.println("Program: " + program);
  188. System.out.println("Registered Courses: ");
  189. while(registered.hasNext()){
  190.  
  191. System.out.println("--" +regCourses.poll());
  192. }
  193.  
  194. System.out.println("Waiting Courses: ");
  195. while(waitingIt.hasNext()){
  196.  
  197. System.out.println("----" +waitingCourses.poll());
  198. }
  199. System.out.println("Taken Courses: ");
  200. while(gradeIt.hasNext()){
  201.  
  202. System.out.println("------" +gradeCourses.poll());
  203. }
  204.  
  205. }
  206.  
  207. /* Register: Given a student id number and a course code, this function
  208. * should try to register the student for that course.
  209. */
  210. static void registerStudent(Connection conn, String student, String course)
  211. throws SQLException
  212. {
  213.  
  214. // TODO: Your implementation here
  215. }
  216.  
  217. /* Unregister: Given a student id number and a course code, this function
  218. * should unregister the student from that course.
  219. */
  220. static void unregisterStudent(Connection conn, String student, String course)
  221. throws SQLException
  222. {
  223. // TODO: Your implementation here
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement