BillingSystemMAin.
java
package billing;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.File;
public class BillingSystemMain extends JFrame {
private JTextField nameField, quantityField, priceField;
private JLabel productImageLabel;
private DefaultTableModel tableModel;
private JTable productTable;
private double totalAmount = 0.0;
private JLabel totalLabel;
// Sample login credentials (you can change or load them from a file/database)
private static final String USERNAME = "admin";
private static final String PASSWORD = "password";
public BillingSystemMain() {
setTitle("Enhanced Billing System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Input panel
JPanel inputPanel = new JPanel(new GridLayout(5, 2));
inputPanel.add(new JLabel("Product Name:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("Quantity:"));
quantityField = new JTextField();
inputPanel.add(quantityField);
inputPanel.add(new JLabel("Price:"));
priceField = new JTextField();
inputPanel.add(priceField);
inputPanel.add(new JLabel("Product Image:"));
productImageLabel = new JLabel();
productImageLabel.setHorizontalAlignment(JLabel.CENTER);
productImageLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
inputPanel.add(productImageLabel);
JButton uploadImageButton = new JButton("Upload Image");
uploadImageButton.addActionListener(e -> uploadImage());
inputPanel.add(uploadImageButton);
JButton addButton = new JButton("Add Product");
addButton.addActionListener(e -> addProduct());
inputPanel.add(addButton);
add(inputPanel, BorderLayout.NORTH);
// Table panel
tableModel = new DefaultTableModel(new Object[]{"Name", "Quantity", "Price", "Image Path"}, 0);
productTable = new JTable(tableModel);
add(new JScrollPane(productTable), BorderLayout.CENTER);
// Total panel
JPanel totalPanel = new JPanel(new BorderLayout());
totalLabel = new JLabel("Total: ₹0.0");
totalLabel.setFont(new Font("Arial", Font.BOLD, 16));
totalPanel.add(totalLabel, BorderLayout.WEST);
JButton generateBillButton = new JButton("Generate Bill");
generateBillButton.addActionListener(e -> generateBillPage());
totalPanel.add(generateBillButton, BorderLayout.EAST);
add(totalPanel, BorderLayout.SOUTH);
setVisible(true);
}
private void addProduct() {
String name = nameField.getText();
String quantityText = quantityField.getText();
String priceText = priceField.getText();
if (name.isEmpty() || quantityText.isEmpty() || priceText.isEmpty() || productImageLabel.getIcon()
== null) {
JOptionPane.showMessageDialog(this, "Please fill all fields and upload an image!", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
try {
int quantity = Integer.parseInt(quantityText);
double price = Double.parseDouble(priceText);
// Store the image path instead of JLabel
Icon icon = productImageLabel.getIcon();
String imagePath = ((ImageIcon) icon).getDescription(); // Store the file path
tableModel.addRow(new Object[]{name, quantity, price, imagePath});
totalAmount += quantity * price;
totalLabel.setText("Total: ₹" + totalAmount);
// Clear input fields
nameField.setText("");
quantityField.setText("");
priceField.setText("");
productImageLabel.setIcon(null);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Quantity and Price must be valid numbers!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void generateBillPage() {
JFrame billFrame = new JFrame("Bill - Enhanced Billing System");
billFrame.setSize(400, 500); // Adjusted size for the quote
billFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
billFrame.setLayout(new BorderLayout());
JTextArea billArea = new JTextArea();
billArea.setEditable(false);
billArea.setFont(new Font("Arial", Font.PLAIN, 14));
billArea.setBackground(Color.WHITE);
billArea.append("Enhanced Billing System\n\n");
billArea.append("=======================================\n");
billArea.append(String.format("%-20s %-10s %-10s\n", "Product", "Qty", "Price"));
billArea.append("=======================================\n");
for (int i = 0; i < tableModel.getRowCount(); i++) {
String name = (String) tableModel.getValueAt(i, 0);
int quantity = (int) tableModel.getValueAt(i, 1);
double price = (double) tableModel.getValueAt(i, 2);
String imagePath = (String) tableModel.getValueAt(i, 3); // Retrieve the image path
billArea.append(String.format("%-20s %-10d ₹%-10.2f\n", name, quantity, price));
billArea.append("Image: " + imagePath + "\n"); // Include the image path
}
billArea.append("=======================================\n");
billArea.append("Total Amount: ₹" + totalAmount + "\n");
billArea.append("=======================================\n");
// Adding motivational quote
billArea.append("\n\"Success is the sum of small efforts, repeated day in and day out.\"\n\n");
billFrame.add(new JScrollPane(billArea), BorderLayout.CENTER);
billFrame.setVisible(true);
}
private void uploadImage() {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
ImageIcon imageIcon = new ImageIcon(new
ImageIcon(file.getAbsolutePath()).getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH));
imageIcon.setDescription(file.getAbsolutePath()); // Store the file path in the icon description
productImageLabel.setIcon(imageIcon);
}
}
private boolean authenticate(String username, String password) {
return USERNAME.equals(username) && PASSWORD.equals(password);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// Display login dialog before showing the main billing system
boolean loggedIn = false;
while (!loggedIn) {
String username = JOptionPane.showInputDialog("Enter Username:");
String password = new String(JOptionPane.showInputDialog("Enter Password:"));
BillingSystemMain billingSystem = new BillingSystemMain();
if (billingSystem.authenticate(username, password)) {
loggedIn = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Login Error",
JOptionPane.ERROR_MESSAGE);
// If login fails, continue asking for credentials
}
}
});
}
}
DatabseConnection.java
package billing;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
public static Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/BillingSystemDB";
String username = "root";
String password = "root";
return DriverManager.getConnection(url, username, password);
}
}
LoginPage.java
package billing;
import javax.swing.*;
public class LoginPage {
public static boolean authenticate(String username, String password) {
return "admin".equals(username) && "password".equals(password);
}
public static JPanel createLoginForm() {
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.Y_AXIS));
JTextField usernameField = new JTextField(20);
JPasswordField passwordField = new JPasswordField(20);
loginPanel.add(new JLabel("Username:"));
loginPanel.add(usernameField);
loginPanel.add(new JLabel("Password:"));
loginPanel.add(passwordField);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(e -> {
if (authenticate(usernameField.getText(), new String(passwordField.getPassword()))) {
JOptionPane.showMessageDialog(null, "Login Successful!");
new BillingSystemMain(); // Open the main billing system
} else {
JOptionPane.showMessageDialog(null, "Invalid credentials. Try again.");
}
});
loginPanel.add(loginButton);
return loginPanel;
}
public static void main(String[] args) {
JFrame loginFrame = new JFrame("Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.add(createLoginForm());
loginFrame.pack();
loginFrame.setVisible(true);
}
}
TableImageRender.java
package billing;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.io.File;
public class TableImageRenderer implements TableCellRenderer {
private JLabel label;
private ImageIcon placeholder;
public TableImageRenderer() {
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
// Load the placeholder image (Make sure "placeholder.png" is in your project folder or provide the
full path)
placeholder = new ImageIcon(new ImageIcon("placeholder.png").getImage().getScaledInstance(50,
50, Image.SCALE_SMOOTH));
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (value instanceof String) {
String imagePath = (String) value;
File file = new File(imagePath);
if (file.exists() && !file.isDirectory()) {
try {
ImageIcon icon = new ImageIcon(new
ImageIcon(imagePath).getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
label.setIcon(icon);
label.setText(null); // Clear text if the image is loaded successfully
} catch (Exception e) {
label.setIcon(placeholder); // Fallback to placeholder on error
label.setText(null);
}
} else {
label.setIcon(placeholder); // Fallback to placeholder for missing/invalid paths
label.setText(null);
}
} else {
label.setIcon(placeholder); // Fallback to placeholder for null or non-string values
label.setText(null);
}
// Highlight selection if needed
if (isSelected) {
label.setBackground(table.getSelectionBackground());
label.setOpaque(true);
} else {
label.setBackground(table.getBackground());
label.setOpaque(false);
}
return label;
}
}