Docsity

CS 1400 Lab #5: Token-Based File Processing - CheckBook.java, Lab Reports of Computer Science

Toggle sidebar
bg1
CS 1400 Lab #5
Token-Based File Processing
In the old days (when dinosaurs roamed the earth and professors were young), before debit
cards, people often wrote checks to pay for purchases. To track your current balance, you
recorded your purchases and deposits in a checkbook register, which had columns for the item
(check number or deposit), date, transaction (the recipient of the check), and the amount.
Included with the lab is a file named checkbook.txt, which represents a checkbook register:
deposit:July 7:-:300.00
416:July 8:Albertsons:15.85
417:7/9:Checker Auto:19.95
418:7/10:Super Target:47.5
419:Dec 5:Home Depot:47.89
By default, scanners use white space to separate tokens. However, the columns or fields in the
checkbook input file are separated by colons (‘:’). This allows tokens to contain spaces (e.g.,
“Super Target”). The Scanner class provides a method named useDelimiter to change the default
delimiters. For example, if the Scanner object is named “input,” then include the statement:
input.useDelimiter(":|\r\n");
The item column distinguishes a deposit from a check by using the word “deposit” for the former
and the check number for the latter. A dash (‘-’) appears in the transaction column for a deposit.
You may find it helpful to review table 4.5, p. 241.
Write a Java program named CheckBook.java that does the following:
1. Prompts for the name of a file to read
2. Prints an error message and terminates if the file does not exist (two techniques were
demonstrated in class, you may use either)
3. Reads and displays the contents of the file
4. Calculates and prints the current balance
5. Program output is illustrated below (note that the dash in the transaction field of a deposit
does not appear in the output)
===========================================
Item Date Transaction Amount
-------------------------------------------
deposit July 7 300.00
416 July 8 Albertsons 15.85
417 7/9 Checker Auto 19.95
418 7/10 Super Target 47.50
419 Dec 5 Home Depot 47.89
-------------------------------------------
Balance 168.81

Partial preview of the text

Download CS 1400 Lab #5: Token-Based File Processing - CheckBook.java and more Lab Reports Computer Science in PDF only on Docsity!

CS 1400 Lab

Token-Based File Processing

In the old days (when dinosaurs roamed the earth and professors were young), before debit cards, people often wrote checks to pay for purchases. To track your current balance, you recorded your purchases and deposits in a checkbook register, which had columns for the item (check number or deposit), date, transaction (the recipient of the check), and the amount.

Included with the lab is a file named checkbook.txt, which represents a checkbook register:

deposit:July 7:-:300. 416:July 8:Albertsons:15. 417:7/9:Checker Auto:19. 418:7/10:Super Target:47. 419:Dec 5:Home Depot:47.

By default, scanners use white space to separate tokens. However, the columns or fields in the checkbook input file are separated by colons (‘:’). This allows tokens to contain spaces (e.g., “Super Target”). The Scanner class provides a method named useDelimiter to change the default delimiters. For example, if the Scanner object is named “input,” then include the statement: input.useDelimiter(":|\r\n"); The item column distinguishes a deposit from a check by using the word “deposit” for the former and the check number for the latter. A dash (‘-’) appears in the transaction column for a deposit.

You may find it helpful to review table 4.5, p. 241.

Write a Java program named CheckBook.java that does the following:

  1. Prompts for the name of a file to read
  2. Prints an error message and terminates if the file does not exist (two techniques were demonstrated in class, you may use either)
  3. Reads and displays the contents of the file
  4. Calculates and prints the current balance
  5. Program output is illustrated below (note that the dash in the transaction field of a deposit does not appear in the output) =========================================== Item Date Transaction Amount

deposit July 7 300. 416 July 8 Albertsons 15. 417 7/9 Checker Auto 19. 418 7/10 Super Target 47. 419 Dec 5 Home Depot 47.


Balance 168.