Skip to content
Snippets Groups Projects
Commit b54079b2 authored by Simon Barrett's avatar Simon Barrett
Browse files

Refactored Lesson

parent 2b05b9a9
Branches
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -30,9 +30,8 @@ public class Lesson {
}
/* GETTER METHODS
First three are standard getter methods
Last one is a custom method used so that the code reads easier
when we need to get the array length (One method instead of chaining)
Standard getter methods
Getter methods for 3 fields aren't needed at the moment so not added to minimise code
*/
public String getLessonTitle() {
return lessonTitle;
......@@ -46,10 +45,6 @@ public class Lesson {
return quizScore;
}
public int getQuestionArrayLength() {
return questionArray.length;
}
// END GETTER METHODS
/* SETTER METHODS
......@@ -79,14 +74,18 @@ public class Lesson {
// CUSTOM METHODS
/**
* To string method will display
* Returns a string based on if the user has completed the lesson or quiz
* Though the method currently isn't called when lessonCompleted == false, this functionality has been added
* in case it is used differently in the future
* @return
*/
public String toString() {
if(quizCompleted == false) {
public String printOutUserCompletionDataForLesson() {
if(lessonCompleted == false) {
return " has not completed the lesson or quiz for " + lessonTitle;
} else if(quizCompleted == false) {
return " has completed the lesson for " + lessonTitle + " but not the quiz";
} else {
return " has completed the lesson and quiz for " + lessonTitle + ", with a score of " + quizScore + "/" + getQuestionArrayLength();
return " has completed the lesson and quiz for " + lessonTitle + ", with a score of " + quizScore + "/" + questionArray.length;
}
}
......@@ -100,11 +99,13 @@ public class Lesson {
int questionsAnswered = 0;
for(int i = 0; i < questionArray.length; i++) {
// Checks that user got the question correct and adds 1 to score if they do
if(questionArray[i].askQuestion(i + 1)) {
correctAnswers += 1;
}
questionsAnswered += 1;
// Checks to see if it is the last questions and outputs a different string depending on if it is or not.
if (i != questionArray.length - 1) {
System.out.println("Your current Score is " + correctAnswers + "/" + questionsAnswered);
} else {
......
......@@ -23,7 +23,11 @@ public class Question {
* @param questionNumber used to format the question string displayed to the user.
*/
public boolean askQuestion(int questionNumber) {
/* Design decision. This was a choice between using a bool to represent correct or false
or using 1 to represent a correct answer and 0 for a false
decided on boolean so it reduces the use of "Magic Numbers" in the code and its more
resilient to code changes
*/
System.out.println("Question " + questionNumber + ": " + question);
for(int i = 1; i <= answers.length; i++) {
......
......@@ -9,20 +9,10 @@ public class QuizSystem {
MyData myData = new MyData();
boolean continueWithQuiz = true;
UserCompletionData users = new UserCompletionData();
boolean firstTimeThrough = true;
// Start the while loop so quiz repeats itself until user quits
while (continueWithQuiz) {
if(firstTimeThrough == false) {
System.out.println("Type l to see a list of all lessons, or data to see progress :");
String userDecision = ScannerExtension.accessUserData();
if(userDecision.equals("data")) {
System.out.println("Please type in your name to access your data, or all to print all user data");
ScannerExtension.viewUserData(users);
}
}
firstTimeThrough = false;
// List all Lessons
String userName = "";
myData.cmt652.listAll();
......@@ -43,8 +33,8 @@ public class QuizSystem {
String nextString = ScannerExtension.chooseYorN();
if (nextString.equals("y")) {
chosenLesson.setQuizScore(chosenLesson.doQuiz());
chosenLesson.setQuizCompleted(true);
chosenLesson.setQuizScore(chosenLesson.doQuiz());
chosenLesson.setQuizCompleted(true);
System.out.println("Please enter your name to record that you have completed the lesson and the quiz");
userName = ScannerExtension.scanString();
System.out.println("Record of completion and Score saved under username: " + userName);
......@@ -58,7 +48,7 @@ public class QuizSystem {
// Add User progress to UserCompletionData and print out all user progress
int userExists = users.checkIfUserExists(userName);
if(userExists == -1) {
if (userExists == -1) {
User recordCurrentIteration = new User(userName);
recordCurrentIteration.addToUserRecord(chosenLesson);
users.add(recordCurrentIteration);
......@@ -66,13 +56,16 @@ public class QuizSystem {
users.getUsers().get(userExists).addToUserRecord(chosenLesson);
}
// User to decide to finish game or go back to starting menu
System.out.println("Would you like to continue playing or exit the game (y for continue playing, n to exit game)");
String userContinue = ScannerExtension.chooseYorN();
if(userContinue.equals("n")) {
System.out.println("Type l to see a list of all lessons, data to see progress or n to exit game :");
String userDecision = ScannerExtension.gameDecision();
if (userDecision.equals("data")) {
System.out.println("Please type in your name to access your data, or all to print all user data");
ScannerExtension.viewUserData(users);
} else if (userDecision.equals("n")) {
continueWithQuiz = false;
System.out.println("Thank you for playing the MSc software engineering quiz game");
}
}
}
}
}
\ No newline at end of file
......@@ -54,12 +54,12 @@ public class ScannerExtension {
return nextString;
}
public static String accessUserData() {
public static String gameDecision() {
boolean correctEntry = false;
String nextString = scanner.next();
nextString = nextString.toLowerCase();
while (!correctEntry) {
if(nextString.equals("l") || nextString.equals("data")) {
if(nextString.equals("l") || nextString.equals("data") || nextString.equals("n")) {
correctEntry = true;
} else {
System.out.println("Incorrect entry!");
......
......@@ -34,7 +34,9 @@ public class UserCompletionData {
public void printAllUsersData() {
for(int i = 0; i < users.size(); i++) {
User currentUser = users.get(i);
System.out.println(currentUser.getUsername() + currentUser.getUserRecord().toString());
for (int j = 0; j < currentUser.getUserRecord().size(); j++) {
System.out.println(currentUser.getUsername() + currentUser.getUserRecord().get(j).printOutUserCompletionDataForLesson());
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment