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

Final Refactor ScannerExtension

parent 426a1d34
Branches
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -3,22 +3,22 @@ import java.util.Scanner;
public class ScannerExtension {
/* Design Decision: Although the Scanner Extension at the end of the file is tightly coupled
with the methods that it is called in, it felt right to have a whole scanner extension file with
each extension in so that future developers know exactly where to go to find the scanner.
/* Design Decision: This extension file has been designed so that all scanner logic is held in one place
and future developers know exactly where to look if there is something wrong with a scanner.
Though the middle two scanners are very program specific they also have a level of reusability and
the method names make it clear what the method achieves.
The middle two scanners are needed to achieve specific goals but have been designed to have a level of
reusability and been given method names to make it clear what goal they achieve.
I have added these Scanner extensions so that the user is unable to crash the program by typing incorrect
information, thus creating a better user experience.
The last scanner is very specific to the method which calls it but despite the cohesion it has been placed
in this file so that all the scanner logic for the program stays together.
These methods are static as I thought it unnecessary to create a new class everytime we need to use one and
they are self-contained methods.
I have added these Scanner extensions so that the user is unable to crash the program by typing incorrect
information, thus creating a better user experience and making the program more resilient.
These Self-Contained methods are static so that you do not need to create a new class everytime we need to use one.
*/
public static Scanner scanner = new Scanner(System.in);
public static Scanner scanner = new Scanner(System.in);
/**
* Takes in a string from the scanner and converts it to lower case
......@@ -30,22 +30,28 @@ public class ScannerExtension {
return nextString;
}
/*
Known Edge Case: For the below method if you type an incorrect int followed by a letter it crashes
the program. This will be fixed in future editions.
*/
/**
* Extends Scanner to include error handling so user can only pick a valid integer
* Extends Scanner to include error handling so that a user can only pick an integer in the given range
* <p>
* Method will keep asking for a user choice until user has picked an integer which is in the
* array which is passed in
* @param totalChoices Use Length of the array that you wish the user to pick from
* @return user choice
* Method will keep asking for a user choice until user has picked a valid integer
* @param totalChoices User can pick a number from 1 - totalChoices (inclusive).
* @return Users integer choice
*/
public static int scanForUserIntChoice(int totalChoices) {
boolean correctEntry = false;
// Prevents a user from entering a non-integer and asks them to pick again
while (scanner.hasNextInt() == false) {
scanner.next();
System.out.println("Incorrect entry! Please choose from one of the options above.");
}
int nextInt = scanner.nextInt();
while (!correctEntry) {
// Scanner will keep looping until the user has entered a valid choice
if(nextInt <= totalChoices && nextInt > 0) {
correctEntry = true;
} else {
......@@ -53,9 +59,11 @@ public class ScannerExtension {
nextInt = scanner.nextInt();
}
}
// Return a valid choice which has been error handled so program won't crash
return nextInt;
}
/**
* User can choose between y or n, this will be converted to lowerCase so Y and N work as well
* <p>
......@@ -65,6 +73,9 @@ public class ScannerExtension {
public static String chooseYorN() {
boolean correctEntry = false;
String nextString = scanner.next();
/* Convert user input to lowercase and check if they have entered y or n,
Loop will continue until user makes one of those two choices
*/
nextString = nextString.toLowerCase();
while (!correctEntry) {
if(nextString.equals("y") || nextString.equals("n")) {
......@@ -79,20 +90,26 @@ public class ScannerExtension {
}
/**
* User can type in all to see all users data or theirs own name to check their data
* <p>
* If the name the user inputs doesn't match any data in the UserCompletionData then user can try another
* name or press x to exit and go back to the main menu
*User can type in all to see all users data or theirs own name to check their data
*<p>
*If the name the user inputs doesn't match any data in the UserCompletionData then user can try another
*name or press x to exit and go back to the main menu
* @param users UserStorage object for user input from scanner to be checked against
*/
public static void viewUserData(UsersStorage users) {
String nextString = scanner.next();
nextString = nextString.toLowerCase();
// Checks if string equals all, if so it prints out all user data
if(nextString.equals("all")) {
users.printAllUsersData();
} else {
int userExists = users.checkIfUserExists(nextString);
/*
If the user didn't type all then the input will be checked to see if the username has data attached to it
Loop will keep going until a correct username is given or the user decides to exit by pressing x.
*/
boolean correctEntry = false;
while (!correctEntry){
int userExists = users.checkIfUserExists(nextString);
if(userExists == -1) {
System.out.println("This user doesn't exist. Please enter another username or 'x' to exit.");
nextString = scanner.next();
......
......@@ -44,7 +44,7 @@ public class User {
lessonsPlaceInRecord = i;
}
}
// Update or create the lesson in the user record
if(inUserRecord) {
userRecord.get(lessonsPlaceInRecord).setQuizScore(currentLesson.getQuizScore());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment