import java.util.Scanner;

/**
 * A program to show how objects of the same class can hold different data.
 *
 * @author Mark Young (A00000000)
 */
public class ObjectData {

    public static void main(String[] args) {
        // create some variables
        Scanner kbd = new Scanner(System.in);   // one Scanner
        String firstName = "Mark";  // one String
        String lastName = "Young";  // another String

        // Introduce yourself
        Utilities.printTitle("Object Data Demo");
        Utilities.printParagraph("This program creates String and Scanner "
            + "objects and calls methods on them. "
            + "Each object has different data that it works with, "
            + "and so the results can be different "
            + "even if the method calls are the same.");

        // Use some canned data
        Utilities.printParagraph("For example, "
            + "my first name is " + firstName + ", "
            + "and my last name is " + lastName + ". "
            + "Thus:");
        System.out.println(""
            + "\tfirstName == \"" + firstName + "\".\n"
            + "\tlastName == \"" + lastName + "\".\n");
        Utilities.printParagraph("When I ask firstName what its length is "
            + "(firstname.length()), it returns " + firstName.length() + ". "
            + "But when I ask lastName what its length is "
            + "(lastName.length()), it returns " + lastName.length() + ".");

        // Use some user data
        System.out.print("What is YOUR first name? ");
        firstName = kbd.nextLine();
        System.out.print("And what is your LAST name? ");
        lastName = kbd.nextLine();
        System.out.println();
        Utilities.printParagraph("Repeating that for your name "
            + "(" + firstName + " " + lastName + "), "
            + "firstName.length() returns " + firstName.length() + ", "
            + "and lastName.length() returns " + lastName.length() + ".");

        // pause
        System.out.print("Press Enter...");
        kbd.nextLine();
        System.out.println("\n\n\n\n");

        // Now, Scanners!
        String line = "10 20 30 40";    // but first, yet another String

        Utilities.printParagraph("We can do similar things with Scanners. "
            + "Our usual Scanner is declared like this:");
        System.out.println("\tScanner kbd = new Scanner(System.in);\n");
        Utilities.printParagraph("Thus when we ask kbd for the \"next int\" "
            + "(kbd.nextInt()), "
            + "it gets that int from System.in "
            + "(that is, the keyboard). "
            + "And similarly for next double, next line, and so on.");
        Utilities.printParagraph("But we can make Scanners "
            + "that read from other locations. "
            + "Next term (in 1227) we'll make Scanners that read from files. "
            + "Right now, let's make a Scanner that reads from a String. "
            + "First we need to create the String. "
            + "We'll use the String line == \"" + line + "\", "
            + "which has some integer numerals on it.");

        // pause
        System.out.print("Press Enter...");
        kbd.nextLine();
        System.out.println("\n\n\n\n");

        // create our String scanner
        Scanner str = new Scanner(line);    // another Scanner!

        // report on how it's created and used
        Utilities.printParagraph("The Scanner is created much like kbd is, "
            + "but instead of saying System.in, "
            + "we put the String we want to use. ");
        System.out.println("\tScanner str = new Scanner(line);\n");
        Utilities.printParagraph("By replacing System.in "
            + "with a String variable, "
            + "we are telling the computer "
            + "that we want THIS Scanner "
            + "to get its data from THAT String. "
            + "And so str will NOT read from the keyboard; "
            + "it will read from the String that line contains. "
            + "(Note: it reads from \"" + line + "\", "
            + "because that's the String that line contains "
            + "when the Scanner was created. "
            + "Changing the value of the variable line "
            + "won't change the String the Scanner is reading from!)");
        Utilities.printParagraph("So when we ask str for the next int "
            + "(str.nextInt()), "
            + "it returns " + str.nextInt() + ". "
            + "When we ask for the next int after that "
            + "(str.nextInt() again), "
            + "it returns " + str.nextInt() + ".");
        Utilities.printParagraph("Note that we are asking str "
            + "for the next int, not kbd! "
            + "kbd will continue to read from System.in, "
            + "just like we told it to. "
            + "And that's a good thing, "
            + "because we're going to be asking you to type more stuff in "
            + "in just a few moments.");

        // pause
        System.out.print("Press Enter...");
        kbd.nextLine();
        System.out.println("\n\n\n\n");

        // Scanner for String the user enters
        Utilities.printParagraph("And just like with our names above, "
            + "we can use data the user enters in our code. "
            + "All we need to do is ask the user to enter the String "
            + "(and HOPE that the user co-operates "
            + "by entering the correct kind of String).");

        // get user's line
        System.out.print("Please enter a String "
            + "starting with at least three int values: ");
        String userLine = kbd.nextLine();
        System.out.println();
        Utilities.printParagraph("The String you entered was "
            + "\"" + userLine + "\", which we called userLine. "
            + "(If that line doesn't start with three int values, "
            + "then this program will very soon crash!)");

        // pause
        System.out.print("Press Enter...");
        kbd.nextLine();
        System.out.println("\n\n\n\n");

        // create Scanner for user's line
        Scanner scanLine = new Scanner(userLine);   // yet another Scanner

        Utilities.printParagraph("Before we can read the numbers "
            + "from that line, "
            + "we need to create a new Scanner:");
        System.out.println("\tScanner scanLine = new Scanner(userLine);\n");

        // check for bad input off the bat
        if (!scanLine.hasNextInt()) {
            Utilities.printParagraph("Oh, I can tell now "
                + "that your input doesn't even have ONE int at the start. "
                + "You disappoint me, " + firstName + ". "
                + "I guess the program is going to crash....");
            Utilities.printParagraph("Note that the program is going to crash "
                + "because we ask for the next int from a string "
                + "that doesn't have a next int. "
                + "The Scanner itself is created just fine.");

            // pause
            System.out.print("Press Enter...");
            kbd.nextLine();
            System.out.println("\n\n\n\n");
        }
        Utilities.printParagraph("Now we simply ask for the values, "
            + "using the usual method (nextInt), "
            + "but addressed to the new Scanner (scanLine). "
            + "Assuming you did as you were asked, "
            + "the first three calls to scanLine.nextInt() return "
            + scanLine.nextInt() + ", "
            + scanLine.nextInt() + ", and "
            + scanLine.nextInt() + ". ");

        // pause
        System.out.print("Press Enter...");
        kbd.nextLine();
        System.out.println("\n\n\n\n");
    }

}
