/** * The Question class stores the questions asked to the user * * @author Reid Roper and Nick Lamb * @1.0 */ import java.io.BufferedReader; import java.io.IOException; import java.lang.NumberFormatException; public class Questions { private String name; private int pegs; private int colors; /** * The Questions constructor initializes the name, pegs, and colors variables. * * @author Reid Roper and Nick Lamb */ Questions() { name = "notset"; //set to notset for debugging pegs = 0; //0 colors = 0; //0 } /** * The enterName method will ask and store the name of the user. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param in this is used to get the info from the user * * @precondition this objects name variable is not set * @postcondition the name variable has been set */ public void enterName(BufferedReader in) { try { System.out.print("What is your name? > "); name = in.readLine(); } catch(IOException exception) { System.out.println("Invalid name type, please try again."); } } /** * The enterPegs method will keep asking the user to input how many pegs they want to * play with. The user is restricted to 1-6 pegs. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param in this is for getting the number of pegs from the user * * @precondition the number of pegs has not been set for the game it is 0 * @postcondition the number of pegs has been set for the game */ public void enterPegs(BufferedReader in) { boolean done = false; while(!done) { try { System.out.print("How many pegs would you like this round? > "); String peg = in.readLine(); pegs = verifyNumberRange(peg,1,6); //check the input if(pegs > 0) //check if pegs got set done = true; //finish up } catch(IOException exception) { System.out.println("Invalid peg type, please try again."); } } } /** * The enterColor method will ask the user for how many colors they want to * play with. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param in for getting the number from the user * * @percondition the number of colors has not been set, it is 0 * @postcondition the number of colors has been set */ public void enterColor(BufferedReader in) { boolean done = false; while(!done) { try { System.out.print("How many colors would you like this round? > "); String color = in.readLine(); colors = verifyNumberRange(color,1,8); if(colors > 0) done = true; } catch(IOException exception) { System.out.println(exception); } } } /** * The method verifyNumberRange will take a string and check if it is a number between two other * number. * * @author Reid Roper and Nick Lamb * @version 1.0 * @param input a string that you need to be checked as a number * @param low an int for the lowbound range * @param high an int for the upperbound range * @return toReturn the final int that has been verified * * */ private int verifyNumberRange(String input, int low, int high) //change to verify range of numbers { boolean running = true; int toReturn = 0; try { int output = Integer.parseInt(input); if(output < low || output > high) throw new InvalidRangeException("Please enter a number between "+low+" and "+high+"."); else toReturn = output; } catch(NumberFormatException exception) { System.out.println("You entered an invalid number, please try again."); running = false; } catch(InvalidRangeException exception) { System.out.println(exception); running = false; } return toReturn; } /** * This method will reset pegs and colors * * @author Reid Roper and Nick Lamb * @version 1.0 * @precondition pegs and colors are both not equal to 0 * @postcondition pegs and colors both set to 0 */ public void reset() { pegs = 0; colors = 0; } /** * This method gets the name variable * * @author Reid Roper and Nick Lamb * @version 1.0 * @return name the name variable */ public String getName() { return this.name; } /** * This method gets the number of pegs * * @author Reid Roper and Nick Lamb * @version 1.0 * @return pegs the number of pegs */ public int getPegs() { return this.pegs; } /** * This method gets the number of colors * * @author Reid Roper and Nick Lamb * @version 1.0 * @return colors the number of colors */ public int getColors() { return this.colors; } }