Question

In: Computer Science

public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...

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

Solutions

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


Related Solutions

import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT