eCheck07A can be found on page 287 of the textbook. This eCheck is quite challenging; it requires that you use strings, a string tokenizer to parse input, wrapper classes to convert strings to numbers, a credit card class, and makes heavy use of formatted output.
Before you start, you should open the type.lib.CreditCard
API in your web browser (in a separate tab or window).
You need to write a Java program that creates a credit card and applies transactions to the credit card where the transactions are read in from a text file.
Create a Java file called Check07A.java and insert the usual template code.
Ask the user for the required input. An example prompt and user input string would be:
Enter name, limit, and trx filename (comma-delimited):
Adam, 7500, lab7data.txt
Adam is the name of the credit card holder, 7500 is the credit card limit, and lab7data.txt is the name of the file holding all of the transactions.
You should read the input using the nextLine
method in the Scanner
class.
You should use a StringTokenizer
object
to break the user input string into separate pieces
(separated by commas). Find the API for
StringTokenizer
in the Java 6 API.
A tokenizer breaks strings into substrings (called tokens) separated by a client-specified string (called a delimiter). The user input for this problem is separated by commas, so you want to create a tokenizer that splits a string wherever a comma appears:
// add this after the point where you get the user input StringTokenizer tokenizer = new StringTokenizer(userInput, ",");
The above code creates a tokenizer that splits the string named
userInput
into substrings separated by a comma.
The client retrieves the substrings one at a time using the
nextToken
method.
String name = tokenizer.nextToken().trim(); String limit = tokenizer.nextToken().trim(); String filename = tokenizer.nextToken().trim();
Notice that the tokens are trimmed to remove an extra whitespace.
You need to convert limit
to a double
value (use the Java Double
class),
and then create a CreditCard
object using any
credit card number you wish, the name, and the limit as a real number.
The first thing you will need to do is add an import statement and
modify the header of the main
method:
import java.io.PrintStream; import java.io.File; import java.util.Scanner; import type.lib.CreditCard; public static void main(String[] args) throws IOException
This is required because creating a File
object
can throw an exception and you do not yet know how to deal with
exceptions in client code.
To read the file, you can use a scanner (just like reading input from a keyboard):
// add this after the point where you create a CreditCard object Scanner fileIn = new Scanner(new File(filename)); for (; fileIn.hasNextLine(); ) { String line = fileIn.nextLine(); // the rest of your code goes here }
The loop will read in file one line at a time where each line is
stored in a string named line
.
Inside the loop, you need to break the string line
into tokens separated by spaces. You need to create a new
StringTokenizer
object to help you; read the constructor
section of the API to find out how to create an appropriate tokenizer.
Once you have a tokenizer, you have to find out how many tokens
the string actually has. If you read the question carefully, you will see
that valid transactions have 2 tokens (a code letter and an amount); if
there are more or less than 2 tokens on a line then the transaction is
invalid. Fortunately, the tokenizer has a method named countTokens
that tells you how many tokens are in the string.
If the line has 2 tokens you may assume that the transaction is valid. Now you have to decide what type of transaction has occurred. The first token, a single letter, tells you the type of the transaction:
Making a payment always succeeds, but charging an amount or changing the limit can fail (see the API).
The second token in the line is the amount of the transaction. Again, the
tokenizer returns a string that you have to convert to a double
value.
Based on the type of the transaction, you have to call a method on
the credit card object (one of charge
, pay
,
or setLimit
) and determine if the transaction was successful.
If the transaction was successful, you need to output a line that ends in
"done."
, otherwise you need to output a line that ends in
"rejected."
(see the textbook for examples).
If the line does not have 2 tokens then the transaction is invalid.
In this case, you need to print out a line that ends in
"invalid record."
(see the textbook for examples).
Outside at the end of the loop (when all of the transactions have been processed) you need to output the final balance and credit limit of the card; see the textbook for the exact formatting required.
Here is a file of transactions that you can test your program with.