|
| 1 | + |
| 2 | + import java.util.ArrayList; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Scanner; |
| 5 | +import java.text.SimpleDateFormat; |
| 6 | +import java.util.Date; |
| 7 | +import java.util.Calendar; |
| 8 | +class Product |
| 9 | + { |
| 10 | + // properties |
| 11 | + private String id; |
| 12 | + private String pname; |
| 13 | + private int qty; |
| 14 | + private double price; |
| 15 | + private double totalPrice; |
| 16 | + |
| 17 | + // constructor |
| 18 | + Product(String id, String pname, int qty, double price, double totalPrice) |
| 19 | + { |
| 20 | + this.id=id; |
| 21 | + this.pname = pname; |
| 22 | + this.qty = qty; |
| 23 | + this.price = price; |
| 24 | + this.totalPrice = totalPrice; |
| 25 | + } |
| 26 | + // getter methods |
| 27 | + public String getId() |
| 28 | + { |
| 29 | + return id; |
| 30 | + } |
| 31 | + public String getPname() |
| 32 | + { |
| 33 | + return pname; |
| 34 | + } |
| 35 | + public int getQty() |
| 36 | + { |
| 37 | + return qty; |
| 38 | + } |
| 39 | + public double getPrice() |
| 40 | + { |
| 41 | + return price; |
| 42 | + } |
| 43 | + public double getTotalPrice() |
| 44 | + { |
| 45 | + return totalPrice; |
| 46 | + } |
| 47 | + //displayFormat |
| 48 | + public static void displayFormat() |
| 49 | + { |
| 50 | + System.out.format("-----------------------------------------------------------------------------------------------------------------------------------"); |
| 51 | + System.out.print("\nProduct ID \t\tName\t\tQuantity\t\tRate \t\t\t\tTotal Price\n"); |
| 52 | + System.out.format("-----------------------------------------------------------------------------------------------------------------------------------\n"); |
| 53 | + } |
| 54 | + |
| 55 | + // display |
| 56 | + public void display() |
| 57 | + { |
| 58 | + System.out.format(" %-9s %-9s %5d %9.2f %14.2f\n" ,id, pname, qty, price, totalPrice); |
| 59 | + } |
| 60 | + } |
| 61 | + public class Shopping_bill |
| 62 | + { |
| 63 | + public static void main(String args[]) |
| 64 | + { |
| 65 | + // variables |
| 66 | + String id = null; |
| 67 | + String productName = null; |
| 68 | + int quantity = 0; |
| 69 | + double price = 0.0; |
| 70 | + double totalPrice = 0.0; |
| 71 | + double overAllPrice = 0.0; |
| 72 | + double cgst, sgst, subtotal=0.0, discount=0.0; |
| 73 | + char choice = '\0'; |
| 74 | + System.out.println("\t\t\t\t--------------------Invoice-----------------"); |
| 75 | + System.out.println("\t\t\t\t\t "+" "+"Metro Mart Grocery Shop"); |
| 76 | + System.out.println("\t\t\t\t\t3/98 Mecrobertganj New Mumbai"); |
| 77 | + System.out.println("\t\t\t\t\t" +" " +"Opposite Metro Walk"); |
| 78 | + System.out.println("GSTIN: 03AWBPP8756K592"+"\t\t\t\t\t\t\tContact: (+91) 9988776655"); |
| 79 | + //format of date and time |
| 80 | + SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); |
| 81 | + Date date = new Date(); |
| 82 | + Calendar calendar = Calendar.getInstance(); |
| 83 | + String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; |
| 84 | + //prints current date and time |
| 85 | + System.out.println("Date: "+formatter.format(date)+" "+days[calendar.get(Calendar.DAY_OF_WEEK) - 1]+"\t\t\t\t\t\t (+91) 9998887770"); |
| 86 | + Scanner scan = new Scanner(System.in); |
| 87 | + System.out.print("Enter Customer Name: "); |
| 88 | + String customername=scan.nextLine(); |
| 89 | + //create Scanner class object |
| 90 | + //creating an ArrayList to store the product |
| 91 | + List<Product> product = new ArrayList<Product>(); |
| 92 | + do |
| 93 | + { |
| 94 | + // read input values |
| 95 | + System.out.println("Enter the product details: "); |
| 96 | + System.out.print("Product ID: "); |
| 97 | + id = scan.nextLine(); |
| 98 | + System.out.print("Product Name: "); |
| 99 | + productName = scan.nextLine(); |
| 100 | + System.out.print("Quantity: "); |
| 101 | + quantity = scan.nextInt(); |
| 102 | + System.out.print("Price (per unit): "); |
| 103 | + price = scan.nextDouble(); |
| 104 | + //calculate total price for a particular product |
| 105 | + totalPrice = price * quantity; |
| 106 | + //calculates overall price |
| 107 | + overAllPrice = overAllPrice + totalPrice; |
| 108 | + //creates Product class object and add it to the List |
| 109 | + product.add( new Product(id, productName, quantity, price, totalPrice) ); |
| 110 | + // ask for continue shopping? |
| 111 | + System.out.print("Want to add more items? (y or n): "); |
| 112 | + //reads a character Y or N |
| 113 | + choice = scan.next().charAt(0); |
| 114 | + //read remaining characters, don't store (no use) |
| 115 | + scan.nextLine(); |
| 116 | + } |
| 117 | + while (choice == 'y' || choice == 'Y'); |
| 118 | + //display all product with its properties |
| 119 | + Product.displayFormat(); |
| 120 | + for (Product p : product) |
| 121 | + { |
| 122 | + p.display(); |
| 123 | + } |
| 124 | + //price calculation |
| 125 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\tTotal Amount (Rs.) " +overAllPrice); |
| 126 | + //calculating discount |
| 127 | + discount = overAllPrice*2/100; |
| 128 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\t Discount (Rs.) " +discount); |
| 129 | + //total amount after discount |
| 130 | + subtotal = overAllPrice-discount; |
| 131 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\t Subtotal "+subtotal); |
| 132 | + //calculating tax |
| 133 | + sgst=overAllPrice*12/100; |
| 134 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\t SGST (%) "+sgst); |
| 135 | + cgst=overAllPrice*12/100; |
| 136 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\t CGST (%) "+cgst); |
| 137 | + //calculating amount to be paid by buyer |
| 138 | + System.out.println("\n\t\t\t\t\t\t\t\t\t\t Invoice Total " +(subtotal+cgst+sgst)); |
| 139 | + System.out.println("\t\t\t\t----------------Thank You for Shopping!!-----------------"); |
| 140 | + System.out.println("\t\t\t\t Visit Again"); |
| 141 | + // close Scanner |
| 142 | + scan.close(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
0 commit comments