public class salesreceipt public static void mainString arg
public class sales_receipt
{ public static void main(String[] args)
{ //declare varables final double tax_rate = 0.05;
//tax rate String cashier_name = \"xxx\"; //sales person
String article1, article2;
//item name for each purchased int quantity1, quantity2;
//number of quantities for each item double unit_cost1, unit_cost2;
//unit price for each item double price1, price2;
//calculates amounts of money for each item. double sub_total;
//total price of two items without tax double tax;
//amount of sales tax double total;
//total price of two items including tax double cash;
//Cash amount from user double change;
/cash amount to the user
//create Scanner object for keyboard input
//get first item
//get second item
//get amount of money tendered...
//Calculate amounts for sales receipt
//Create a DecimalFormat object
//display sales receipt System.out.println(\"H O M E D E C O R S T O R E S\"); System.out.println(\"R I C H M O N D O U T L E T M A L L\");
0System.out.println(\" THANK YOU - HAVE A NICE DAY \"); System.out.println(); } }
Create Scanner object and DecimalFormat object
using java
1. Import Scanner class and create scanner object to read user input. Write a program to read input from keyboard for the first item with following: 2. Input name of the first item purchased and save it to local variable “article1”. 3. Input quantity purchased and save it to a local variable “quantity1”. 4. Input unit price and save it to a local variable “unit_cost1”. Write a program to read input from keyboard for the second item with following: 5. Input name of the second item purchased and save it to a local variable “article2”. 6. Input quantity purchased and save it to a local variable “quantity2”. 7. Input unit price and save it to a local variable “unit_cost2”. 8. Input amount of cash received from a client and save it to a local variable “cash”. Calculate amounts for sales receipts 9. Calculate the amount of the first item by multiplying quantity and unit cost, and save it to a local variable “price1”. 10. Calculate the amount of the second item by multiplying quantity and unit cost, and save it to a local variable “price2”. 11. Calculate the sum amount of items purchased, and save it to a local variable “sub_total”. 12. Calculate the sales tax of items purchased, and save it to a local variable “tax”. 13. Calculate the total price of items including tax, and save it to a local variable “total”. 14. Calculate the change amount, and save it to local variable “change”. Import DecimalFormat class and create DecimalFormat object to format floating points. Print sales receipt based on the calculation 16. Now, you need to represents the sales receipt on the screen with following format. –use escape sequences for formatting sales receipt
Solution
package salesreceipt;
 import java.util.Scanner;
 import java.text.DecimalFormat;
/**
 *
 * @author
**/
 public class SalesReceipt
 {
/**
 * @param args the command line arguments
 */
 public static void main(String[] args)
 {
 //declare varables
 final double tax_rate = 0.05; //tax rate
 String cashier_name = \"xxx\"; //sales person
 String article1, article2; //item name for each purchased
 int quantity1, quantity2; //number of quantities for each item
 double unit_cost1, unit_cost2; //unit price for each item
 double price1, price2; //calculates amounts of money for each item.
 double sub_total; //total price of two items without tax
 double tax; //amount of sales tax
 double total; //total price of two items including tax
 double cash; //Cash amount from user
 double change; //cash amount to the user
   
 //create Scanner object for keyboard input
 Scanner sc=new Scanner(System.in);
 //get first item
 System.out.print(\"Enter the name of the first item:\");
 article1 = sc.next();   
 System.out.print(\"Enter the number (quantity) of \" + article1 + \"(s) required:\");
 quantity1 = sc.nextInt();
 System.out.print(\"Enter the unit-price of the \" + article1 + \":\");
 unit_cost1 = sc.nextDouble();
 price1 = quantity1 * unit_cost1; //compute the first article total price
   
 //get second item
 System.out.print(\"Enter the name of the second item:\");
 article2 = sc.next();   
 System.out.print(\"Enter the number (quantity) of the \" + article2 + \"(s) required:\");
 quantity2 = sc.nextInt();
 System.out.print(\"Enter the unit-price of the \" + article2 + \":\");
 unit_cost2 = sc.nextDouble();
 price2 = quantity2 * unit_cost2; //compute the second article total price
   
 sub_total = price1 + price2; //Sum of the amounts of two items purchased
 tax = sub_total * tax_rate; //Tax amount
 total = sub_total + tax; //total price of two items including tax
   
 System.out.print(\"Enter the amount tendered (must be greater than \" + total + \"):\");
 cash = sc.nextDouble();//get amount of money tendered...
 change = cash - total;
   
   
 //Calculate amounts for sales receipt
 //Create a DecimalFormat object
 DecimalFormat decimalFormat = new DecimalFormat(\"###,###.##\");
 //display sales receipt
 System.out.println(\"H O M E D E C O R S T O R E S\");
 System.out.println(\"R I C H M O N D O U T L E T M A L L\");
 System.out.println(\"Item\\t\\tUnit_price\\t\\tQuantity\\t\\tPrice\");
  System.out.println(article1+\"\\t\\t\" + decimalFormat.format(unit_cost1) + \"\\t\\t\\t\" + quantity1 + \"\\t\\t\" + decimalFormat.format(price1));
 System.out.println(article2+\"\\t\\t\" + decimalFormat.format(unit_cost2) + \"\\t\\t\\t\" + quantity2 + \"\\t\\t\" + decimalFormat.format(price2));
 System.out.println(\"----------------------------------------------------------------------------------------------------------------\");
  System.out.println(\"Total price of items\\t\\t\\t\\t\\t\\t\" + decimalFormat.format(sub_total));
 System.out.println(\"Sales Tax\\t\\t\\t\\t\\t\\t\\t\" + decimalFormat.format(tax));
 System.out.println(\"Total cost including tax\\t\\t\\t\\t\\t\" + decimalFormat.format(total));
 System.out.println(\"----------------------------------------------------------------------------------------------------------------\");
  System.out.println(\"Tendered amount: \" + decimalFormat.format(cash));
 System.out.println(\"Change to be paid: \" + decimalFormat.format(change));
   
 System.out.println(\" THANK YOU - HAVE A NICE DAY \");
System.out.println();
 }
   
 }
![public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \ public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \](/WebImages/39/public-class-salesreceipt-public-static-void-mainstring-arg-1120972-1761596538-0.webp)
![public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \ public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \](/WebImages/39/public-class-salesreceipt-public-static-void-mainstring-arg-1120972-1761596538-1.webp)
![public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \ public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = \](/WebImages/39/public-class-salesreceipt-public-static-void-mainstring-arg-1120972-1761596538-2.webp)
