Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.text.NumberFormat;
  3.  
  4. public class FormattedInvoice {
  5. public static void main(String[] args) {
  6.  
  7. final double SALES_TAX_PCT = .05;
  8.  
  9. Scanner sc = new Scanner(System.in);
  10. String choice = "y";
  11. while (choice.equalsIgnoreCase ("y")){
  12. //get the input from the user
  13. System.out.print("Enter Subtotal: ");
  14. double subtotal = sc.nextDouble();
  15.  
  16. //calculate the results
  17. double discountPercent = 0.0;
  18. if (subtotal>= 100)
  19. discountPercent = .1;
  20. else
  21. discountPercent = 0.0;
  22. double discountAmount = subtotal * discountPercent;
  23. double totalBeforeTax= subtotal - discountAmount;
  24. double salesTax = totalBeforeTax * SALES_TAX_PCT;
  25. double total = totalBeforeTax + salesTax;
  26.  
  27. //format and display the results
  28. NumberFormat currency = NumberFormat.getCurrencyInstance();
  29. NumberFormat percent = NumberFormat.getPercentInstance();
  30. String message =
  31. "Discount percent: " + percent.format(discountPercent) + "\n"
  32. + "Discount amount: " + currency.format(discountAmount) + "\n"
  33. + "Total Before Tax: " + currency.format(totalBeforeTax) + "\n"
  34. + "Sales Tax: " + currency.format(salesTax) + "\n"
  35. + "Invoice Total: " + currency.format(total) + "\n";
  36. System.out.println(message);
  37.  
  38. //see if the user wants to continue
  39. System.out.print("Continue? (y/n): ");
  40. choice = sc.next();
  41. System.out.println();
  42.  
  43. }
  44. // TODO code application logic here
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement