Java Billing example
import java.util.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Item {
private String name;
private double price;
private int quantity;
public Item(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public double getSubtotal() {
return price * quantity;
}
}
class BillingProgram {
private List<Item> items;
private String shopName;
private String billNumber;
private LocalDateTime billDate;
private double discountPercent;
private double taxPercent;
public BillingProgram(String shopName, String billNumber, double discountPercent, double taxPercent) {
this.items = new ArrayList<>();
this.shopName = shopName;
this.billNumber = billNumber;
this.billDate = LocalDateTime.now();
this.discountPercent = discountPercent;
this.taxPercent = taxPercent;
}
public void addItem(String name, double price, int quantity) {
items.add(new Item(name, price, quantity));
}
public void removeItem(String name) {
items.removeIf(item -> item.getName().equalsIgnoreCase(name));
}
public double calculateSubtotal() {
return items.stream().mapToDouble(Item::getSubtotal).sum();
}
public double calculateDiscount() {
return calculateSubtotal() * (discountPercent / 100.0);
}
public double calculateAfterDiscount() {
return calculateSubtotal() - calculateDiscount();
}
public double calculateTax() {
return calculateAfterDiscount() * (taxPercent / 100.0);
}
public double calculateFinalAmount() {
return calculateAfterDiscount() + calculateTax();
}
public void printBill() {
if (items.isEmpty()) {
System.out.println("\n⚠ No items in bill!");
return;
}
DecimalFormat df = new DecimalFormat("0.00");
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// Header Section
System.out.println("\n" + "═".repeat(80));
System.out.println(centerText(shopName, 80));
System.out.println(centerText("CASH BILL", 80));
System.out.println("═".repeat(80));
// Bill Details
System.out.println("Bill No: " + billNumber +
String.format("%50s", "Date: " + billDate.format(dateFormatter)) +
String.format("%15s", "Time: " + billDate.format(timeFormatter)));
System.out.println("─".repeat(80));
// Table Header
System.out.println(String.format("%-35s %12s %10s %18s",
"ITEM", "PRICE", "QTY", "SUBTOTAL"));
System.out.println("─".repeat(80));
// Items
for (Item item : items) {
System.out.println(String.format("%-35s %12s %10d %18s",
truncateString(item.getName(), 35),
"₹" + df.format(item.getPrice()),
item.getQuantity(),
"₹" + df.format(item.getSubtotal())));
}
System.out.println("─".repeat(80));
// Summary Section
double subtotal = calculateSubtotal();
double discount = calculateDiscount();
double afterDiscount = calculateAfterDiscount();
double tax = calculateTax();
double finalAmount = calculateFinalAmount();
System.out.println(String.format("%-48s %32s", "Subtotal:", "₹" + df.format(subtotal)));
if (discountPercent > 0) {
System.out.println(String.format("%-48s %32s",
"Discount (" + discountPercent + "%):",
"-₹" + df.format(discount)));
}
System.out.println(String.format("%-48s %32s",
"After Discount:",
"₹" + df.format(afterDiscount)));
if (taxPercent > 0) {
System.out.println(String.format("%-48s %32s",
"Tax (" + taxPercent + "%):",
"₹" + df.format(tax)));
}
System.out.println("═".repeat(80));
System.out.println(String.format("%-48s %32s",
"TOTAL AMOUNT:",
"₹" + df.format(finalAmount)));
System.out.println("═".repeat(80));
System.out.println("\n" + centerText("Thank you for your purchase!", 80));
System.out.println(centerText("Visit again soon!", 80));
System.out.println("═".repeat(80) + "\n");
}
public void viewItems() {
if (items.isEmpty()) {
System.out.println("\n⚠ No items in bill!");
return;
}
System.out.println("\n" + "─".repeat(60));
System.out.println(String.format("%-30s %12s %10s %10s",
"Item", "Price", "Qty", "Subtotal"));
System.out.println("─".repeat(60));
DecimalFormat df = new DecimalFormat("0.00");
for (Item item : items) {
System.out.println(String.format("%-30s %12s %10d %10s",
truncateString(item.getName(), 30),
"₹" + df.format(item.getPrice()),
item.getQuantity(),
"₹" + df.format(item.getSubtotal())));
}
System.out.println("─".repeat(60));
}
private String truncateString(String str, int length) {
if (str.length() > length) {
return str.substring(0, length - 3) + "...";
}
return str;
}
private String centerText(String text, int width) {
int padding = (width - text.length()) / 2;
return " ".repeat(Math.max(0, padding)) + text;
}
}
public class BillingApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Initialize billing system
System.out.print("Enter shop name: ");
String shopName = scanner.nextLine();
System.out.print("Enter bill number: ");
String billNumber = scanner.nextLine();
System.out.print("Enter discount percentage (0 for no discount): ");
double discount = scanner.nextDouble();
System.out.print("Enter tax percentage (0 for no tax): ");
double tax = scanner.nextDouble();
scanner.nextLine(); // Consume newline
BillingProgram billing = new BillingProgram(shopName, billNumber, discount, tax);
while (true) {
System.out.println("\n╔════════════════════════════════╗");
System.out.println("║ BILLING SYSTEM MENU ║");
System.out.println("╠════════════════════════════════╣");
System.out.println("║ 1. Add Item ║");
System.out.println("║ 2. Remove Item ║");
System.out.println("║ 3. View Items ║");
System.out.println("║ 4. Print Bill ║");
System.out.println("║ 5. Exit ║");
System.out.println("╚════════════════════════════════╝");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter item name: ");
String itemName = scanner.nextLine();
System.out.print("Enter price: ");
double price = scanner.nextDouble();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine();
billing.addItem(itemName, price, quantity);
System.out.println("✓ Item added successfully!");
break;
case 2:
System.out.print("Enter item name to remove: ");
String removeName = scanner.nextLine();
billing.removeItem(removeName);
System.out.println("✓ Item removed successfully!");
break;
case 3:
billing.viewItems();
break;
case 4:
billing.printBill();
break;
case 5:
System.out.println("Thank you for using Billing System!");
scanner.close();
return;
default:
System.out.println("✗ Invalid choice. Please try again.");
}
}
}
}
Comments
Post a Comment