Added some comments

This commit is contained in:
2020-07-19 15:47:38 -04:00
parent 3035387aa5
commit 5e0f2a0c8e
2 changed files with 15 additions and 0 deletions

View File

@@ -26,9 +26,11 @@ import java.util.Scanner;
public class Driver{ public class Driver{
//An enum to hold the possible menu selections
private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE}; private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE};
private static final Scanner input = new Scanner(System.in); private static final Scanner input = new Scanner(System.in);
//Drives the program
public static void main(String[] args){ public static void main(String[] args){
SELECTIONS selection = SELECTIONS.SIZE; //Holds the menu selection of the user SELECTIONS selection = SELECTIONS.SIZE; //Holds the menu selection of the user
do{ do{
@@ -47,6 +49,7 @@ public class Driver{
} }
}while(!selection.equals(SELECTIONS.EXIT)); }while(!selection.equals(SELECTIONS.EXIT));
} }
//Print the menu
private static void printMenu(){ private static void printMenu(){
System.out.println("1. Solve a problem"); System.out.println("1. Solve a problem");
System.out.println("2. Print a problem description"); System.out.println("2. Print a problem description");
@@ -55,6 +58,7 @@ public class Driver{
System.out.println("5. Exit"); System.out.println("5. Exit");
System.out.println(); System.out.println();
} }
//Get a menu selection from the user
private static SELECTIONS getMenuSelection(){ private static SELECTIONS getMenuSelection(){
Integer selection = input.nextInt(); Integer selection = input.nextInt();
while(!isValidMenu(selection)){ while(!isValidMenu(selection)){
@@ -64,6 +68,7 @@ public class Driver{
} }
return getSelection(selection); return getSelection(selection);
} }
//Make sure the value passed in is a valid menu option
private static Boolean isValidMenu(Integer selection){ private static Boolean isValidMenu(Integer selection){
//Ordinal + 1 because enum starts at 0 //Ordinal + 1 because enum starts at 0
if((selection > 0) && (selection < (SELECTIONS.SIZE.ordinal() + 1))){ if((selection > 0) && (selection < (SELECTIONS.SIZE.ordinal() + 1))){
@@ -73,6 +78,7 @@ public class Driver{
return false; return false;
} }
} }
//Turns an integer passed to it into a SELECTION enum
private static SELECTIONS getSelection(Integer selection){ private static SELECTIONS getSelection(Integer selection){
SELECTIONS sel = null; SELECTIONS sel = null;
@@ -86,9 +92,11 @@ public class Driver{
} }
return sel; return sel;
} }
//Print an error message
private static void printErrorMessage(){ private static void printErrorMessage(){
System.out.println("That is an invalid selection!"); System.out.println("That is an invalid selection!");
} }
//Handle what happens when a user wants to solve a problem
private static void solveMenu(){ private static void solveMenu(){
Integer problemNumber = ProblemSelection.getProblemNumber(); Integer problemNumber = ProblemSelection.getProblemNumber();
//This selection solves all problems in order //This selection solves all problems in order
@@ -106,6 +114,7 @@ public class Driver{
ProblemSelection.solveProblem(problemNumber); ProblemSelection.solveProblem(problemNumber);
} }
} }
//Handle what happens when a user wants to see the description of a problem
private static void descriptionMenu(){ private static void descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
System.out.println("\n"); System.out.println("\n");

View File

@@ -31,11 +31,13 @@ import mattrixwv.ProjectEuler.Problems.*;
public class ProblemSelection{ public class ProblemSelection{
private static final Scanner input = new Scanner(System.in); private static final Scanner input = new Scanner(System.in);
//Holds the valid problem numbers
public static final ArrayList<Integer> PROBLEM_NUMBERS = new ArrayList<Integer>(Arrays.asList( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, public static final ArrayList<Integer> PROBLEM_NUMBERS = new ArrayList<Integer>(Arrays.asList( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 67)); 31, 67));
//Returns the problem corresponding to the given problem number
public static Problem getProblem(Integer problemNumber){ public static Problem getProblem(Integer problemNumber){
Problem problem = null; Problem problem = null;
switch(problemNumber){ switch(problemNumber){
@@ -74,12 +76,14 @@ public class ProblemSelection{
} }
return problem; return problem;
} }
//Print the description of a problem
public static void printDescription(Integer problemNumber){ public static void printDescription(Integer problemNumber){
//Get the problem //Get the problem
Problem problem = getProblem(problemNumber); Problem problem = getProblem(problemNumber);
//Print the problem's description //Print the problem's description
System.out.println(problem.getDescription()); System.out.println(problem.getDescription());
} }
//Solve a problem
public static void solveProblem(Integer problemNumber){ public static void solveProblem(Integer problemNumber){
//Get the problem //Get the problem
Problem problem = getProblem(problemNumber); Problem problem = getProblem(problemNumber);
@@ -90,6 +94,7 @@ public class ProblemSelection{
//Print the results //Print the results
System.out.println(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n"); System.out.println(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n");
} }
//Get a valid problem number from a user
public static Integer getProblemNumber(){ public static Integer getProblemNumber(){
Integer problemNumber = 0; Integer problemNumber = 0;
System.out.print("Enter a problem number: "); System.out.print("Enter a problem number: ");
@@ -100,6 +105,7 @@ public class ProblemSelection{
} }
return problemNumber; return problemNumber;
} }
//List all valid problem numbers
public static void listProblems(){ public static void listProblems(){
System.out.print(PROBLEM_NUMBERS.get(1)); System.out.print(PROBLEM_NUMBERS.get(1));
for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){ for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){