Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package onlinequiz;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- //interface def{
- //BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
- // String intro="-----------Welcome to Amrita Online Exam Services----------\n";
- // String ask= "Press 1 for Student\nPress 2 for Faculty\n:";
- // String fac= "Please Enter your staff username:";
- // String fac1="Please Enter your staff password:";
- //String stu="Please Enter your Username:";
- //String stu1="Please Enter your Password:";
- // String ask1="Press 1 to create new User\nPress 2 to Specify the number of Questions\n:";
- //String ask2="Enter the username of the new user:";
- //String ask3="Enter the password:";
- //String log="Successfully Logged in";
- //String attend="Press 1 to Attend your Exame\nPress 2 to View your Marks\n:";
- //String ho="Login id and Password created for the user :)";
- // String opt="Want to use the app again \npress 1\nPress 2:";
- //String qe="Specify the number of question:";
- //}
- class NoOfQuestion{
- static int n=0;
- static void changeN(int n){
- NoOfQuestion.n = n;
- }
- static int mark=0;
- }
- class Query {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- public Query() throws Exception{
- con = DriverManager.getConnection("jdbc:postgresql://localhost/postgres","postgres", "asdf");
- st = con.createStatement();
- }
- public void answer(String sql) throws SQLException{
- try {
- st.executeQuery(sql);
- } catch (SQLException ex) {
- Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
- }
- con.close();
- }
- }
- class Staff {
- private String user;
- private String pass;
- Query q;
- NoOfQuestion obj = new NoOfQuestion();
- public Staff() throws IOException{
- System.out.println("Please Enter your staff username:");
- user = b.readLine();
- System.out.println("Please Enter your staff password:");
- pass = b.readLine();
- }
- boolean check() throws Exception{
- this.q = new Query();
- String re = "Select * from faculty where username=? and password=?;";
- PreparedStatement pst = q.con.prepareStatement(re);
- pst.setString(1,user);
- pst.setString(2,pass);
- ResultSet rs = pst.executeQuery();
- if(rs.next() && rs.getString(1).equals(user) && rs.getString(2).equals(pass)) {
- return true;
- }
- else{
- System.out.println("Sorry :) Wrong Username and password");
- return false;
- }
- }
- void createUser() throws IOException, Exception{
- String luser,lpass;
- this.q=new Query();
- String a="Insert into student values(?,?)";
- PreparedStatement pst = q.con.prepareStatement(a);
- System.out.println("Enter the username of the new user:");
- luser=b.readLine();
- System.out.println("Enter the password:");
- lpass=b.readLine();
- pst.setString(1,luser);
- pst.setString(2,lpass);
- int check=pst.executeUpdate();
- if(check == 1){
- System.out.println("Login id and Password created for the user :)");
- }
- else{
- System.out.println("Sorry");
- }
- }
- void addNoOfQuestion() throws IOException{
- System.out.println("Specify the number of question:");
- int jk = Integer.parseInt(b.readLine());
- NoOfQuestion.changeN(jk);
- }
- }
- class Student {
- private String user;
- private String pass;
- int marks=0;
- Query q;
- NoOfQuestion obj = new NoOfQuestion();
- public Student() throws IOException{
- System.out.println("Please Enter your student username:");
- user=b.readLine();
- System.out.println("Please Enter your student password:");
- pass = b.readLine();
- }
- boolean check() throws Exception{
- this.q = new Query();
- String re = "Select * from student where username=? and password=?;";
- PreparedStatement pst = q.con.prepareStatement(re);
- pst.setString(1,user);
- pst.setString(2,pass);
- ResultSet rs = pst.executeQuery();
- if(rs.next() && rs.getString(1).equals(user) && rs.getString(2).equals(pass)) {
- return true;
- }
- else{
- System.out.println("Sorry :( Wrong Username and password\n Contact Admin");
- return false;
- }
- }
- void exam() throws IOException, SQLException{
- System.out.print("Press 1 to Attend your Exame\nPress 2 to View your Marks\n:");
- int op=Integer.parseInt(b.readLine());
- if(op == 1){
- if (NoOfQuestion.n == 0){
- System.out.println("Sreekumar didn't upload the question please wait:)");
- }
- else{
- System.out.print("Amrita Online Exam");
- System.out.println("__________________\n");
- BufferedReader reader = new BufferedReader(new FileReader("/home/praneeth/Desktop/Questions.txt"));
- List<String> lines = new ArrayList<String>();
- String line = reader.readLine();
- while( line != null ) {
- lines.add(line);
- line = reader.readLine();
- }
- Random r = new Random();
- int qno;
- String query="select * from answers where question_no=? and ans=?";
- PreparedStatement pst = q.con.prepareStatement(query);
- // Choose a random one from the list
- String x;
- for(int i =0;i<NoOfQuestion.n;i++){
- String randomString = lines.get(r.nextInt(lines.size()));
- String elements[] = randomString.split(",");
- if(elements[0].equals("mu")){
- System.out.println(elements[1]+"\n"+elements[2]+"\n"+elements[3]+"\n"+elements[4]+"\n"+elements[5]+"\n"+elements[6]+"\n");
- qno = Integer.parseInt(elements[7]);
- x=b.readLine();
- pst.setInt(1,qno);
- pst.setString(2,x);
- ResultSet rs = pst.executeQuery();
- if(rs.next()){
- System.out.println("Sreekumar disapointed");
- marks = marks+Integer.parseInt(elements[8]);
- }
- else{
- System.out.println("Sreekumar is happy");
- }
- }
- else if(elements[0].equals("fill")){
- System.out.println(elements[1]+"\n");
- x=b.readLine();
- qno = Integer.parseInt(elements[2]);
- pst.setInt(1,qno);
- pst.setString(2,x);
- ResultSet rs = pst.executeQuery();
- if(rs.next()){
- System.out.println("Sreekumar disapointed");
- marks = marks+Integer.parseInt(elements[3]);
- }
- else{
- System.out.println("Sreekumar is happy");
- }
- }
- NoOfQuestion.mark = marks;
- } }
- }
- else{
- if(NoOfQuestion.mark == 0){
- System.out.println("Babu exam rayamma mundu malla marks gurinchi chuddachule");
- }
- else{
- System.out.println("Marks:"+NoOfQuestion.mark);
- }
- }
- }
- }
- public class OnlineQuiz {
- public static void main(String[] args) throws IOException, Exception {
- int option;
- do{
- System.out.print("-----------Welcome to Amrita Online Exam Services----------\n");
- System.out.print("Press 1 for Student\nPress 2 for Faculty\n:");
- int choice = Integer.parseInt(b.readLine());
- if(choice == 1){
- Student s = new Student();
- if(s.check() == true){
- System.out.println("Successfully Logged in");
- //System.out.println(attend);
- s.exam();
- }
- }
- else if(choice == 2){
- Staff s = new Staff();
- if(s.check()== true){
- int cho;
- System.out.println("log");
- System.out.print("Press 1 to create new User\nPress 2 to Specify the number of Questions\n:");
- cho = Integer.parseInt(b.readLine());
- if(cho==1){
- s.createUser();
- }
- else{
- s.addNoOfQuestion();
- }
- }
- }
- System.out.println("Want to use the app again \npress 1\nPress 2:");
- option = Integer.parseInt(b.readLine());
- }while(option == 1);
- }
- }
Add Comment
Please, Sign In to add comment