CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Data

Data
Formatted Application
Analyze data

Output data that illustrates a problem with the Invoice application

The results for a subtotal of 100.05 don't add up. If the discount amount is $10.00, the total before tax should be $90.05, but it's $90.04. Similarly, the sales tax for a subtotal entry of .70 is shown as $0.03, so the invoice total should be $0.73, but it's shown as $0.74.

Statements that you can add to the program to help analyze this problem:

// debugging statements that display the unformatted fields
// these are added before displaying the formatted results
String debugMessage = "\nUNFORMATTED RESULTS\n"
    + "Discount percent: " + discountPercent + "\n"
    + "Discount amount:  " + discountAmount + "\n"
    + "Total before tax: " + totalBeforeTax + "\n"
    + "Sales tax:        " + salesTax + "\n"
    + "Invoice total:    " + total + "\n"
    + "\nFORMATTED RESULTS";
System.out.println(debugMessage);


import java.util.Scanner;
import java.text.NumberFormat;

public class FormattedInvoiceApp
{
    public static void main(String[] args)
    {
        // welcome the user to the program
        System.out.println("Welcome to the Invoice Total Calculator");
        System.out.println();  // print a blank line

        // create a Scanner object and start a while loop
		Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
		    // get the input from the user
            System.out.print("Enter subtotal:   ");
        	double subtotal = sc.nextDouble();

        	// calculate the discount amount and invoice total
        	double discountPercent = 0.0;
        	if (subtotal >= 200)
        		discountPercent = .2;
        	else if (subtotal >= 100)
        		discountPercent = .1;
        	else
        		discountPercent = 0.0;
   			double discountAmount = subtotal * discountPercent;
  			double totalBeforeTax = subtotal - discountAmount;
  			double salesTax = totalBeforeTax * .05;
  			double total = totalBeforeTax + salesTax;

        	// get the currency and percent formatter objects
        	NumberFormat currency = NumberFormat.getCurrencyInstance();
        	NumberFormat percent = NumberFormat.getPercentInstance();

			// debugging statements that display the unformatted fields
			// these are added before displaying the formatted results
			String debugMessage = "\nUNFORMATTED RESULTS\n"
    		  + "Discount percent: " + discountPercent + "\n"
  			  + "Discount amount:  " + discountAmount + "\n"
  			  + "Total before tax: " + totalBeforeTax + "\n"
 		      + "Sales tax:        " + salesTax + "\n"
  			  + "Invoice total:    " + total + "\n"
  			  + "\nFORMATTED RESULTS";
			System.out.println(debugMessage);

			// display the results
        	String message =
        	    "Discount percent: " + percent.format(discountPercent) + "\n"
        	  + "Discount amount:  " + currency.format(discountAmount) + "\n"
        	  + "Total before tax: " + currency.format(totalBeforeTax) + "\n"
        	  + "Sales tax:        " + currency.format(salesTax) + "\n"
        	  + "Invoice total:    " + currency.format(total) + "\n";
            System.out.println(message);

        	// see if the user wants to continue
        	System.out.print("Continue? (y/n): ");
        	choice = sc.next();
        	System.out.println();
		}
    }
}

The unformatted and formatted output data

Previous | Formatted Application | Analyze data | Next