From 5e0f2a0c8e6567fbc6c4b982b20d8b1ec4c5b1d4 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 19 Jul 2020 15:47:38 -0400 Subject: [PATCH] Added some comments --- src/main/java/mattrixwv/ProjectEuler/Driver.java | 9 +++++++++ .../java/mattrixwv/ProjectEuler/ProblemSelection.java | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/mattrixwv/ProjectEuler/Driver.java b/src/main/java/mattrixwv/ProjectEuler/Driver.java index 47691b4..38231a0 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Driver.java +++ b/src/main/java/mattrixwv/ProjectEuler/Driver.java @@ -26,9 +26,11 @@ import java.util.Scanner; public class Driver{ + //An enum to hold the possible menu selections private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, BENCHMARK, EXIT, SIZE}; private static final Scanner input = new Scanner(System.in); + //Drives the program public static void main(String[] args){ SELECTIONS selection = SELECTIONS.SIZE; //Holds the menu selection of the user do{ @@ -47,6 +49,7 @@ public class Driver{ } }while(!selection.equals(SELECTIONS.EXIT)); } + //Print the menu private static void printMenu(){ System.out.println("1. Solve a problem"); System.out.println("2. Print a problem description"); @@ -55,6 +58,7 @@ public class Driver{ System.out.println("5. Exit"); System.out.println(); } + //Get a menu selection from the user private static SELECTIONS getMenuSelection(){ Integer selection = input.nextInt(); while(!isValidMenu(selection)){ @@ -64,6 +68,7 @@ public class Driver{ } return getSelection(selection); } + //Make sure the value passed in is a valid menu option private static Boolean isValidMenu(Integer selection){ //Ordinal + 1 because enum starts at 0 if((selection > 0) && (selection < (SELECTIONS.SIZE.ordinal() + 1))){ @@ -73,6 +78,7 @@ public class Driver{ return false; } } + //Turns an integer passed to it into a SELECTION enum private static SELECTIONS getSelection(Integer selection){ SELECTIONS sel = null; @@ -86,9 +92,11 @@ public class Driver{ } return sel; } + //Print an error message private static void printErrorMessage(){ System.out.println("That is an invalid selection!"); } + //Handle what happens when a user wants to solve a problem private static void solveMenu(){ Integer problemNumber = ProblemSelection.getProblemNumber(); //This selection solves all problems in order @@ -106,6 +114,7 @@ public class Driver{ ProblemSelection.solveProblem(problemNumber); } } + //Handle what happens when a user wants to see the description of a problem private static void descriptionMenu(){ //Give some extra space to print the description System.out.println("\n"); diff --git a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java index c5ac570..a2ade36 100644 --- a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java +++ b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java @@ -31,11 +31,13 @@ import mattrixwv.ProjectEuler.Problems.*; public class ProblemSelection{ private static final Scanner input = new Scanner(System.in); + //Holds the valid problem numbers public static final ArrayList PROBLEM_NUMBERS = new ArrayList(Arrays.asList( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 67)); + //Returns the problem corresponding to the given problem number public static Problem getProblem(Integer problemNumber){ Problem problem = null; switch(problemNumber){ @@ -74,12 +76,14 @@ public class ProblemSelection{ } return problem; } + //Print the description of a problem public static void printDescription(Integer problemNumber){ //Get the problem Problem problem = getProblem(problemNumber); //Print the problem's description System.out.println(problem.getDescription()); } + //Solve a problem public static void solveProblem(Integer problemNumber){ //Get the problem Problem problem = getProblem(problemNumber); @@ -90,6 +94,7 @@ public class ProblemSelection{ //Print the results 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(){ Integer problemNumber = 0; System.out.print("Enter a problem number: "); @@ -100,6 +105,7 @@ public class ProblemSelection{ } return problemNumber; } + //List all valid problem numbers public static void listProblems(){ System.out.print(PROBLEM_NUMBERS.get(1)); for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){