Create initial classes

This commit is contained in:
2024-12-09 19:10:03 -05:00
parent 31dc122dfa
commit 955310d32b
5 changed files with 225 additions and 2 deletions

View File

@@ -0,0 +1,118 @@
package com.mattrixwv.adventOfCode24;
import java.util.Scanner;
import com.mattrixwv.adventOfCode24.exception.InvalidResult;
public class Driver{
//An enum to hold the possible menu selections
private enum SELECTIONS { SOLVE, DESCRIPTION, LIST, EXIT, SIZE };
private static final Scanner input = new Scanner(System.in);
//Drives the program
public static void main(String[] args){
SELECTIONS selection; //Holds the menu selection of the user
do{
//Print the menu and prompt the user to select an action
printMenu();
selection = getMenuSelection();
try{
switch(selection){
case SOLVE : solveMenu(); break;
case DESCRIPTION : descriptionMenu(); break;
case LIST : ProblemSelector.listProblems();
case EXIT : break;
case SIZE :
default : throw new InvalidResult();
}
}
catch(InvalidResult error){
printErrorMessage();
System.out.println("\n\n\n");
}
}while(!selection.equals(SELECTIONS.EXIT));
}
private static void printMenu(){
System.out.println("1. Solve a problem");
System.out.println("2. Print a problem description");
System.out.println("3. List valid problem numbers");
System.out.println("4. Exit");
System.out.println();
}
private static SELECTIONS getMenuSelection(){
Integer selection = input.nextInt();
while(!isValidMenu(selection)){
System.out.println("That is an invalid option!\nPress Enter to continue");
printMenu();
selection = input.nextInt();
}
return getSelection(selection);
}
private static boolean isValidMenu(Integer selection){
//Ordinal + 1 because enum stats at 0
return ((selection > 0) && (selection < (SELECTIONS.SIZE.ordinal() + 1)));
}
private static SELECTIONS getSelection(Integer selection){
SELECTIONS sel = null;
switch(selection){
case 1 : sel = SELECTIONS.SOLVE; break;
case 2 : sel = SELECTIONS.DESCRIPTION; break;
case 3 : sel = SELECTIONS.LIST; break;
case 4 : sel = SELECTIONS.EXIT; break;
default : sel = SELECTIONS.SIZE;
}
return sel;
}
private static void printErrorMessage(){
System.out.println("That is an invalid selection!");
}
private static void solveMenu() throws InvalidResult{
Integer problemNumber = ProblemSelector.getProblemNumber();
//This selection solved all problems in order
if(problemNumber.equals(0)){
//Solve every valid problem number, skipping over 0
for(Integer problemLocation = 1;problemLocation < ProblemSelector.PROBLEM_NUMBERS.size();++problemLocation){
//Solve the problems
System.out.print(ProblemSelector.PROBLEM_NUMBERS.get(problemLocation).toString() + ". ");
ProblemSelector.solveProblem(ProblemSelector.PROBLEM_NUMBERS.get(problemLocation));
}
}
//This is if a single problem number was chosen
else{
//Solve the problem
ProblemSelector.solveProblem(problemNumber);
}
}
private static void descriptionMenu(){
//Give some extra space to print the description
System.out.println("\n");
//Get the problem number
Integer problemNumber = ProblemSelector.getProblemNumber();
//If the problem number is 0 print out all the descriptions
if(problemNumber.equals(0)){
//Print description for every valid problem number, skipping over 0
for(Integer problemLocation = 1;problemLocation < ProblemSelector.PROBLEM_NUMBERS.size();++problemLocation){
//Print the description
System.out.print(ProblemSelector.PROBLEM_NUMBERS.get(problemLocation) + ". ");
ProblemSelector.printDescription(ProblemSelector.PROBLEM_NUMBERS.get(problemLocation));
System.out.println();
}
}
//Otherwise print out a single problem's description
else{
ProblemSelector.printDescription(problemNumber);
}
}
}

View File

@@ -0,0 +1,75 @@
package com.mattrixwv.adventOfCode24;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.Scanner;
import java.util.StringJoiner;
import com.mattrixwv.adventOfCode24.days.Problem;
public class ProblemSelector{
private static final Scanner input = new Scanner(System.in);
//Holds the valid problem numbers
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0);
private ProblemSelector(){
throw new IllegalStateException("Utility class");
}
//Returns the problem corresponding to the given problem number
public static Problem getProblem(Integer dayNumber){
Problem day = null;
switch(dayNumber){
default: throw new InvalidParameterException();
}
//return day;
}
//Print the description of a problem
public static void printDescription(Integer problemNumber){
//Get the problem
Problem day = getProblem(problemNumber);
//Print the problem's description
System.out.println(day.getDescription());
}
//Solve a problem
public static void solveProblem(Integer problemNumber){
//Get the problem
Problem problem = getProblem(problemNumber);
System.out.println("\n\n\n");
//Print the problem description
System.out.println(problem.getDescription());
//Solve the problem
problem.runSolution();
//Print the results
System.out.println(problem.getResult());
System.out.println("\n\n\n");
}
//Get a valid problem number from a user
public static Integer getProblemNumber(){
Integer problemNumber = 0;
System.out.print("Enter a problem number: ");
problemNumber = input.nextInt();
while(!PROBLEM_NUMBERS.contains(problemNumber)){
System.out.print("That is an invalid problem number!\n Enter a problem number: ");
problemNumber = input.nextInt();
}
return problemNumber;
}
//List all valid problem numbers
public static void listProblems(){
StringJoiner joiner = new StringJoiner(", ");
for(Integer num : PROBLEM_NUMBERS){
joiner.add(num.toString());
}
System.out.println(joiner.toString());
System.out.println();
}
}

View File

@@ -0,0 +1,10 @@
package com.mattrixwv.adventOfCode24.days;
public abstract class Problem{
protected String description;
public String getDescription(){return description;}
public String result;
public String getResult(){return result;}
public abstract String runSolution();
}

View File

@@ -0,0 +1,20 @@
package com.mattrixwv.adventOfCode24.exception;
public class InvalidResult extends Exception{
public InvalidResult(){
super();
}
public InvalidResult(String message){
super(message);
}
public InvalidResult(Throwable cause){
super(cause);
}
public InvalidResult(String message, Throwable cause){
super(message, cause);
}
}