Creating Invoice of a Shop in Java

Problem:

Assume you own a grocery shop where clients always buy 3 items (all prices are in
dollars). To facilitate the billing process you write a program that reads the item
names and prices and generates an invoice. For example, if a client buys Chocolate
(at $0.5), Gum (at $1), Water (at $0.5), the program output should look as follow:
Please enter the first item name:
Chocolate
Please enter the first item price:
1.5
Please enter the second item name:
Gum
Please enter the second item price:
1.5
Please enter the third item name:
Water
Please enter the third item price:
1.5
Invoice is:
Chocolate $1.5
Gum $1.5
Water $1.5
Total $4.5
Note that all prices are in dollars and you should use number formats to generate the invoice (do not use string concatenation to add the dollar sign).

Solution:

/**
 * @(#)Problem1.java
 *
 *
 * @author 
 * @version 1.00 2012/10/16
 */
import java.util.Scanner;
//import java.text.NumberFormat;

public class ProblemC
{
 public  static void main (String args[])
 {
  Scanner scan = new Scanner(System.in);
  String name1, name2, name3;
  double price1, price2, price3, total;
  
  System.out.println("Please enter first item name:");
  name1 = scan.next();
  
  System.out.println("Please enter first item price:");
  price1 = scan.nextDouble();
  
  System.out.println("Please enter second item name:");
  name2 = scan.next();
  
  System.out.println("Please enter second item price:");
  price2 = scan.nextDouble();
  
  System.out.println("Please enter third item name:");
  name3 = scan.next();
  
  System.out.println("Please enter third item price:");
  price3 = scan.nextDouble();
  
  total = price1 + price2 + price3;
 
    //NumberFormat fmt = NumberFormat.getCurrencyInstance();
  
     System.out.println("Invoice details are:\n");
       System.out.println(name1 + " " +"$"+price1);
     System.out.println(name2 + " " + "$"+price2);
  System.out.println(name3 + " " + "$"+price3);
  System.out.println("Total: " + "$"+price4);
 }
}

No comments:

Post a Comment