Test log

Stage 1: String longer than 6 charactersINPUT DATAEXPECTED OUTPUTGENERATED OUTPUTFIELD5
abcdeInvalid input. Try again.Invalid input. Try again.
abcdefgPassPass
123456Invalid input. Try again.Invalid input. Try again.
1234567PassPass
Invalid input. Try again.Invalid input. Try again.
Stage 2: String containing lowercase ‘a’INPUT DATAEXPECTED OUTPUTGENERATED OUTPUT
ABCDEFGInvalid input. Try again.Invalid input. Try again.
aBCDEFGPassPass
AbCdEfGInvalid input. Try again.Invalid input. Try again.
abCdEfGaPassPass
aaaaaaaaaaaaaaPassPass
bbbbbbaaaaaaaPassPass
cccccccccccaPassPass
dddddddddddaPassPass
eeeeeeeeeeeaPassPass
fffffffffzaInvalid input. Try again.Invalid input. Try again.
Stage 3: String between 5-15 characters (no ‘z’)INPUT DATAEXPECTED OUTPUTGENERATED OUTPUT
abInvalid input. Try again.Invalid input. Try again.
abcdefghijklmnopqrInvalid input. Try again.Invalid input. Try again.
abcdezInvalid input. Try again.Invalid input. Try again.
abcdefghijPassPass
123456789012345Invalid input. Try again.Invalid input. Try again.
12345678901234PassPass
Stage 4: Integer between 5 and 500INPUT DATAEXPECTED OUTPUTGENERATED OUTPUT
3Invalid input. Try again.Invalid input. Try again.
501Invalid input. Try again.Invalid input. Try again.
123PassPass
1Invalid input. Please enter an integer.Invalid input. Please enter an integer.
abcInvalid input. Please enter an integer.Invalid input. Please enter an integer.
500.5Invalid input. Please enter an integer.Invalid input. Please enter an integer.
Stage 5: Negative IntegerINPUT DATAEXPECTED OUTPUTGENERATED OUTPUT
5Invalid input. Try again.Invalid input. Try again.
-123PassPass
-5.5Invalid input. Please enter an integer.Invalid input. Please enter an integer.
Stage 6: PositiveOdd IntegerINPUT DATAEXPECTED OUTPUTGENERATED OUTPUT
-7Invalid input. Try again.Invalid input. Try again.
10Invalid input. Try again.Invalid input. Try again.
25PassPass
5aInvalid input. Please enter an integer.Invalid input. Please enter an integer.

Flow Chart

graph LR

subgraph Data Validation Program

    Start((Start)) --> Stage1["Stage 1"]

    Stage1 --> Validation1{Validate String Length > 6}

    Validation1 --> |Invalid| Stage1

    Validation1 --> |Valid| Stage2["Stage 2"]

    Stage2 --> Validation2{Validate Contains Lowercase 'a'}

    Validation2 --> |Invalid| Stage2

    Validation2 --> |Valid| Stage3["Stage 3"]

    Stage3 --> Validation3{Validate Length 5-15 & No 'z'}

    Validation3 --> |Invalid| Stage3

    Validation3 --> |Valid| Stage4["Stage 4"]

    Stage4 --> Validation4{Validate Integer 5-500}

    Validation4 --> |Invalid| Stage4

    Validation4 --> |Valid| Stage5["Stage 5"]

    Stage5 --> Validation5{Validate Negative Integer}

    Validation5 --> |Invalid| Stage5

    Validation5 --> |Valid| Stage6["Stage 6"]

    Stage6 --> Validation6{Validate Positive Odd Integer}

    Validation6 --> |Invalid| Stage6

    Validation6 --> |Valid| AllStagesSuccessful{All Stages Successful?}

    AllStagesSuccessful --> |No| Stage1

    AllStagesSuccessful --> |Yes| Congratulations["Congratulations!"]

    Congratulations --> End((End))

end

Top down design

Data Validation Program - Top-Down Design

Overall Purpose:

  • Repeatedly prompt the user for input until all six validation stages are successfully completed.
  • Each stage has a unique validation requirement.

Input:

  • Stage 1: String (from user)
  • Stage 2: String (from user)
  • Stage 3: String (from user)
  • Stage 4: Integer (from user)
  • Stage 5: Integer (from user)
  • Stage 6: Integer (from user)

Processing:

  1. Main Function:

    • Initializes success flags for each stage (all set to false initially).
    • Enters a loop that continues until all stages are successful.
    • Within the loop, calls each stage function if it has not been successfully completed yet.
  2. Stage Functions (stage1, stage2, …, stage6):

    • Prompt: Displays a message instructing the user on the type of input required.
    • Read Input: Uses a Scanner to read the user’s input.
    • Validate: Performs specific checks on the input:
      • Stage 1: Checks if the string length is greater than 6 characters.
      • Stage 2: Checks if the string (converted to lowercase) contains the letter ‘a’.
      • Stage 3: Checks if the string length is between 5 and 15 characters and does not contain the letter ‘z’.
      • Stage 4: Checks if the integer is between 5 and 500 (inclusive).
      • Stage 5: Checks if the integer is negative.
      • Stage 6: Checks if the integer is positive and odd.
    • Handle Invalid Input: If the input is invalid, displays an error message and continues to the next iteration of the loop within that stage.
    • Return Value:
      • If the input is valid, returns true to indicate success.
      • If the input is invalid, returns false.

Output:

  • Error Messages:
    • For each stage, if the input is invalid, an appropriate error message is displayed.
  • Success Message:
    • Once all stages have been successfully completed, a congratulatory message is printed: “Congratulations! You have successfully completed all stages.”

Code

// Name: Jason Cameron
// Date: 2024-07-20
// Purpose of program: To create a data validation program that prompts the user for various types of input and validates the input based on specific criteria.
 
import java.util.Scanner;
 
public class DataValidation {
 
    // Stage 1: Validates a string input from the user.
    // The input must be longer than 6 characters to be considered valid.
    public static boolean stage1() {
        Scanner scanner = new Scanner(System.in);
        while (true) { // Continue prompting the user until valid input is provided.
            System.out.print("Enter a string longer than 6 characters: ");
            String input = scanner.nextLine(); // Read the user's input.
            if (input.length() > 6) { // Check if the input length meets the criteria.
                return true; // Return true to indicate successful validation.
            } else {
                System.out.println("Invalid input. Try again."); // Display an error message.
            }
        }
    }
 
    // Stage 2: Validates a string input from the user.
    // The input must contain at least one lowercase 'a' character to be considered valid.
    public static boolean stage2() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a string containing at least one lowercase 'a': ");
            String input = scanner.nextLine();
            if (input.toLowerCase().contains("a")) { // Convert to lowercase and check for 'a'.
                return true;
            } else {
                System.out.println("Invalid input. Try again.");
            }
        }
    }
 
    // Stage 3: Validates a string input from the user.
    // The input must be between 5 and 15 characters long and cannot contain the letter 'z' to be considered valid.
    public static boolean stage3() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a string between 5 and 15 characters long (no 'z'): ");
            String input = scanner.nextLine();
            if (input.length() >= 5 && input.length() <= 15 && !input.contains("z")) { // Check length and absence of 'z'.
                return true;
            } else {
                System.out.println("Invalid input. Try again.");
            }
        }
    }
 
    // Stage 4: Validates an integer input from the user.
    // The input must be a number between 5 and 500 (inclusive) to be considered valid.
    public static boolean stage4() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter an integer between 5 and 500: ");
            try { // Attempt to read an integer from the input.
                int num = scanner.nextInt();
                if (num >= 5 && num <= 500) { // Check if the integer is within the valid range.
                    return true;
                } else {
                    System.out.println("Invalid input. Try again.");
                }
            } catch (java.util.InputMismatchException e) { // Handle the case where the input is not an integer.
                System.out.println("Invalid input. Please enter an integer.");
                scanner.nextLine(); // Clear the invalid input from the buffer.
            }
        }
    }
 
    // Stage 5: Validates an integer input from the user.
    // The input must be a negative number to be considered valid.
    public static boolean stage5() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a negative integer: ");
            try {
                int num = scanner.nextInt();
                if (num < 0) {
                    return true;
                } else {
                    System.out.println("Invalid input. Try again.");
                }
            } catch (java.util.InputMismatchException e) {
                System.out.println("Invalid input. Please enter an integer.");
                scanner.nextLine();
            }
        }
    }
 
    // Stage 6: Validates an integer input from the user.
    // The input must be a positive and odd number to be considered valid.
    public static boolean stage6() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter a positive, odd integer: ");
            try {
                int num = scanner.nextInt();
                if (num > 0 && num % 2 != 0) { // Check if the integer is positive and odd.
                    return true;
                } else {
                    System.out.println("Invalid input. Try again.");
                }
            } catch (java.util.InputMismatchException e) {
                System.out.println("Invalid input. Please enter an integer.");
                scanner.nextLine();
            }
        }
    }
 
    // Main function that runs the data validation process
    public static void main(String[] args) {
        boolean stage1Success = false, stage2Success = false, stage3Success = false,
                stage4Success = false, stage5Success = false, stage6Success = false;
 
        while (!(stage1Success && stage2Success && stage3Success && stage4Success && stage5Success && stage6Success)) {
            if (!stage1Success) stage1Success = stage1(); // Attempt stage 1 if not already passed
            if (!stage2Success) stage2Success = stage2(); // Attempt stage 2 if not already passed
            if (!stage3Success) stage3Success = stage3(); // Attempt stage 3 if not already passed
            if (!stage4Success) stage4Success = stage4(); // Attempt stage 4 if not already passed
            if (!stage5Success) stage5Success = stage5(); // Attempt stage 5 if not already passed
            if (!stage6Success) stage6Success = stage6(); // Attempt stage 6 if not already passed
        }
 
        System.out.println("Congratulations! You have successfully completed all stages.");
    }
}