Question: Hi, I have this JAVA assignment for a Quiz Maker: Modify your code so that instructors can also use your program. Change your login method

Hi, I have this JAVA assignment for a "Quiz Maker":

Modify your code so that instructors can also use your program. Change your login method to recognize if the username/password entered belong to a student or instructor (based on the last column in the UsersInfo.txt file). If the user is a student, you will need to go with the regular path (start the quiz). If the user is an instructor, display the following options as a menu:

Register a new student.

Ask the instructor to enter a students information: first name, last name, username, email. The password needs to be generated automatically. All the information will then be added to the Users_Info.txt file.

2) 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).

3) Add new questions

Allow the instructor to add new questions to the test bank. The questions can be added in two ways: 1) manually by typing the question and the answer. 2) by providing a file name to be read and appended to the original test bank.

-------------------------------------------------------------------------------------------------------------------------

The UsersInfo.txt contains info like this (seperated by tab):

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

The TestBank.txt file contains questions seperated by a line break like this:

question1

question2

question3

The Answers.txt file contains the TRUE/FALSE answers designated to the certain questions sequentially:

TRUE

TRUE

FALSE

---------------------------------------------------------------------------------------------------------

My code doesn't run correctly, please help modify it! The problems with my code:

After running the code as an instructor it doesn't give me a text area to enter a new "Username" for the new student.

Also, after entering the new student's info, it comes out as: Student3fc702e1-a209-4235-a78a-f5c09a969c1aJoshKingadad@adsdsdf.com and doesn't skip a line after the previous student info in the UsersInfo.txt file. The password should be 6 characters A-Z and capitalized.

And when adding a new question, the text area to enter the question is also missing. Only the field where I can enter the new answer shows up.

Here 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; } };

static List method1() 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; }

static List method2() 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;

}

static List method3() 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 = method1();

questionList = method2();

answerList = method3();

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 { //Provide a menu System.out.println("Welcome Instructor..Please select an option from below: "); Scanner s = new Scanner(System.in); System.out.println("Menu" + "/n1. Register a Student." + "/n2. Get Stats." + "/n3. Add New Questions" + "/n4. Exit"); int choice = s.nextInt(); switch(choice) { case 1: { BufferedWriter br = new BufferedWriter(new FileWriter("UsersInfo.txt", true)); StringBuilder sb = new StringBuilder(); String newuser, fname, lname, new_Email; newuser = s.nextLine(); fname = s.nextLine(); lname = s.nextLine(); new_Email = s.nextLine(); String password = UUID.randomUUID().toString(); password.replace("-", ""); sb.append(newuser).append(password).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; System.out.println("Enter a new question: "); ques = s.nextLine(); System.out.println("Enter answer: "); ans = s.nextLine(); brq.write(ques); brans.write(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("Wrong 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!