
import java.util.Scanner;

/**
 * This program calculates the area of a rectangle, using the Rectangle class.
 * Based on week02/RectangleArea
 *
 * @author Mark Young (A00000000)
 */
public class RectangleArea {

    public static final Scanner KBD = new Scanner(System.in);

    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.
     *
     * @return a new Rectangle object with dimensions entered by the user
     */
    private static Rectangle getRectangle() {
        double height, width;
        
        height = readDouble("Enter the Rectangle's height: ");
        width = readDouble("Enter the Rectangle's width: ");

        return new Rectangle(height, width);
    }

    /**
     * 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;

        // 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.
     *
     * @param rect the Rectangle to report on
     */
    private static void reportResults(Rectangle rect) {
        System.out.println("The area of a " 
                + rect
                + " is " + rect.getArea() + ".");
    }

    /**
     * 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() {
        System.out.print("\nPress enter...");
        KBD.nextLine();
        System.out.println();
    }

}

