Code to the invoice application that validates the data the


Exercise 2-3 Modify the Invoice application

In this exercise, you'll modify the Invoice application. When you're through with the modifications, a test run should look something like this:

Welcome to the Invoice Total Calculator

Enter subtotal:            100

Discount percent:         0.1

Discount amount:         10.0

Invoice total:              90.0

Continue? (y/n): y

Enter subtotal:            500

Discount percent:        0.25

Discount amount:        125.0

Invoice total:             375.0

Continue? (y/n): n

Number of invoices: 2
Average invoice: 232.5

Average discount: 67.5

1. Open the project named ch02_ex3_Invoice that's in the ex_starts directory. Then, open the file named InvoiceApp.java.

2. Modify the code so the application ends only when the user enters "n" or "N". As it is now, the application ends when the user enters anything other than "y" or "Y". To do this, you need to use a not operator (!) with the equalsIgnoreCase method. This is illustrated by the third example in figure 2-14. Then, compile this class and test this change by entering 0 at the Continue prompt.

3. Modify the code so it provides a discount of 25 percent when the subtotal is greater than or equal to 500. Then, test this change.

4. Modify the code so it displays the number of invoices, the average invoice amount, and the average discount amount when the user ends the program. Then, test this change.

Exercise 3-2 Modify the Test Score application

In this exercise, you'll use some of the skills that you learned in this chapter to modify the Test Score application.

I. Open the project named ch03_ex2_TestScore that's in the ex_starts directory.

Then, review the code for this project and run it until you understand how it works.

2. Use the += operator to increase the scoreCount and scoreTotal variables. Then, test this to make sure that it works.

3. As the user enters test scores, use the methods of the Math class to keep track of the minimum and maximum scores. When the user enters 999 to end the program, display these scores at the end of the other output data. Now, test these changes to make sure that they work. (This step can be challenging if you're new to programming, but you'll learn a lot by doing it.)

4. Change the variable that you use to total the scores from a double to an int data type. Then, use casting to cast the score count and score total to doubles as you calculate the average score and save that average as a double. Now, test that change.

5. Use the NumberFormat class to round the average score to one decimal place before displaying it at the end of the program. Then, test this change. Note that the rounding method that's used doesn't matter in a program like this.

Exercise 4-2 Enhance the Invoice application

In this exercise, you'll modify the nested if/else statements that are used to determine the discount percent for the invoice application in figure 4-6. Then, you'll code and call a static method that determines the discount percent.

Open the application and change the if/else statement

1. Open the project named ch04_ex2_1nvoice that's stored in the ex_starts directory. Then, run the application to see how it works.

2. Change the if/else statement so customers of type "R" with a subtotal that is greater than or equal to $250 but less than $500 get a 25% discount and those with a subtotal of $500 or more get a 30% discount. Next, change the if/else statement so customers of type "C" always get a 20% discount. Then, test the application to make sure this works.

3. Add another customer type to the if/else statement so customers of type "T" get a 40% discount for subtotals of less than $500, and a 50% discount for subtotals of $500 or more. Then, test the application.

4. Check your code to make sure that no discount is provided for a customer type code that isn't "R", "C", or "T". Then, fix this if necessary.
Code and call a static method that determines the discount percent

5. Code a static method named getDiscountPercent that has two parameters: customer type and subtotal. To do that efficiently, you can move the appropriate code from the main method of the application into the static method and make the required modifications.

6. Add code that calls the static method from the body of the application. Then, test to make sure that it works.

Exercise 5-1 Add validation to the Invoice application

In this exercise, you'll add code to the Invoice application that validates the data the user enters. That includes exception handling code as well as specific data validation methods.

1. Open the project named ch05_exl_Invoice in the ex_starts directory. Then, test the application to see how it works.

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.

Validate the customer type code

3. Modify the application so it will only accept customer type codes r and c. 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. This should be done before the user enters a subtotal. Then, test this enhancement.

4. Code a static method named get ValidCustomerType that does the validation of step 3. This method should include 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. The easiest way to add the code for this method is to copy the code you wrote in step 3.

5. Modify the application so it uses this method. Then, test this enhancement. Validate the subtotal

6. Add a try statement that catches any InputMismatchException that the nextDouble 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 any other entries on the line. For this to work, you'll need to import the InputMismatchException class, and you'll need to declare the subtotal variable before the try statement so it can be used outside that statement. Test this enhancement.

7. 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.

8. 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.

Discard any extra entries for the Continue prompt

9. Run the application again. When the Continue prompt is displayed, enter two or more values to see what happens.

10. Modify the code so the application works right even if the user enters two or more values 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.

Exercise 7-3 Use objects in the Invoice application

In this exercise, you'll create an Invoice class and construct objects from it as you convert the Invoice application to an object-oriented application.

1. Open the project named ch07_ex3invoice that's in the ex_starts directory. Then, review the code for the InvoiceApp and Validator classes, and run the project to see how this application works.

2. Start a new class named Invoice and save it in the same package as the other classes. Then, write the code for this class as described here, copying the code from the InvoiceApp class whenever that makes sense:

• Include two private fields for the customer type and subtotal entered by the user.

• Include a single constructor that accepts the customer type and subtotal as parameters.

• Include a get method that returns the subtotal as a double value. In addition, include get methods that calculate and return double values for the discount percent, discount amount, and total.

• Include get methods that return formatted string values for the subtotal, discount percent, discount amount, and total. These methods should call the other get methods to get the values to be formatted.

• Include a get method that returns a string that contains the full name for a customer type.

3. Modify the code in the InvoiceApp class so it creates an Invoice object. Then, call the methods of the Invoice object to display the formatted values for the Invoice, and delete any code that is no longer needed. That should simplify the InvoiceApp class considerably.

