import java.util.Scanner;

/**
 * This program calculates the area of a rectangle, using the Rectangle class.
 * It catches exceptions thrown when the user enters invalid dimensions.
 *
 * @author Mark Young (A00000000)
 */
public class RectangleArea {

    public static void main(String[] args) {
        // Create variables for the Rectangle and its area
        Rectangle rect;
        double area;

        // Tell the user what we're doing
        printIntroduction();
        pause();

        // Get the length and width of the rectangle from the user
        rect = getRectangle();
        pause();

        // Report dimensions and area to user
        reportResults(rect);
        pause();
    }

    /**
     * Print the introduction for this program.
     */
    private static void printIntroduction() {
        System.out.println("This program calculates the area of a rectangle.");
    }

    /**
     * Create a Rectangle object using dimensions entered by the user. Complain
     * and return nothing if the object could not be created.
     *
     * @return a new Rectangle object with dimensions entered by the user, or
     * null if the object could not be created
     */
    private static Rectangle getRectangle() {
        double height, width;
        
        height = readDouble("Enter the Rectangle's height: ");
        width = readDouble("Enter the Rectangle's width: ");

        try {
            return new Rectangle(height, width);
        } catch (IllegalArgumentException iae) {
            // illegal dimensions -- complain and return nothing
            System.out.println("\nI couldn't create a " 
                    + height + "x" + width + " rectangle "
                    + "-- " + iae.getMessage() + ".");
            return null;
        }
    }

    /**
     * Prompt for and read an double value from the user.
     *
     * @param prompt the message explaining to the user what to enter
     * @return the value entered by the user in response to the prompt
     */
    private static double readDouble(String prompt) {
        // create a variable to hold the result
        double result;
        Scanner kbd = new Scanner(System.in);

        // prompt the user for input
        System.out.print(prompt);

        // get their answer and tidy the input stream
        result = kbd.nextDouble();
        kbd.nextLine();

        // send their answer back to the caller
        return result;
    }

    /**
     * Report the dimensions and area of a rectangle to the user. Test for no
     * Rectangle being present, because that could happen!
     *
     * @param rect the Rectangle to report on
     */
    private static void reportResults(Rectangle rect) {
        if (rect != null) {
            System.out.println("The area of a " 
                    + rect
                    + " is " + rect.getArea() + ".");
        } else {
            System.out.println("There's no rectangle to calculate "
                    + "the area of!");
        }
    }

    /**
     * Prompt the user and wait for them to press the enter key.
     * NOTE: the input stream must not have a new-line character in it.
     */
    private static void pause() {
        Scanner kbd = new Scanner(System.in);
        System.out.print("\nPress enter...");
        kbd.nextLine();
        System.out.println();
    }

}

