Question: Hello experts please help modifying my code , I'm creating a JAVA Quiz Maker program for a class. I'm close to being finished with it

Hello experts please help modifying my code,

I'm creating a JAVA Quiz Maker program for a class. I'm close to being finished with it but I can't seem to figure out how to do this task (*when logged in as an instructor*):

Task: Display stats

Display on screen the following stats: the student with the shortest duration, highest grade, the lowest grade and average grade of the class. (Hint: upon a successful completion of each quiz, write the students name, elapsed time and score to a file, use this to extract the stats listed above).

If it helps, my UsersInfo.txt file consist of text formatted like this:

Username Password FirstName LastName Email Role
kingkrule1 fjsdklfjs King Krule kk1@sfsdf.com Instructor
katy1 jdjskfdkfh Katy Perry kp2@sdfdsf.com Student
post1 sdsdfsf Post Malone pm3@sdsf.com Student

Also, one little problem I can't seem to fix is that when making a new user as an instructor, the new information written into the UsersInfo.txt file comes out like this:

pos1sdSTDFDSDfPostMalonepm3@sdsf.comStudent when it's supposed to be seperated by tabs! The password is also supposed to be randomized A-Z and 6 capitalized characters.

Thanks for the help!

This is my code so far:

import java.util.*; import java.io.*; import java.text.*;