4. Test this application to make sure that it works the way it did in step 1.

Exercise 8-2 Create a Product application that uses inheritance

In this exercise, you'll create a Product application like the one presented in this chapter that uses inheritance. However, you will add an additional kind of product: compact discs.

Create a new subclass named CompactDisc

1. Open the project named ch08_ex2_Product that's in the ex_starts directory. Then, review the code.

2. Add a class named CompactDisc that inherits the Product class. This new class should work like the Book and Software classes, but it should include public get and set methods for a private instance variable named artist. In addition, its toString method should append the artist name to the end of the string.

Modify the ProductDB class so it returns a CompactDisc object

3. Modify the ProductDB class so it creates at least one CompactDisc object. For example, this object might contain the following information:

Code: sgtp
Description: Sgt. Pepper's Lonely Hearts Club Band
Price: $15.00
Artist: The Beatles
Add a protected variable

4. Open the Product class and change the access modifier for the count variable from public to protected.

5. Run the application to make sure that it works correctly and that the count is maintained properly.

Exercise 9-2 Add an update function to the Product Maintenance application

In this exercise, you'll review the Product Maintenance application presented in this chapter. Then, you'll add an update function to this application.

Review and run the application

1. Open the project named ch09_ex2_ProductMaintenance in the ex_starts directory.

2. Review the code in each file to see how it works.

3. Run the application and try each of its functions. When you're comfortable with how it works, exit from the application.
Modify the application so it includes an update function

4. Add code to the ProductMaintApp class that lets the user update an existing product. To do that, you'll need to add an update command to the list of commands, you'll need to add an updateProduct method that provides for updating a product, and you'll need to add an else if clause to the if statement in the main method that executes the updateProduct method if the user enters the update command.

5. The updateProduct method should start by getting a valid product code from the user. Then, it should ask the user if he wants to update the product's description or price. Depending on the user's response, it should then accept a new description or price from the user. Finally, it should call the updateProduct method of the ProductDAO object to update the product and print a line that indicates the update operation that was performed.

6. Run the ProductMaintApp class to make sure it works correctly.

Exercise 10-4 Create and use an enumeration

In this exercise, you'll create an enumeration and then use it in a test application.

1. Open the project named ch10_ex4_Enumeration that's in the ex_starts directory.

2. Create an enumeration named CustomerType. This enumeration should contain constants that represent three types of customers: retail, trade, and college.

3. Open the CustomerTypeApp class. Then, add a method to this class that returns a discount percent (.10 for retail, .30 for trade, and .20 for college) depending on the CustomerType variable that's passed to it.

4. Add code to the main method that declares a CustomerType variable, assigns one of the customer types to it, gets the discount percent for that customer type, and displays the discount percent. Run the application to be sure that it works correctly.

5. Add a statement to the main method that displays the string returned by the toString method of the customer type. Then, run the application again to see the result of this method.

6. Add a toString method to the CustomerType enumeration. This method should return a string that contains "Retail customer," "Trade customer," or "College customer" depending on the customer type. Run the application one more time to view the results of the toString method.

Exercise 11-4 Work with a deck of cards

In this exercise, you'll write an application that uses a variety of arrays and for loops to work with a deck of cards. If you can complete this exercise, you can be sure that you have a solid grasp of the skills presented in this chapter.

1. Open the project named chl l_ex4_CardDeck in the ex_starts directory. Then, open the CardDeckApp class.

2. Create an array whose elements hold the first initial of the four different suits in a card deck. Declare another array that can hold a representation of the cards in a deck of cards without jokers. Both of these arrays should be declared at the class level so they can be accessed by all the methods you'll add to this project.

3. Write a method to load the card array, one suit at a time. (Use the numbers 11, 12, and 13 to represent Jacks, Queens, and Kings respectively, and use the number 1 to represent Aces.) Write another method to print the cards in the array. Print each suit on a separate line by processing the cards array in 4 groups of 13 cards each. Separate the cards in each suit by a space. Call these two methods from the main method. Test the application to be sure the array is loaded and printed properly.

4. Write a method that shuffles the deck of cards. To do that, this method should get a number between 1 and 51 by multiplying the result of the random function by 50, converting it to an integer, and adding I. Then, it should switch each card in the deck with the card that is the given number of cards after it (if there is one). This should be repeated 100 times to shuffle the deck thoroughly. Call this method from the main method, followed by the method that prints the cards array. Test the application to be sure that the cards are shuffled.

5. Declare a rectangular array at the class level that represents fours hands of cards with five cards each. Write a method that loads this array by dealing cards from the cards array. Be sure to deal one card at a time to each hand. Write a method that prints the hands, separating the cards in each hand by a space and printing each hand on a separate line. Test the application to be sure that the cards are dealt properly.

Exercise 12-2 Use a linked list

In this exercise, you'll create a Future Value application that's similar to the one described in the previous exercise. However, this application will use a linked list and display the calculations in the reverse order.

1. Open the project named ch12_ex2 FutureValue that's stored in the ex_starts directory. Then, review the code for this application and run it to make sure it works correctly.

2. Declare a variable at the beginning of the main method for a linked list that stores strings.

3. After the code that calculates, formats, and displays the results for each calculation, add code that formats a string with the results of the calculation and then stores the string in the linked list.

4. Add code to display the elements in the linked list at the console when the user indicates that the program should end. This code should retrieve the elements of the linked list in reverse order. To do that, you'll need to use methods of the LinkedList class. Then, test the program by making at least 3 future value calculations.

Attachment:- Java.zip

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Code to the invoice application that validates the data the
Reference No:- TGS01122953

Now Priced at $90 (50% Discount)

Recommended (91%)

Rated (4.3/5)