import java.util.Scanner;

/**
 * Handles all user interaction to create Boxes, populate the Warehouse, and to
 * ship Boxes.
 *
 * @author Nancy Harris
 * @version 11/24/2012
 */
public class DukesShipping {
    // used for all input
    private Scanner keyboard;

    /**
     * Default constructor that builds the Scanner.
     */
    public DukesShipping() {
        keyboard = new Scanner(System.in);
    }

    /**
     * Runs the application.
     */
    public void start() {
        Warehouse warehouse;
        char choice;
        Box aBox;
        Box shipBox;
        double length;
        double width;
        double depth;

        // create the Warehouse
        warehouse = new Warehouse(10, 10, 10);
        System.out.println(warehouse);

        // while option not exit
        do {
            makeMenu();
            choice = validateChoice();
            if (choice == 's') {
                length = enterDouble("Enter the length: ");
                width = enterDouble("Enter the width: ");
                depth = enterDouble("Enter the depth: ");
                aBox = new Box(length, width, depth);

                // look for a box
                shipBox = warehouse.findBox(aBox);
                if (shipBox != null) {
                    printResult(aBox, shipBox);
                } else {
                    System.out.println("A suitable box cannot be found.");
                }
            }
        } while (choice != 'x');

        System.out.println(warehouse);
    }

    /**
     * Displays the menu choices.
     */
    public void makeMenu() {
        System.out.println();
        System.out.print("Enter your choice of action:\n\ts: Ship\n\tx: Exit");
        System.out.println();
        System.out.print("choice---> ");
    }

    /**
     * Gets the user choice (checks for bad input).
     * 
     * @return a valid user choice
     */
    public char validateChoice() {
        char choice;
        choice = keyboard.next().charAt(0);

        while (choice != 'x' && choice != 's') {
            System.out.println("Invalid choice, choice. Try again. (x or s)");
            System.out.print("choice---> ");
            choice = keyboard.next().charAt(0);
        }

        return choice;
    }

    /**
     * Displays the prompt and makes sure we have a double value.
     *
     * @param prompt The prompt to display
     * @return a double number
     */
    public double enterDouble(String prompt) {
        double size;
        boolean goodValue;

        size = -5;
        goodValue = false;

        while (!goodValue) {
            System.out.print(prompt);
            if (keyboard.hasNextDouble()) {
                size = keyboard.nextDouble();
                if (size >= 0) {
                    goodValue = true;
                }
            } else {
                keyboard.next(); // throw away the bad value
            }
        }
        return size;
    }

    /**
     * Prints the result of finding a box.
     *
     * @param customerBox The box to ship
     * @param shipBox The box to ship customerBox in
     */
    public void printResult(Box customerBox, Box shipBox) {
        System.out.printf("Customer: %s\nShips in: %s\n", customerBox, shipBox);
    }

}
