Guest User

Untitled

a guest
Nov 13th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.56 KB | None | 0 0
  1. package onlinequiz;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Random;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. //interface def{
  18.     //BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
  19.    // String intro="-----------Welcome to Amrita Online Exam Services----------\n";
  20.    // String ask= "Press 1 for Student\nPress 2 for Faculty\n:";
  21.    // String fac= "Please Enter your staff username:";
  22.    // String fac1="Please Enter your staff password:";
  23.     //String stu="Please Enter your Username:";
  24.     //String stu1="Please Enter your Password:";
  25.    // String ask1="Press 1 to create new User\nPress 2 to Specify the number of Questions\n:";
  26.     //String ask2="Enter the username of the new user:";
  27.     //String ask3="Enter the password:";
  28.     //String log="Successfully Logged in";
  29.     //String attend="Press 1 to Attend your Exame\nPress 2 to View your Marks\n:";
  30.     //String ho="Login id and Password created for the user :)";
  31.    // String opt="Want to use the app again \npress 1\nPress 2:";
  32.     //String qe="Specify the number of question:";
  33. //}
  34. class NoOfQuestion{
  35.     static int n=0;
  36.     static void changeN(int n){
  37.         NoOfQuestion.n = n;
  38.     }
  39.     static int mark=0;
  40. }
  41. class Query {
  42.     Connection con = null;
  43.     Statement st = null;
  44.     ResultSet rs = null;
  45.     public Query() throws Exception{
  46.          con = DriverManager.getConnection("jdbc:postgresql://localhost/postgres","postgres", "asdf");
  47.          st = con.createStatement();
  48.     }
  49.     public void answer(String sql) throws SQLException{
  50.         try {
  51.             st.executeQuery(sql);
  52.         } catch (SQLException ex) {
  53.             Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
  54.         }
  55.        con.close();  
  56.     }
  57. }
  58.  
  59. class Staff {
  60.     private String user;
  61.     private String pass;
  62.     Query q;
  63.     NoOfQuestion obj = new NoOfQuestion();
  64.     public Staff() throws IOException{
  65.         System.out.println("Please Enter your staff username:");
  66.         user = b.readLine();
  67.         System.out.println("Please Enter your staff password:");
  68.         pass = b.readLine();    
  69.     }
  70.     boolean check() throws Exception{
  71.         this.q = new Query();
  72.         String re = "Select * from faculty where username=? and password=?;";
  73.         PreparedStatement pst = q.con.prepareStatement(re);
  74.         pst.setString(1,user);
  75.         pst.setString(2,pass);
  76.         ResultSet rs = pst.executeQuery();
  77.         if(rs.next() && rs.getString(1).equals(user) && rs.getString(2).equals(pass)) {
  78.            return true;
  79.         }
  80.         else{
  81.            System.out.println("Sorry :) Wrong Username and password");  
  82.            return false;
  83.         }
  84.     }
  85.     void createUser() throws IOException, Exception{
  86.         String luser,lpass;
  87.         this.q=new Query();
  88.         String a="Insert into student values(?,?)";
  89.         PreparedStatement pst = q.con.prepareStatement(a);
  90.         System.out.println("Enter the username of the new user:");
  91.         luser=b.readLine();
  92.         System.out.println("Enter the password:");
  93.         lpass=b.readLine();
  94.         pst.setString(1,luser);
  95.         pst.setString(2,lpass);
  96.         int check=pst.executeUpdate();
  97.         if(check == 1){
  98.             System.out.println("Login id and Password created for the user :)");
  99.         }
  100.         else{
  101.             System.out.println("Sorry");
  102.         }
  103.          
  104.     }
  105.     void addNoOfQuestion() throws IOException{
  106.         System.out.println("Specify the number of question:");
  107.         int jk = Integer.parseInt(b.readLine());
  108.         NoOfQuestion.changeN(jk);
  109.     }
  110.    
  111. }
  112. class Student {
  113.     private String user;
  114.     private String pass;
  115.     int marks=0;
  116.     Query q;
  117.     NoOfQuestion obj = new NoOfQuestion();
  118.     public Student() throws IOException{
  119.         System.out.println("Please Enter your student username:");
  120.         user=b.readLine();
  121.         System.out.println("Please Enter your student password:");
  122.         pass = b.readLine();
  123.     }
  124.      boolean check() throws Exception{
  125.         this.q = new Query();
  126.         String re = "Select * from student where username=? and password=?;";
  127.         PreparedStatement pst = q.con.prepareStatement(re);
  128.         pst.setString(1,user);
  129.         pst.setString(2,pass);
  130.         ResultSet rs = pst.executeQuery();
  131.         if(rs.next() && rs.getString(1).equals(user) && rs.getString(2).equals(pass)) {
  132.            return true;
  133.         }
  134.         else{
  135.            System.out.println("Sorry :( Wrong Username and password\n Contact Admin");  
  136.            return false;
  137.         }
  138.     }
  139.     void exam() throws IOException, SQLException{
  140.         System.out.print("Press 1 to Attend your Exame\nPress 2 to View your Marks\n:");
  141.         int op=Integer.parseInt(b.readLine());
  142.         if(op == 1){
  143.            if (NoOfQuestion.n == 0){
  144.                System.out.println("Sreekumar didn't upload the question please wait:)");
  145.            }
  146.            else{
  147.                System.out.print("Amrita Online Exam");
  148.                System.out.println("__________________\n");
  149.                BufferedReader reader = new BufferedReader(new FileReader("/home/praneeth/Desktop/Questions.txt"));
  150.                List<String> lines = new ArrayList<String>();
  151.                 String line = reader.readLine();
  152.                 while( line != null ) {
  153.                     lines.add(line);
  154.                     line = reader.readLine();
  155.                 }
  156.                  Random r = new Random();
  157.                  int qno;
  158.                  String query="select * from answers where question_no=? and ans=?";
  159.                  PreparedStatement pst = q.con.prepareStatement(query);
  160.                  
  161.                 // Choose a random one from the list
  162.                  String x;
  163.                 for(int i =0;i<NoOfQuestion.n;i++){
  164.                     String randomString = lines.get(r.nextInt(lines.size()));
  165.                     String elements[] = randomString.split(",");
  166.                    
  167.                     if(elements[0].equals("mu")){
  168.                         System.out.println(elements[1]+"\n"+elements[2]+"\n"+elements[3]+"\n"+elements[4]+"\n"+elements[5]+"\n"+elements[6]+"\n");
  169.                         qno = Integer.parseInt(elements[7]);
  170.                         x=b.readLine();
  171.                         pst.setInt(1,qno);
  172.                         pst.setString(2,x);
  173.                         ResultSet rs = pst.executeQuery();
  174.                         if(rs.next()){
  175.                             System.out.println("Sreekumar disapointed");
  176.                             marks = marks+Integer.parseInt(elements[8]);
  177.                         }
  178.                         else{
  179.                             System.out.println("Sreekumar is happy");
  180.                         }
  181.                     }
  182.                     else if(elements[0].equals("fill")){
  183.                         System.out.println(elements[1]+"\n");
  184.                         x=b.readLine();
  185.                         qno = Integer.parseInt(elements[2]);
  186.                         pst.setInt(1,qno);
  187.                         pst.setString(2,x);
  188.                         ResultSet rs = pst.executeQuery();
  189.                         if(rs.next()){
  190.                             System.out.println("Sreekumar disapointed");
  191.                             marks = marks+Integer.parseInt(elements[3]);
  192.                         }
  193.                         else{
  194.                             System.out.println("Sreekumar is happy");
  195.                         }
  196.                     }
  197.                     NoOfQuestion.mark = marks;
  198.            }    }
  199.         }
  200.         else{
  201.             if(NoOfQuestion.mark == 0){
  202.                 System.out.println("Babu exam rayamma mundu malla marks gurinchi chuddachule");
  203.             }
  204.             else{
  205.                 System.out.println("Marks:"+NoOfQuestion.mark);
  206.             }
  207.         }
  208.     }
  209.    
  210. }
  211. public class OnlineQuiz {
  212.     public static void main(String[] args) throws IOException, Exception {
  213.         int option;
  214.         do{
  215.             System.out.print("-----------Welcome to Amrita Online Exam Services----------\n");
  216.             System.out.print("Press 1 for Student\nPress 2 for Faculty\n:");
  217.             int choice = Integer.parseInt(b.readLine());
  218.             if(choice == 1){
  219.                Student s = new Student();
  220.                if(s.check() == true){
  221.                    System.out.println("Successfully Logged in");
  222.                    //System.out.println(attend);
  223.                    s.exam();
  224.                    
  225.                }
  226.             }
  227.             else if(choice == 2){
  228.                 Staff s = new Staff();
  229.                 if(s.check()== true){
  230.                    int cho;
  231.                    System.out.println("log");
  232.                    System.out.print("Press 1 to create new User\nPress 2 to Specify the number of Questions\n:");
  233.                    cho = Integer.parseInt(b.readLine());
  234.                    if(cho==1){
  235.                        s.createUser();
  236.                    }
  237.                    else{
  238.                        s.addNoOfQuestion();
  239.                    }
  240.                }
  241.             }
  242.             System.out.println("Want to use the app again \npress 1\nPress 2:");
  243.             option = Integer.parseInt(b.readLine());
  244.            
  245.         }while(option == 1);
  246.     }    
  247. }
Add Comment
Please, Sign In to add comment