public class QuizMaker { final int QUESTION_COUNT = 125; class User { String firstname; String lastname; String username; String password; String email; String role; public String getFirstname() { return firstname; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }; // Reads UsersInfo.txt static List usersmethod() throws IOException { List userList = new ArrayList(); BufferedReader br1 = new BufferedReader(new FileReader("UsersInfo.txt")); String line = ""; while ((line = br1.readLine()) != null) { String[] u = line.split("\t"); User user = new QuizMaker().new User(); user.setUsername(u[0]); user.setPassword(u[1]); user.setFirstname(u[2]); user.setLastname(u[3]); user.setEmail(u[4]); user.setRole(u[5]); userList.add(user); } br1.close(); return userList; } // Reads TestBank.txt static List questionmethod() throws IOException { List questionList = new ArrayList(); BufferedReader br1 = new BufferedReader(new FileReader("TestBank.txt")); String line = ""; while ((line = br1.readLine()) != null) { questionList.add(line); } br1.close(); return questionList; } // Reads Answers.txt static List answersmethod() throws IOException { List answerList = new ArrayList(); BufferedReader br1 = new BufferedReader(new FileReader("Answers.txt")); String line = ""; while ((line = br1.readLine()) != null) { answerList.add(line); } br1.close(); return answerList; } static String method4() { Scanner scan = new Scanner(System.in); System.out.print("Do you want to take the quiz with another user name?" + " If so, enter YES, otherwise enter done. "); String opt = scan.nextLine(); return opt; } public static void main(String[] args) throws IOException { List userList = new ArrayList(); List questionList = new ArrayList(); List answerList = new ArrayList(); User userAcc = null; int score = 0; int[] ranQns = new int[10]; userList = usersmethod(); questionList = questionmethod(); answerList = answersmethod(); Scanner scan = new Scanner(System.in); String filename =""; int attempt = 1; boolean loginSuccess = false; do { System.out.print("Enter Username: "); String uname = scan.nextLine(); for (User user : userList) { if (user.getUsername().equals(uname)) { do { System.out.print("Enter password: "); String password = scan.nextLine(); if (user.getPassword().equals(password)) { loginSuccess = true; System.out.println("Login accepted, welcome " + user.getFirstname() + "."); userAcc = user; break; } else { System.out.println("Invalid password: "); attempt++; } } while (attempt <= 3); } } if (attempt > 3) { System.out.println("3 Failed attempt. Closing application"); System.exit(0); } if (loginSuccess && userAcc.getRole().equals("Student")) { Random rand = new Random(); int cnt = 0; while (cnt <= 9) { boolean found = false; int randNo = rand.nextInt(125); for (int i = 0; i <= cnt; i++) { if (randNo == ranQns[i]) { found = true; break; } } if (!found) { ranQns[cnt] = randNo; cnt++; } } System.out.println("Get Ready to take Quiz. Type Either True/False or T/F "); long startTime = System.currentTimeMillis(); if (loginSuccess) { for (int i = 0; i < ranQns.length; i++) { System.out.println(questionList.get(ranQns[i])); while (true) { System.out.print("Answer: "); String res = scan.nextLine(); if (res.equalsIgnoreCase("true") || res.equalsIgnoreCase("false") || res.equalsIgnoreCase("t") || res.equalsIgnoreCase("f")) { if (res.equalsIgnoreCase("f") || res.equalsIgnoreCase("false")) { res = "FALSE"; } else if (res.equalsIgnoreCase("t") || res.equalsIgnoreCase("true")) { res = "TRUE"; } if (res.equalsIgnoreCase(answerList.get(i).toString())) { score++; } } else { System.out.println("Wrong answer Type Either True/False or T/F"); } } } } long endTime = System.currentTimeMillis(); StringBuilder sb = new StringBuilder(); sb.append("First name: " + userAcc.getFirstname() + " "); sb.append("Last Name: " + userAcc.getLastname() + " "); sb.append("Score: " + score + " "); sb.append("Elapsed Time: " + (endTime - startTime) / 1000 + " seconds" + " "); System.out.println(sb.toString()); Calendar cal = Calendar.getInstance(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = dateFormat.format(cal.getTime()); date = date.replaceAll(":", "."); filename = userAcc.getUsername() + "_COSC_236_Quiz_" + date + ".txt"; BufferedWriter br = new BufferedWriter(new FileWriter(filename)); br.write(sb.toString()); br.flush(); br.close(); String opt = method4(); if (!opt.equalsIgnoreCase("yes")) { break; } } else { //Instructor Menu System.out.println("Welcome Instructor! Please select a number from below: "); Scanner s = new Scanner(System.in); System.out.println("-Menu-"); System.out.println("1 Register a Student"); System.out.println("2 Get Stats"); System.out.println("3 Add New Questions"); System.out.println("4 Exit"); int choice = s.nextInt(); switch(choice) { case 1: { BufferedWriter br = new BufferedWriter(new FileWriter("UsersInfo.txt", true)); StringBuilder sb = new StringBuilder(); String newuser, passw, fname, lname, new_Email, dummy; dummy = s.nextLine(); System.out.println("Add username:"); newuser = s.nextLine(); System.out.println("Add password"); passw = s.nextLine(); System.out.println("Add first name:"); fname = s.nextLine(); System.out.println("Add last name:"); lname = s.nextLine(); System.out.println("Add email:"); new_Email = s.nextLine(); String password = UUID.randomUUID().toString(); password.replace("-", ""); sb.append(newuser).append(passw).append(fname).append(lname).append(new_Email); br.write(sb.toString()); br.flush(); br.close(); break; } case 2: { BufferedReader br1 = new BufferedReader(new FileReader(filename)); String line = ""; while ((line = br1.readLine()) != null) { String[] u = line.split("\t"); for(String item : u) { System.out.print(item + "\t\t"); } } br1.close(); break; } case 3: { BufferedWriter brq = new BufferedWriter(new FileWriter("TestBank.txt", true)); BufferedWriter brans = new BufferedWriter(new FileWriter("Answers.txt", true)); String choic = "y"; while(choic.equals("Y") || choic.equals("y")) { String ques, ans, dummy; dummy = s.nextLine(); System.out.println("Enter a new question: "); ques = s.nextLine(); System.out.println("Enter answer: "); ans = s.nextLine(); brq.write(System.getProperty("line.separator")+ques); brans.write(System.getProperty("line.separator")+ans); System.out.println("Do you want to add more(y/n): "); choic = s.nextLine(); } brans.flush(); brq.flush(); brq.close(); brans.close(); break; } case 4: { System.exit(0); break; } default:System.out.println("Invalid choice. Try again"); } break; } } while (true); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!