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

Final Refactor Completed

parent 3e9b1972
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
......@@ -85,12 +85,6 @@ public class Lesson {
* Sets lessonCompleted to true as the lesson has now been printed out for user to read
*/
public void printLesson() {
// Styled header so it is easy to see where the lesson begins
System.out.println(" + ------------------------------------------------ +");
System.out.println(" + | LESSON | +");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" ");
// Prints out lesson title and text and sets the lesson completed to true
System.out.println("You have chosen the lesson: " + lessonTitle);
System.out.println(" ");
......
public class QuizSystem {
/*
This class is the heart of the Quiz and is where the main method is run from, it contains the vast majority of
the logic for how the user navigates through the quiz and contains the styling to make the quiz more readable rather
than just having a block of text in the command line. This class both initialises the vast majority of the data and
contains the code logic for it, a discussion of why this decision has been made is written below.
*/
public static void main(String[] args) {
// Welcome message
// Welcome message, shown once to users when quiz starts
System.out.println(" ");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" + | Welcome to the MSc Software Engineering Quiz | +");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" \u00a9 Simon Barrett (C1218123) 2021");
/* This is Basic Set up:
Import the quiz data, set up the User Completion Data Storage, Boolean to check if user wants to continue
*/
// QUIZ AND DATA SET UP
/* Create a boolean which is used to keep the quiz repeating itself and the storage to contain users
completion data */
boolean continueWithQuiz = true;
UsersStorage users = new UsersStorage();
......@@ -26,29 +34,41 @@ public class QuizSystem {
Lesson[] cmt652LessonsArray = {htmlLesson, cssLesson, scrumLesson, dataTypesLesson,oopLesson,keywordsLesson};
Module cmt652 = new Module(cmt652LessonsArray);
// END QUIZ AND DATA SETUP
// Quiz will repeat itself until the user chooses to end it.
// continueWithQuiz can be changed in the method checkUserDecision()
// continueWithQuiz can be changed by the output from checkUserDecision()
while (continueWithQuiz) {
// Value set to false so that main menu loops until user chooses to view the lesson
boolean goToLesson = false;
/*
Design Decision: The code has been broken up into 4 methods as listed below. This is to increase readability,
even someone who's never seen code before should be able to follow the logic. It is also so that when code is
changed in one method it is easy to see if anything breaks. Rather than not having the methods and having all the
code inside the while loop where it could get confusing.
Design Decision: The code has been broken up into 4 methods as listed below the main method. This is to
increase readability, and make the logic easier to follow. It is also so that when code is changed in one
method it is easier to see if and where the code breaks and to maintain a better separation of code. This is
instead of having all the code inside the while loop where it could get confusing.
The methods could also be removed from this Class and put into their own class if the program got larger,
however at this stage I don't think it's necessary. It is enough that they make it more readable.
*/
/*
Design Decision: The user has three options to choose from in the main menu which means that a boolean can't be
used for the user decision, even though this would be the preferred option as it makes the code more resilient and
harder for the next developer to make a mistake.
*/
int userDecision = checkUserDecision(users);
// !!! CHECK THE BELOW NUMBERS AGAINST THE OUTPUTS for checkUserDecision BEFORE CHANGING THEM !!!!
// User choice evaluated (these values have been hard-coded as it is unlikely the menu will change and if it does
// then it will likely be refactored at that time)
// User wants to go to lessons
int userDecision = checkUserDecision();
/* !!! CHECK THE BELOW NUMBERS AGAINST THE OUTPUTS for checkUserDecision BEFORE CHANGING THEM !!!!
User choice evaluated (these values have been hard-coded as it is unlikely the menu will change and if it does
then it will likely be refactored at that time), having "Magic numbers" is a known weakness of this code */
// User chooses to go to the lessons
if (userDecision == 1) {
goToLesson = true;
// User wants to check progress
// User chooses to check progress, once user has checked progress it will loop back to the main menu
} else if (userDecision == 2) {
// Create Progress Banner
System.out.println(" + ------------------------------------------------ +");
......@@ -56,11 +76,13 @@ public class QuizSystem {
System.out.println(" + ------------------------------------------------ +");
System.out.println("Please type in your name to access your data, or (all) to print all user data");
ScannerExtension.viewUserData(users);
// User wants to exit game
// User chooses to exit game
} else if (userDecision == 3) {
continueWithQuiz = false;
}
// If user has chosen to view the lessons then this logic is run
if(goToLesson) {
Lesson chosenLesson = chooseLessonToComplete(cmt652);
......@@ -71,6 +93,7 @@ public class QuizSystem {
}
// Styling shown to user when they exit
System.out.println(" ");
System.out.println(" + ----------------------------------------------------------- +");
System.out.println(" + | Thank you for playing the MSc Software Engineering Quiz | +");
......@@ -78,16 +101,52 @@ public class QuizSystem {
System.out.println(" + ----------------------------------------------------------- +");
}
// 4 MAIN CONTROL FLOW METHODS
/**
* Checks if user wants to continue quiz, see their completion data or see everyone's completion data.
* @return 1 = to go to lessons, 2 = go to data, 3 = exit game
*/
public static int checkUserDecision() {
// Create Main Menu text
System.out.println(" ");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" + | MAIN MENU | +");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" Please pick from one of the options:");
System.out.println(" ");
// Create a list from user to choose from
/*
DO NOT CHANGE THESE NUMBERS WITHOUT CHECKING THE IF/ELSE STATEMENT AFTER THE METHOD IS CALLED
*/
System.out.println("[1] See the list of lessons");
System.out.println("[2] Check your progress");
System.out.println("[3] Exit Game");
System.out.println("By pressing the corresponding number (1, 2, 3):");
// User makes choice
int userDecision = ScannerExtension.scanForUserIntChoice(3);
return userDecision;
}
/**
* Lists all the lessons in the module, user chooses lesson, user is shown lesson
* @param module contains the data needed to run the quiz
* @return Returns a Lesson object
* @return Returns the users Chosen Lesson Object
*/
public static Lesson chooseLessonToComplete(Module module) {
// List all the Lessons in the module, Get the User's lesson choice and Print it out
module.listAll();
// List all the Lessons in the module, User inputs lesson choice and this is saved as a variable
int choice = ScannerExtension.scanForUserIntChoice(module.getLessonsArrayLength());
Lesson chosenLesson = module.getLesson(choice);
// Styled header so it is easy to see where the lesson begins
System.out.println(" + ------------------------------------------------ +");
System.out.println(" + | LESSON | +");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" ");
// Print out users chosen lesson and return the variable
chosenLesson.printLesson();
return chosenLesson;
}
......@@ -99,13 +158,15 @@ public class QuizSystem {
* @return Returns the username which the player used to record their progress
*/
public static String checkIfUserWantsToDoQuiz(Lesson chosenLesson) {
// Ask user if they want to do QUIZ
// Ask user if they want to do the quiz, and check users decision
System.out.println("Would you like to complete the quiz now? Type 'y' (yes) or 'n' (no): ");
// Check user choice
String userName = "";
String nextString = ScannerExtension.chooseYorN();
if (nextString.equals("y")) {
// Decision based on users choice, y = users does the quiz, n = user taken back to main menu
// Username is taken in both cases and a different string is shown based on user decision
if (nextString.equals("y")) {
// User does the quiz. Score and quiz completion is updated on the lesson object
chosenLesson.setQuizScore(chosenLesson.doQuiz());
chosenLesson.setQuizCompleted(true);
System.out.println("Please enter your name (in lowercase) to record that you have completed the lesson and the quiz: ");
......@@ -119,50 +180,28 @@ public class QuizSystem {
System.out.println(" ");
}
System.out.println(" ");
// Output the username so that it can be used to save User completion data
return userName;
}
/**
* Either adds user progress to their data, or adds a new user
* @param users
* @param userName
* @param chosenLesson
* Either adds user progress to their data, or adds a new user to the UserStorage
* @param users UserStorage for the program
* @param userName Username give by user to store their data
* @param chosenLesson Lesson object data you wish to store
*/
public static void addUserProgressToCompletionData(UsersStorage users, String userName, Lesson chosenLesson) {
// Add User progress to UserCompletionData and print out all user progress
// Add User progress to UserStorage
int userExists = users.checkIfUserExists(userName);
// Create a user and add to storage if they don't already exist
if (userExists == -1) {
User recordCurrentIteration = new User(userName);
recordCurrentIteration.addToUserRecord(chosenLesson);
users.add(recordCurrentIteration);
} else {
// Add to existing users data
users.getUsers().get(userExists).addToUserRecord(chosenLesson);
}
}
/**
* Checks if user wants to continue quiz, see their completion data or see everyone's completion data.
** @param users
* @return 1 = to go to lessons, 2 = go to data, 3 = exit game
*/
public static int checkUserDecision(UsersStorage users) {
// Create Main Menu text
System.out.println(" ");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" + | MAIN MENU | +");
System.out.println(" + ------------------------------------------------ +");
System.out.println(" Please pick from one of the options:");
System.out.println(" ");
// Create a list from user to choose from
System.out.println("[1] See the list of lessons");
System.out.println("[2] Check your progress");
System.out.println("[3] Exit Game");
System.out.println("By pressing the corresponding number (1, 2, 3):");
// User makes choice
int userDecision = ScannerExtension.scanForUserIntChoice(3);
return userDecision;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment