Demo - Number Utility
Number Utility Static Class
/**
* Static Class to check format of a customer number
* Book Example modified by Kevin Roark
* */
public class NumberUtility {
/**
* The isValid method accepts a String as its argument
* and tests its contents for a valid customer number.
*/
public static boolean isValid(String custNumber)
{
boolean goodSoFar = true; // Flag
int index = 0; // Loop control variable
// Is the string the correct length?
if (custNumber.length() != 7)
goodSoFar = false;
// Test the first three characters for letters.
while (goodSoFar && index < 3)
{
if (!Character.isLetter(custNumber.charAt(index)))
goodSoFar = false;
index++;
}
// Test the last four characters for digits.
while (goodSoFar && index < 7)
{
if (!Character.isDigit(custNumber.charAt(index)))
goodSoFar = false;
index++;
}
// Return the results
return goodSoFar;
}
public static void PrintHeader()
{
System.out.println("*************************************");
System.out.println(" Number Utilty");
System.out.println("*************************************");
}
}
The NumberUtility
class provides utility methods for checking the format of a customer number. Let's break it down step by step:
The class is declared as a public class named
NumberUtility
. It contains static methods and serves as a utility class.The
isValid
method is a static method that accepts aString
argumentcustNumber
. It tests whether the givencustNumber
is a valid customer number.Inside the
isValid
method, a boolean variablegoodSoFar
is initialized astrue
, acting as a flag to track the validity of the customer number. An integer variableindex
is also declared and initialized to 0, which will be used as a loop control variable.The method checks the length of
custNumber
and if it is not equal to 7 characters, thegoodSoFar
flag is set tofalse
.The method then iterates through the first three characters of
custNumber
using a while loop. It checks if each character is a letter by calling theCharacter.isLetter
method. If any character is not a letter, thegoodSoFar
flag is set tofalse
.After the loop completes, the method continues to iterate through the last four characters of
custNumber
using another while loop. It checks if each character is a digit by calling theCharacter.isDigit
method. If any character is not a digit, thegoodSoFar
flag is set tofalse
.Finally, the
isValid
method returns the value of thegoodSoFar
flag, indicating whether thecustNumber
is a valid customer number or not.The
PrintHeader
method is another static method that simply prints a header message to the console. It prints a series of asterisks and the title "Number Utility".
In summary, the NumberUtility
class provides utility methods for checking the validity of a customer number and printing a header message. The isValid
method checks if a given string meets the criteria of a valid customer number, while the PrintHeader
method prints a header message to the console.
Driver
import java.util.Scanner;
/**
* This program tests a customer number to determine
* whether it is in the proper format.
*/
public class Demo2
{
public static void main(String[] args)
{
String customer; // To hold a customer number
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Print Program Header
NumberUtility.PrintHeader();
System.out.println("Enter a customer number in " +
"the form LLLNNNN");
System.out.print("(LLL = letters and NNNN " +
"= numbers): ");
// Get a customer number from the user.
customer = keyboard.nextLine();
// Determine whether it is valid.
if (NumberUtility.isValid(customer))
{
System.out.println("That's a valid customer " +
"number.");
}
else
{
System.out.println("That is not the proper " +
"format.");
System.out.println("Here is an example: " +
"ABC1234");
}
keyboard.close();
}
}
The code tests a customer number entered by the user to determine whether it is in the proper format. Let's go through it step by step:
The code imports the
Scanner
class from thejava.util
package to facilitate keyboard input.The
Demo2
class is declared as a public class.The main method starts. It declares a
String
variablecustomer
to hold a customer number.A
Scanner
object namedkeyboard
is created to read input from the keyboard.The
NumberUtility.PrintHeader()
method is called to print a program header message to the console. It is a static method from theNumberUtility
class.A prompt is displayed, asking the user to enter a customer number in the form "LLLNNNN" (where "LLL" represents letters and "NNNN" represents numbers).
The user is prompted to enter a customer number.
The entered customer number is stored in the
customer
variable using thekeyboard.nextLine()
method.The
NumberUtility.isValid(customer)
method is called to determine whether the entered customer number is valid. It is a static method from theNumberUtility
class.If the customer number is valid, a message stating "That's a valid customer number." is printed to the console.
If the customer number is not valid, a message stating "That is not the proper format." is printed to the console, along with an example customer number "ABC1234".
The
keyboard.close()
statement closes theScanner
object.
In summary, the code prompts the user to enter a customer number, validates it using the NumberUtility.isValid
method, and provides feedback about whether it is in the proper format. It also prints a program header message.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark