package com.troels.bicycle; import java.lang.reflect.Array; import java.util

  • package com.troels.bicycle;

  •  

  • import java.lang.reflect.Array;

  • import java.util.Arrays;

  • import java.util.Scanner;

  •  

  • public class Main {

  •  

  •     public static String stringInput = “”;

  •     public static int intInput = 0;

  •  

  •     public static void main(String[] args) {

  •         createBicycle();

  •     }

  •  

  •     public static void createBicycle() {

  •         Bicycle bicycle = new Bicycle();

  •         Scanner sc = new Scanner(System.in);

  •  

  •         color(bicycle, sc);

  •         gears(bicycle, sc);

  •         wheelSize(bicycle, sc);

  •         System.out.println(“Your bicycle is ” + bicycle.color + “, has ” + bicycle.gears + ” gears, and has a wheel size of ” + bicycle.wheelSize + “!”);

  •     }

  •  

  •     public static void color(Bicycle bicycle, Scanner sc) {

  •         System.out.println(“Which color should the bicycle be?”);

  •         stringInput = sc.nextLine().toLowerCase();

  •         if (bicycle.isValidColor(stringInput) == true) {

  •             bicycle.color = stringInput;

  •             //System.out.println(“Bicycle is now ” + bicycle.color);

  •         } else {

  •             System.out.println(stringInput + ” is not a valid color! You can choose between these colors: ” + Arrays.asList(bicycle.colors).toString());

  •             color(bicycle, sc);

  •         }

  •     }

  •  

  •     public static void gears(Bicycle bicycle, Scanner sc){

  •         System.out.println(“How many gears should the bicycle have?”);

  •         intInput = sc.nextInt();

  •         if (intInput <= 21 && intInput > 0){

  •             bicycle.gears = intInput;

  •             //System.out.println(“Bicycle now has ” + bicycle.gears + ” gears”);

  •         } else {

  •             System.out.println(“You can only have 0-21 gears!”);

  •             gears(bicycle, sc);

  •         }

  •     }

  •    

  •     public static void wheelSize(Bicycle bicycle, Scanner sc){

  •         System.out.println(“How big should the wheels be? (Diameter in centimetres)”);

  •         intInput = sc.nextInt();

  •         if (intInput <= 100 && intInput > 40){

  •             bicycle.wheelSize = intInput;

  •             //System.out.println(“The wheels are now ” + bicycle.wheelSize + ” cm in diameter”);

  •         } else {

  •             System.out.println(“Wheel size can only be between 40 and 100 cm !”);

  •             wheelSize(bicycle, sc);

  •         }

  •     }

  •    

  • }

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  • package com.troels.bicycle;

  •  

  • import java.util.Arrays;

  •  

  • public class Bicycle {

  •    

  •     public String color = “White”;

  •     public static final String[] colors = {“red”, “green”, “blue”, “white”, “black”, “yellow”, “purple”, “pink”, “orange”};

  •    

  •     public int gears = 21;

  •    

  •     public int wheelSize = 60;

  •    

  •    

  •     public boolean isValidColor(String input){

  •         if(Arrays.asList(colors).contains(input)){

  •             color = input;

  •             return true;

  •         } else {

  •             return false;

  •         }

  •     }

  •    

  •    

  •    

  • }

  • Source Article