Second part of the final project...I have done all of the rest. I dont know what
ID: 3838228 • Letter: S
Question
Second part of the final project...I have done all of the rest. I dont know what to do for the second part. Need help! I NEED A MODIFIED CODE THANK YOU!
Modify your code from part 1 so that instructors can 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:
1) Registar a new student.
Ask the instructor to eneter a student's information: first name, last name, username, email. The password needs to be generated automatically. All the information will then be added to the UsersInfor.txt file.
2) Display stats.
Display on screen the follwoing 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 student's name, elapsed time and score to the 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 two ways: 1) manually by typing the question and answer. 2) by providing a file name to be read and appended to the original test bank.
COSC 236
Here is my code for part 1 of the project
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Scanner;
public class QuizMaker {
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public static void main(String[] args) {
String userNamesFilePath="UserInfo.txt";
String testBankFilePath="TestBank.txt";
String testBankAnswersPath="Answers.txt";
BufferedReader br = null;
FileReader fr = null;
HashMap<String,String> uLoginDetails= new HashMap<String,String>();
HashMap<String,String> uFirstName= new HashMap<String,String>();
HashMap<String,String> uLastName= new HashMap<String,String>();
LinkedHashSet<Integer> randomQuestionNumbers = new LinkedHashSet<Integer>();
LinkedHashMap<Integer,String> questions = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> answers = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> userEnteredAnswers = new LinkedHashMap<Integer,String>();
int loginAttempt=0;
String username="";
String password="";
Scanner sc= new Scanner(System.in);
int score=0;
//Get all user details from file for validation
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(userNamesFilePath));
while ((sCurrentLine = br.readLine()) != null) {
String[] splitLine = sCurrentLine.split("\s+");
uLoginDetails.put(splitLine[0], splitLine[1]);
uFirstName.put(splitLine[0], splitLine[2]);
uLastName.put(splitLine[0], splitLine[3]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
while(loginAttempt<4)
{
System.out.println("Enter User Name");
username=sc.nextLine();
System.out.println("Enter Password");
password=sc.nextLine();
if(uLoginDetails.containsKey(username))
{
String pwd=uLoginDetails.get(username);
if(pwd.equals(password))
{
break;
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
if(loginAttempt<3)
{
//Generate 10 random numbers in range of 1 to 125
while(randomQuestionNumbers.size()<10)
{
randomQuestionNumbers.add(getRandomNumberInRange(1, 125));
}
//Get 10 questions from file based on random numbers
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankFilePath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
questions.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//Get 10 Answers from file based on random numbers
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankAnswersPath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
answers.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
long startTime = System.currentTimeMillis();
for(int q:randomQuestionNumbers)
{
int validFormat=0;
System.out.println(questions.get(q));
while(validFormat<1)
{
System.out.println("Enter answer as either T or F");
String answer=sc.nextLine();
if(answer.contentEquals("T") || answer.contentEquals("F") || answer.contentEquals("t") || answer.contentEquals("f"))
{
String ans=answers.get(q);
if(answer.equalsIgnoreCase(ans))
{
score=score+1;
}
userEnteredAnswers.put(q,answer);
validFormat=1;
}
}
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Result of the Test");
System.out.println("First Name "+uFirstName.get(username));
System.out.println("Last Name "+uLastName.get(username));
System.out.println("Score "+score);
System.out.println("Elapsed Time in milli seconds"+totalTime);
System.out.println("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
System.out.println(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
//DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//Date date = new Date();
//dateFormat.format(date);
String opFileName="C:/xampp/chg/"+username+"_COSC_QUIZ_"+new SimpleDateFormat("yyyyMMdd_HHmm'.txt'").format(new Date());;
//File file=new File(opFileName);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(opFileName))) {
bw.write("First Name "+uFirstName.get(username));
bw.write("Last Name "+uLastName.get(username));
bw.write("Score "+score);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
bw.write(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
// no need to close it.
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Scanner;
public class QuizMaker {
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public static void main(String[] args) {
String userNamesFilePath="UserInfo.txt";
String testBankFilePath="TestBank.txt";
String testBankAnswersPath="Answers.txt";
BufferedReader br = null;
FileReader fr = null;
HashMap<String,String> uLoginDetails= new HashMap<String,String>();
HashMap<String,String> uFirstName= new HashMap<String,String>();
HashMap<String,String> uLastName= new HashMap<String,String>();
LinkedHashSet<Integer> randomQuestionNumbers = new LinkedHashSet<Integer>();
LinkedHashMap<Integer,String> questions = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> answers = new LinkedHashMap<Integer,String>();
LinkedHashMap<Integer,String> userEnteredAnswers = new LinkedHashMap<Integer,String>();
int loginAttempt=0;
String username="";
String password="";
Scanner sc= new Scanner(System.in);
int score=0;
//Get all user details from file for validation
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(userNamesFilePath));
while ((sCurrentLine = br.readLine()) != null) {
String[] splitLine = sCurrentLine.split("\s+");
uLoginDetails.put(splitLine[0], splitLine[1]);
uFirstName.put(splitLine[0], splitLine[2]);
uLastName.put(splitLine[0], splitLine[3]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
while(loginAttempt<4)
{
System.out.println("Enter User Name");
username=sc.nextLine();
System.out.println("Enter Password");
password=sc.nextLine();
if(uLoginDetails.containsKey(username))
{
String pwd=uLoginDetails.get(username);
if(pwd.equals(password))
{
break;
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
else
{
System.out.println("Incorrect Credentials, please try again");
loginAttempt=loginAttempt+1;
}
}
if(loginAttempt<3)
{
while(randomQuestionNumbers.size()<10)
{
randomQuestionNumbers.add(getRandomNumberInRange(1, 125));
}
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankFilePath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
questions.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
String sCurrentLine;
int LineCount=0;
br = new BufferedReader(new FileReader(testBankAnswersPath));
while ((sCurrentLine = br.readLine()) != null) {
LineCount=LineCount+1;
if(randomQuestionNumbers.contains(LineCount))
{
answers.put(LineCount,sCurrentLine);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
long startTime = System.currentTimeMillis();
for(int q:randomQuestionNumbers)
{
int validFormat=0;
System.out.println(questions.get(q));
while(validFormat<1)
{
System.out.println("Enter answer as either T or F");
String answer=sc.nextLine();
if(answer.contentEquals("T") || answer.contentEquals("F") || answer.contentEquals("t") || answer.contentEquals("f"))
{
String ans=answers.get(q);
if(answer.equalsIgnoreCase(ans))
{
score=score+1;
}
userEnteredAnswers.put(q,answer);
validFormat=1;
}
}
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Result of the Test");
System.out.println("First Name "+uFirstName.get(username));
System.out.println("Last Name "+uLastName.get(username));
System.out.println("Score "+score);
System.out.println("Elapsed Time in milli seconds"+totalTime);
System.out.println("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
System.out.println(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
String opFileName="C:/xampp/chg/"+username+"_COSC_QUIZ_"+new SimpleDateFormat("yyyyMMdd_HHmm'.txt'").format(new Date());;
try (BufferedWriter bw = new BufferedWriter(new FileWriter(opFileName))) {
bw.write("First Name "+uFirstName.get(username));
bw.write("Last Name "+uLastName.get(username));
bw.write("Score "+score);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("Elapsed Time in milli seconds"+totalTime);
bw.write("User Answer"+"-"+"Correct Answer");
for(int q:randomQuestionNumbers)
{
bw.write(userEnteredAnswers.get(q)+"-"+answers.get(q));
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.