Untitled
unknown
java
a year ago
6.1 kB
8
Indexable
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class QuizSystem { private static final String USERS_FILE_PATH = "users.json"; private static final String QUESTIONS_FILE_PATH = "quiz.json"; @SuppressWarnings("unchecked") public static void main(String[] args) { JSONParser parser = new JSONParser(); Scanner scanner = new Scanner(System.in); List<JSONObject> users = new ArrayList<>(); List<JSONObject> questions = new ArrayList<>(); try { // Load users from JSON file JSONArray usersArray = (JSONArray) parser.parse(new FileReader(USERS_FILE_PATH)); for (Object obj : usersArray) { users.add((JSONObject) obj); } // Load questions from JSON file JSONArray questionsArray = (JSONArray) parser.parse(new FileReader(QUESTIONS_FILE_PATH)); for (Object obj : questionsArray) { questions.add((JSONObject) obj); } } catch (IOException | ParseException e) { e.printStackTrace(); return; } while (true) { System.out.println("System:> Enter your username"); String username = scanner.nextLine(); System.out.println("System:> Enter password"); String password = scanner.nextLine(); JSONObject user = null; for (JSONObject u : users) { if (u.get("username").equals(username) && u.get("password").equals(password)) { user = u; break; } } if (user != null) { System.out.println("System:> Welcome " + user.get("username") + "!"); if (user.get("role").equals("admin")) { System.out.println("System:> Please create new questions in the question bank."); while (true) { JSONObject question = addQuestion(scanner); questions.add(question); saveQuestions(questions); System.out.println("System:> Saved successfully! Do you want to add more questions? (press 's' for start and 'q' for quit)"); String choice = scanner.nextLine(); if (choice.equalsIgnoreCase("q")) { break; } } } else if (user.get("role").equals("student")) { System.out.println("System:> Welcome to the quiz! We will throw you 10 questions. Each MCQ mark is 1 and no negative marking. Are you ready? Press 's' for start."); String choice = scanner.nextLine(); if (choice.equalsIgnoreCase("s")) { generateQuiz(questions, scanner); System.out.println("Would you like to start again? Press 's' for start or 'q' for quit"); choice = scanner.nextLine(); if (choice.equalsIgnoreCase("q")) { break; } } else { System.out.println("Invalid choice. Exiting..."); break; } } } else { System.out.println("Invalid username or password. Please try again."); } } scanner.close(); } @SuppressWarnings("unchecked") private static JSONObject addQuestion(Scanner scanner) { JSONObject question = new JSONObject(); System.out.println("System:> Input your question"); question.put("question", scanner.nextLine()); for (int i = 1; i <= 4; i++) { System.out.println("System:> Input option " + i + ": Admin:>"); question.put("option " + i, scanner.nextLine()); } System.out.println("System:> What is the answer key? Admin:>"); question.put("answerkey", Integer.parseInt(scanner.nextLine())); return question; } private static void saveQuestions(List<JSONObject> questions) { try (FileWriter file = new FileWriter(QUESTIONS_FILE_PATH)) { JSONArray jsonArray = new JSONArray(); jsonArray.addAll(questions); file.write(jsonArray.toJSONString()); file.flush(); } catch (IOException e) { e.printStackTrace(); } } private static void generateQuiz(List<JSONObject> questions, Scanner scanner) { Random random = new Random(); int score = 0; for (int i = 0; i < 10; i++) { JSONObject question = questions.get(random.nextInt(questions.size())); System.out.println("\n[Question " + (i + 1) + "] " + question.get("question")); for (int j = 1; j <= 4; j++) { System.out.println(j + ". " + question.get("option " + j)); } int answer = 0; try { answer = Integer.parseInt(scanner.nextLine()); } catch (NumberFormatException e) { System.out.println("Invalid input! 0 mark awarded for this question."); } if (answer == (long) question.get("answerkey")) { score++; } } System.out.println("\nQuiz complete! Score: " + score + " out of 10."); if (score >= 8) { System.out.println("Excellent!"); } else if (score >= 5) { System.out.println("Good."); } else if (score >= 2) { System.out.println("Very poor!"); } else { System.out.println("Very sorry, you have failed."); } } }
Editor is loading...
Leave a Comment