CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades | Students

Lab #4 (Exercise 5-1)

Add validation to the Invoice application

Due Date: Tuesday of Week 2
15 points

Before you start, check which team you belong to.

In this lab, you'll add data validation to the Invoice application. To do that, you'll use exception handling. You'll also write and use some specific data validation methods.

  1. If you downloaded the file, open the file named CodeInvoiceApp.java that's in your computer or USB drive. Save the program as ValidatedInvoiceApp.java. Change the class name to ValidatedInvoiceApp. Compile and run the application. If you don't have the file, copy and paste the code below:
  2. As you test the application, enter an invalid customer type code to see what happens. Then, enter an invalid subtotal entry like $1000 to see what happens when the application crashes.
  3. Validate the computer type code

  4. Modify the application so it will only accept these customer type codes: r/c/t or just r/c, depending on which version of the program you're modifying. It should also discard any extra entries on the customer type line. If the user enters an invalid code, the application should display an error message and ask the user to enter a valid code. Now, test this enhancement.

  5. Code a static method named getValidCustomerType that does the validation of step 3. This method should require one parameter that receives a Scanner object, and it should return a valid customer type code. The method should get an entry from the user, check it for validity, display an error message if it's invalid, and discard any other user entries whether or not the entry is valid. This method should continue getting user entries until one is valid.

  6. After you've written this method, modify the application so it uses this method. Then test this enhancement.
  7. Validate the subtotal

  8. Add a try statement so it catches any InputMismatchException that the nexDouble method of the Scanner class might throw. The catch block should display an error message and issue a continue statement to jump to the beginning of the while loop. It should also discard the invalid entry and nay other entries on the line. To do this, you need to import the exception classes in the java.util package, and the best way to do that is to import all of the classes in this package. Now, test this enhancement.

  9. Code a static method named getValidSubtotal that uses the hasDouble method of the Scanner class to validate the subtotal entry so the InputmismatchException won't occur. This method should require one parameter that receives a Scanner object, and it should return a valid subtotal. This method should get an entry from the user, check that it's a valid double value, check that it's greater than zero and less than 10000, display appropriate error messages if it isn't valid, and discard any other user entries whether or not the entry is valid. This should continue until the method gets a valid subtotal entry.

  10. After you've written this method, modify the code within the try statement so it uses this method. then, test this enhancement so you can see that an InputMismatchException is no longer caught by the catch block. (When the code in a try block calls a method, any exception that isn't handled by the method is passed back to the code in the try block.)
  11. Discard any extra entries for the Continue prompt

  12. Modify the code so the application will work right even if the user enters two or more entries when asked if he wants to continue. To do that, you need to discard any extra entries. Then, test this enhancement.
At this point, the application should be bulletproof. It should only accept valid entries for customer type and subtotal, and it should work even if the user makes two or more entries for a single prompt.

Include in your program the following comments:

  • Name of program
  • Team's name with all the names of the members (e.g. Team A: John Doe, Jane Smith, etc.)
  • Current Date
  • Computer system and compiler you are using
  • Brief description of the program


Compile and run the Java program.

Take a screen capture by hitting simultaneously the keys Alt and Print Screen (Prt Sc) of the output results (console) and paste it in MS Paint. Save it as a gif file.

When you are ready, upload the java program by clicking the link below:

Upload Lab

I cannot grade if I don't get the files and receive your email.