import java.util.Scanner; /** * @author Steven Fernández Espinoza B72905 * */ public class Solution { private Scanner input = null; private Tree tree = null; private boolean running = true; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String[] args) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { tree = new Tree(); input = new Scanner(System.in); int choice = 0; while (running) { while (choice != 1 || choice != 2 || choice != 3) { // Ask what the user wants to do with the tree choice = askChoice(); } } } public int askChoice() { int choice = printIndications(); switch (choice) { case 1: tree.reset(); return 1; case 2: addNumber( newNumber() ); return 2; case 3: tree.printFarthest(); return 3; case 4: System.exit(0); return 4; default: return 4; } } public int printIndications() { int indication = 0; System.out.println("Choose what to do with the tree: "); System.out.printf("%s%n%s%n%s%n%s%n", "1= Reset tree", "2= Add element" , "3= Print farthest leaf", "4= Exit"); indication = newNumber(); while ( !validIndication(indication) ) { System.out.println("Invalid choice"); indication = newNumber(); } return indication; } public int newNumber() { int number = 0; try { number = input.nextInt(); } catch (java.util.InputMismatchException e) { System.out.println("Invalid input"); input.nextLine(); } return number; } public boolean validIndication(int indication) { switch (indication) { case 1: return true; case 2: return true; case 3: return true; case 4: return true; default: return false; } } public void addNumber(int value) { tree.insert(value); System.out.println(); } }