In: Computer Science
(code in java please)
14.20 (Check Protection) Computers are frequently employed in check-writing systems, such as payroll and accounts payable applications. There are many strange stories about weekly paychecks being printed (by mistake) for amounts in excess of $1 million. Incorrect amounts are printed by computerized check-writing systems because of human error or machine failure. Systems designers build controls into their systems to prevent such erroneous checks from being issued.
Another serious problem is the intentional alteration of a check amount by someone who plans to cash a check fraudulently. To prevent a dollar amount from being altered, some computerized check-writing systems employ a technique called check protection. Checks designed for imprinting by computer contain a fixed number of spaces in which the computer may print an amount. Suppose a paycheck contains eight blank spaces in which the computer is supposed to print the amount of a weekly paycheck. If the amount is large, then all eight of the spaces will be filled. For example,
1,230.60 (check amount) -------- 12345678 (position numbers)
On the other hand, if the amount is less than $1000, then several of the spaces would ordinarily be left blank. For example,
99.87 -------- 12345678
contains three blank spaces. If a check is printed with blank spaces, it’s easier for someone to alter the amount. To prevent alteration, many check-writing systems insert leading asterisks to protect the amount as follows:
***99.87 -------- 12345678
Write an application that inputs a dollar amount to be printed on a check, then prints the amount in check-protected format with leading asterisks if necessary. Assume that nine spaces are available for printing the amount.
import java.util.Scanner;
public class CheckProtection {
public static void main(String[] args) {
// Variable to hold the max number
of spaces to be filled
final int MAX_SPACES = 9;
// String to prevent amount
alteration on checks
final String
ALTERATION_PREVENTION_CHAR = "*";
// Scanner to get user
input
Scanner in = new
Scanner(System.in);
// Get dollar amount
System.out.print("Enter dollar
amount to be printed: $");
String amount =
in.nextLine().trim();
// Check if amount length is
less than or equal to MAX_SPACES
if (amount.length() <=
MAX_SPACES) {
// Convert
amount to check protected format
String
amountInCheckProtectedFormat = String.format("%" + MAX_SPACES +
"s", amount).replaceAll("\\s",
ALTERATION_PREVENTION_CHAR);
// Print
amount in check protected format
System.out.printf("Amount in check protected format: %s",
amountInCheckProtectedFormat);
} else
System.out.print("ERROR: Dollar amount cannot be greater than " +
MAX_SPACES + " length.");
}
}
CODE SCREENSHOTS:
SAMPLE OUTPUT: