Files
ProjectEulerTS/ProblemSelection.ts
2020-10-19 18:13:46 -04:00

90 lines
3.2 KiB
TypeScript

//ProjectEulerTS/ProblemSelection.ts
//Matthew Ellison
// Created: 10-18-20
//Modified: 10-18-20
//This class holds all of the functions needed to handle a problem
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/typescriptClasses
/*
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const readlineSync = require("readline-sync");
import { Problem } from "./Problems/Problem";
import { Problem1 } from "./Problems/Problem1";
import { Problem2 } from "./Problems/Problem2";
import { Problem3 } from "./Problems/Problem3";
export class ProblemSelection{
//Holds the valid problem numbers
public static PROBLEM_NUMBERS: number[] = [1, 2, 3];
//Returns the problem corresponding to the given problem number
public static getProblem(problemNumber: number): Problem{
let problem: Problem = null;
switch(problemNumber){
case 1 : problem = new Problem1(); break;
case 2 : problem = new Problem2(); break;
case 3 : problem = new Problem3(); break;
}
return problem;
}
//Print the description of a problem
public static printDescription(problemNumber: number): void{
//Get the problem
let problem: Problem = ProblemSelection.getProblem(problemNumber);
//Print the problem's description
console.log(problem.getDescription() + "\n\n");
}
//Solve a problem
public static solveProblem(problemNumber: number): void{
//Get the problem
let problem: Problem = ProblemSelection.getProblem(problemNumber);
//Print the problem description
console.log("\n\n" + problem.getDescription());
//Solve the problem
problem.solve();
//Print the results
console.log(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n");
}
//Get a valid problem number from a user
public static getProblemNumber(): number{
let problemNumber: number = 0;
let prob = readlineSync.question("Enter a problem number: ");
try{
problemNumber = parseInt(prob);
}
catch(error){
problemNumber = -1;
}
while(!ProblemSelection.PROBLEM_NUMBERS.includes(problemNumber)){
console.log("That is an invalid problem number!");
problemNumber = ProblemSelection.getProblemNumber();
}
problemNumber = parseInt(prob);
return problemNumber;
}
//List all valid problem numbers
public static listProblems(): void{
console.log(ProblemSelection.PROBLEM_NUMBERS[1]);
for(let problemNumber: number = 2;problemNumber < ProblemSelection.PROBLEM_NUMBERS.length;++problemNumber){
console.log(", " + ProblemSelection.PROBLEM_NUMBERS[problemNumber]);
}
console.log();
}
}