CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Data

Data
Formatted Application

The console for the formatted Invoice application

The code for the formatted Invoice application

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();

			// 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();
		}
    }
}
Previous | Formatted Application | Analyze data | Next