Sure! Here’s a Billing System that incorporates all the discussed concepts in Java, including classes, inheritance, polymorphism, exception handling, packages, generics, and file handling.
This example will simulate a billing system for a store, where we have different types of products, customer details, and billing processes.
com.store.billing
: Package for the billing system.Product
, Bill
,
Customer
, BillingSystem
, and
Utility
.Product
classpackage com.store.billing;
public class Product {
private String name;
private double price;
private int quantity;
public Product(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 getTotalPrice() {
return price * quantity;
}
}
Customer
classpackage com.store.billing;
public class Customer {
private String name;
private String contact;
private String address;
public Customer(String name, String contact, String address) {
this.name = name;
this.contact = contact;
this.address = address;
}
public String getName() {
return name;
}
public String getContact() {
return contact;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Customer{name='" + name + "', contact='" + contact + "', address='" + address + "'}";
}
}
Bill
class (Using Generics)package com.store.billing;
import java.util.List;
public class Bill<T extends Product> {
private Customer customer;
private List<T> products;
private double totalAmount;
public Bill(Customer customer, List<T> products) {
this.customer = customer;
this.products = products;
calculateTotalAmount();
}
public Customer getCustomer() {
return customer;
}
public List<T> getProducts() {
return products;
}
public double getTotalAmount() {
return totalAmount;
}
private void calculateTotalAmount() {
= 0;
totalAmount for (T product : products) {
+= product.getTotalPrice();
totalAmount }
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
.append("Bill Details:\n");
sb.append("Customer: ").append(customer).append("\n");
sb.append("Products: \n");
sbfor (T product : products) {
.append(product.getName()).append(" - Quantity: ").append(product.getQuantity()).append(" - Total: ").append(product.getTotalPrice()).append("\n");
sb}
.append("Total Amount: ").append(totalAmount).append("\n");
sbreturn sb.toString();
}
}
BillingSystem
classpackage com.store.billing;
import java.io.*;
import java.util.*;
public class BillingSystem {
private List<Bill<?>> bills = new ArrayList<>();
public void addBill(Bill<?> bill) {
.add(bill);
bills}
public void printAllBills() {
for (Bill<?> bill : bills) {
System.out.println(bill);
}
}
public void saveBillsToFile(String filename) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
for (Bill<?> bill : bills) {
.write(bill.toString());
writer.write("\n\n");
writer}
}
}
public void readBillsFromFile(String filename) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
}
Utility
class (For Exception Handling)package com.store.billing;
public class Utility {
public static void validateInput(String input) throws IllegalArgumentException {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input cannot be empty or null.");
}
}
public static void validatePrice(double price) throws IllegalArgumentException {
if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative.");
}
}
public static void validateQuantity(int quantity) throws IllegalArgumentException {
if (quantity <= 0) {
throw new IllegalArgumentException("Quantity must be greater than zero.");
}
}
}
Main
class to run the programpackage com.store.billing;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) {
try {
// Creating products
= new Product("Apple", 1.2, 10);
Product apple = new Product("Banana", 0.8, 20);
Product banana = new Product("Orange", 1.5, 5);
Product orange
// Creating a customer
= new Customer("John Doe", "1234567890", "123 Main St");
Customer customer
// Creating a bill for the customer
List<Product> productList = new ArrayList<>();
.add(apple);
productList.add(banana);
productList.add(orange);
productList
<Product> bill = new Bill<>(customer, productList);
Bill
// Creating a billing system and adding the bill
= new BillingSystem();
BillingSystem billingSystem .addBill(bill);
billingSystem
// Printing all bills
.printAllBills();
billingSystem
// Saving bills to a file
.saveBillsToFile("bills.txt");
billingSystem
// Reading bills from the file
System.out.println("\nReading from file:");
.readBillsFromFile("bills.txt");
billingSystem
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error while handling file: " + e.getMessage());
}
}
}
try-catch
.Product
class into different
product types (e.g., Electronics
,
Groceries
).Bill
class uses
generics to handle different types of products, demonstrating
polymorphism.Bill
class is designed
to be generic, so it can handle any type of Product
or its
subclasses.Utility
class
handles invalid inputs, and the main program catches exceptions related
to file handling and input validation.BufferedWriter
and
BufferedReader
.This program integrates all the concepts discussed so far in Java to build a functional and organized billing system! Let me know if you’d like to add more features or modify it further. 😊