One way to increase the likelihood of correct data entry is to use checksum software to test whether the digits of a potential code number have some particular property.
For example, there is a verification scheme for the Universal Product Code (UPC) numbers. The familiar UPC in bar code form is shown below.
Fig. 1: UPC bar code 128016691675. |
A valid UPC number is a 12-digit value that meets the
following
criteria:
Consider the number 780070121354.
For this number,
m = 8 + 0 + 0 + 2 + 3 = 13Because r = 4, the 12th digit in the UPC code, the number corresponds to a possible UPC code.
n = 7 + 0 + 7 + 1 + 1 + 5 = 21
r = 10 - ((13 + 3 * 21) mod 10)
= 10 - (76 mod 10)
= 10 - 6
= 4
Now consider the number 123456789011.
For this number,
m = 2 + 4 + 6 + 8 + 0 = 20Because r is not equal to 1, the 12th digit, the number does not correspond to a possible UPC code.
n = 1 + 3 + 5 + 7 + 9 + 1 = 26
r = 10 - ((20 + 3 * 26) mod 10)
= 10 - (98 mod 10)
= 10 - 8
= 2
Write a driver program (i.e., a program with a main method) called TestUPC that does the following:
0. Display the function of the program to the user, i.e.,
let them know what they should enter and what to expect
for a result
1. While true
2. Prompt the user to enter a 12-digit number (read this in as a String)
3. While the String entered has the wrong number of digits, or if the String contains non-digits
4. Report an invalid entry and prompt the user to re-enter the number
5. Create a new UniversalProductCode object called upc
6. If upc is valid
7. Tell the user that the number is a feasible UPC code
8. Else
9. Tell the user that the number is not a feasible UPC code
10. If the user does not want to enter another UPC code, break
11. Exit the program