Untitled

 avatar
unknown
plain_text
a year ago
7.1 kB
8
Indexable
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.*;

public class DriverDetailsPage extends JFrame implements ActionListener {
    private Connection conn;
    private String username;
    private JTable driverDetailsTable;
    private JTable vehicleDetailsTable;
    private JTable violationsTable;
    private JButton payButton;
    private JButton logoutButton;
    private JLabel totalFinesLabel;

    public DriverDetailsPage(Connection conn, String username) {
        // Initialize the database connection and username
        this.conn = conn;
        this.username = username;

        // Initialize the UI components
        driverDetailsTable = new JTable();
        vehicleDetailsTable = new JTable();
        violationsTable = new JTable();
        payButton = new JButton("Pay");
        logoutButton = new JButton("Logout");
        totalFinesLabel = new JLabel("Total Fines Pending: 0.0");

        // Add action listeners to the buttons
        payButton.addActionListener(this);
        logoutButton.addActionListener(this);

        // Add the UI components to the form
        JPanel formPanel = new JPanel(new BorderLayout());
        JPanel driverDetailsPanel = new JPanel(new BorderLayout());
        driverDetailsPanel.setBorder(BorderFactory.createTitledBorder("Driver Details"));
        driverDetailsPanel.add(new JScrollPane(driverDetailsTable), BorderLayout.CENTER);
        formPanel.add(driverDetailsPanel, BorderLayout.NORTH);

        JPanel vehicleDetailsPanel = new JPanel(new BorderLayout());
        vehicleDetailsPanel.setBorder(BorderFactory.createTitledBorder("Vehicle Details"));
        vehicleDetailsPanel.add(new JScrollPane(vehicleDetailsTable), BorderLayout.CENTER);
        formPanel.add(vehicleDetailsPanel, BorderLayout.CENTER);

        JPanel violationsPanel = new JPanel(new BorderLayout());
        violationsPanel.setBorder(BorderFactory.createTitledBorder("Violations"));
        violationsPanel.add(new JScrollPane(violationsTable), BorderLayout.CENTER);
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        bottomPanel.add(payButton);
        bottomPanel.add(logoutButton);
        violationsPanel.add(bottomPanel, BorderLayout.SOUTH);
        violationsPanel.add(totalFinesLabel, BorderLayout.NORTH); // Display total fines at the top
        formPanel.add(violationsPanel, BorderLayout.SOUTH);
        add(formPanel);

        // Set the form properties
        setTitle("Driver Details");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        // Load driver and vehicle details
        loadDriverDetails();
        loadVehicleDetails();

        // Load violations associated with the driver
        loadViolations();

        // Update the total fines label
        updateTotalFinesLabel();
    }

    private void loadDriverDetails() {
        try {
            Statement stmt = conn.createStatement();
            String query = "SELECT driver_id, full_name, license_number, birth_date, phone_number, address FROM Driver WHERE username = '" + username + "'";
            ResultSet rs = stmt.executeQuery(query);
            driverDetailsTable.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void loadVehicleDetails() {
        try {
            Statement stmt = conn.createStatement();
            String query = "SELECT vehicle_id, vehicle_number, vehicle_name, vehicle_model, registration_year, color, rto_office FROM Vehicle WHERE owner_id = (SELECT driver_id FROM Driver WHERE username = '" + username + "')";
            ResultSet rs = stmt.executeQuery(query);
            vehicleDetailsTable.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void loadViolations() {
        try {
            Statement stmt = conn.createStatement();
            String query = "SELECT violation_id, violation_type, violation_date, fine_amount FROM TrafficViolation INNER JOIN Vehicle ON TrafficViolation.vehicle_id = Vehicle.vehicle_id WHERE Vehicle.owner_id = (SELECT driver_id FROM Driver WHERE username = '" + username + "')";
            ResultSet rs = stmt.executeQuery(query);
            violationsTable.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void updateTotalFinesLabel() {
        try {
            Statement stmt = conn.createStatement();
            String query = "SELECT SUM(fine_amount) AS total_fines FROM TrafficViolation INNER JOIN Vehicle ON TrafficViolation.vehicle_id = Vehicle.vehicle_id WHERE Vehicle.owner_id = (SELECT driver_id FROM Driver WHERE username = '" + username + "')";
            ResultSet rs = stmt.executeQuery(query);
            double totalFines = 0.0;
            if (rs.next()) {
                totalFines = rs.getDouble("total_fines");
            }
            totalFinesLabel.setText("Total Fines Pending: " + totalFines);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == payButton) {
            int selectedRow = violationsTable.getSelectedRow();
            if (selectedRow != -1) {
                int violationId = (int) violationsTable.getValueAt(selectedRow, 0);

                try {
                    Statement stmt = conn.createStatement();
                    // Implement the logic to process the payment and remove the violation entry from the database.
                    String deleteQuery = "DELETE FROM TrafficViolation WHERE violation_id = " + violationId;
                    stmt.executeUpdate(deleteQuery);

                    // Refresh the violations table to reflect the changes
                    loadViolations();

                    // After payment, update the total fines label
                    updateTotalFinesLabel();

                    JOptionPane.showMessageDialog(this, "Payment processed successfully.");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(this, "Error processing payment.");
                }
            } else {
                JOptionPane.showMessageDialog(this, "Select a violation to pay.");
            }
        } else if (e.getSource() == logoutButton) {
            // Close the current window and return to the LoginPage
            this.dispose(); // Close the current window
            new LoginPage(conn).setVisible(true); // Open the LoginPage
        }
    }

    public static void main(String[] args) {
        // You can include the main method if needed, but it's not typically used for this class.
    }
}
Editor is loading...