userinterface.EnvironmentRole.PollutionCheckJPanel.java Source code

Java tutorial

Introduction

Here is the source code for userinterface.EnvironmentRole.PollutionCheckJPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package userinterface.EnvironmentRole;

import Business.Employee.Employee;
import Business.Enterprise.Enterprise;
import Business.Fine.Fine;
import Business.Organization.CarOwnerOrganization;
import Business.Organization.EnvironmentOrganization;
import Business.Organization.Organization;
import Business.UserAccount.UserAccount;
import Business.WorkQueue.EnvironmentWorkRequest;
import Business.WorkQueue.WorkRequest;
import java.awt.CardLayout;
import java.awt.Component;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Sarthak
 */
public class PollutionCheckJPanel extends javax.swing.JPanel {

    JPanel userProcessContainer;
    UserAccount account;
    Enterprise enterprise;
    EnvironmentOrganization environmentOrganization;
    CarOwnerOrganization carOwnerOrganization;

    PollutionCheckJPanel(JPanel userProcessContainer, UserAccount account, Enterprise enterprise,
            EnvironmentOrganization environmentOrganization) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.account = account;
        this.enterprise = enterprise;
        this.environmentOrganization = environmentOrganization;
        populateTable();
    }

    public void populateTable() {
        int rowCount = tableCarOwners.getRowCount();
        DefaultTableModel model = (DefaultTableModel) tableCarOwners.getModel();
        for (int i = rowCount - 1; i >= 0; i--) {
            model.removeRow(i);
        }
        for (WorkRequest request : environmentOrganization.getWorkQueue().getWorkRequestList()) {
            if (account == request.getReceiver() || request.getReceiver() == null) {
                Object[] row = new Object[5];
                row[0] = ((EnvironmentWorkRequest) request).getCarListing().getCarOwner().getName();
                row[1] = ((EnvironmentWorkRequest) request).getCarListing();

                String expectedPattern = "MM/dd/yyyy";
                SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
                Date userInput = ((EnvironmentWorkRequest) request).getCarListing().getLastPollution(); // MM/DD/YYYY
                if (userInput != null) {
                    String date = formatter.format(userInput);
                    row[2] = date;
                } else {
                    row[2] = null;
                }

                row[3] = ((EnvironmentWorkRequest) request);

                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                Calendar c = Calendar.getInstance();
                c.setTime(((EnvironmentWorkRequest) request).getCarListing().getLastPollution()); // Now use today date.
                c.add(Calendar.DATE, 90); // Adding 90 days
                String output = sdf.format(c.getTime());
                //                System.out.println(output);

                row[4] = output;

                model.addRow(row);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableCarOwners = new javax.swing.JTable();
        btnBack = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        textFine = new javax.swing.JTextField();
        btnSetFine = new javax.swing.JButton();
        btnCheckReport = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnSincereCarOwner = new javax.swing.JButton();

        setBackground(new java.awt.Color(128, 203, 196));

        tableCarOwners.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Car Owner", "Car Model", "Last Pollution check", "Registered", "Expiry Date" }) {
            boolean[] canEdit = new boolean[] { false, false, false, false, false };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableCarOwners);

        btnBack.setText("<< Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        jLabel1.setText("Fine:");

        btnSetFine.setText("Set Fine");
        btnSetFine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetFineActionPerformed(evt);
            }
        });

        btnCheckReport.setText("Generate Report");
        btnCheckReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckReportActionPerformed(evt);
            }
        });

        jLabel2.setText("Check individual fine over time report:");

        jLabel3.setText("Most sincere Car Owner:");

        btnSincereCarOwner.setText("Generate Report");
        btnSincereCarOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSincereCarOwnerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 770,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnBack)
                                .addGroup(layout.createSequentialGroup().addGap(15, 15, 15).addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(textFine, javax.swing.GroupLayout.PREFERRED_SIZE, 145,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(btnSetFine))))
                        .addGroup(layout.createSequentialGroup().addGap(23, 23, 23)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2).addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(btnCheckReport, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnSincereCarOwner, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addContainerGap(182, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(60, 60, 60)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(textFine, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnSetFine))
                        .addGap(31, 31, 31)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(btnCheckReport))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3).addComponent(btnSincereCarOwner))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(btnBack).addGap(81, 81, 81)));
    }// </editor-fold>//GEN-END:initComponents

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        userProcessContainer.remove(this);
        Component[] componentArray = userProcessContainer.getComponents();
        Component component = componentArray[componentArray.length - 1];
        CardLayout layout = (CardLayout) userProcessContainer.getLayout();
        layout.previous(userProcessContainer);
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnSetFineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetFineActionPerformed
        int selectedRow = tableCarOwners.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row from the table first", "Warning",
                    JOptionPane.WARNING_MESSAGE);
            return;
        } else {
            WorkRequest workRequest = (WorkRequest) tableCarOwners.getValueAt(selectedRow, 3);
            try {
                int oldFine = ((EnvironmentWorkRequest) workRequest).getFine();
                ((EnvironmentWorkRequest) workRequest).setFine(oldFine + Integer.parseInt(textFine.getText()));

                //            ArrayList<Employee,int> sdfdsf;

                Fine fine = new Fine();
                Date date = new Date();
                fine.setDate(date);
                fine.setFineIncurred(Integer.parseInt(textFine.getText()));
                fine.setUserAccount(((EnvironmentWorkRequest) workRequest).getSender());

                environmentOrganization.getFineHistory().getFineIncurredhistory().add(fine);

                JOptionPane.showMessageDialog(null, "FIne Charged", "Success", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Enter a valid number", "Erroe", JOptionPane.ERROR_MESSAGE);
            }

        }

    }//GEN-LAST:event_btnSetFineActionPerformed

    private void btnCheckReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckReportActionPerformed
        int selectedRow = tableCarOwners.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row from the table first", "Warning",
                    JOptionPane.WARNING_MESSAGE);
            return;
        } else {

            WorkRequest workRequest = (WorkRequest) tableCarOwners.getValueAt(selectedRow, 3);
            UserAccount userAccount = workRequest.getSender();

            XYSeries series = new XYSeries("XYGraph");
            int i = 0;
            for (Fine fine : environmentOrganization.getFineHistory().getFineIncurredhistory()) {
                if (userAccount == fine.getUserAccount()) {
                    series.add(fine.getFineIncurred(), i); //fine.getDate().getTime()
                    i++;
                }
            }

            XYSeriesCollection dataset = new XYSeriesCollection();
            dataset.addSeries(series);

            JFrame frame = new JFrame("Charts");

            frame.setSize(600, 400);
            //            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            JFreeChart chart = ChartFactory.createXYLineChart("Test Chart", "Time", "Fine", dataset,
                    PlotOrientation.VERTICAL, true, true, false);

            ChartPanel cp = new ChartPanel(chart);

            frame.getContentPane().add(cp);
        }

    }//GEN-LAST:event_btnCheckReportActionPerformed

    private void btnSincereCarOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSincereCarOwnerActionPerformed
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        //        int f = 0;

        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof CarOwnerOrganization) {
                for (UserAccount userAccount : ((CarOwnerOrganization) organization).getUserAccountDirectory()
                        .getUserAccountList()) {
                    int f = 0;
                    for (WorkRequest request : environmentOrganization.getWorkQueue().getWorkRequestList()) {
                        if (request.getSender() == userAccount) {
                            f = f + ((EnvironmentWorkRequest) request).getFine();
                        }
                    }
                    pieDataset.setValue(userAccount.getEmployee().getName(), f);
                }
            }
        }

        JFrame frame = new JFrame("PieChart");
        frame.setSize(600, 400);
        frame.setVisible(true);

        JFreeChart pieChart = ChartFactory.createPieChart("Best Car Owner in terms of fine charged", // Title
                pieDataset, // Dataset
                true, // Show legend
                true, // Use tooltips
                false // Configure chart to generate URLs?
        );

        ChartPanel cp1 = new ChartPanel(pieChart);

        frame.getContentPane().add(cp1);

    }//GEN-LAST:event_btnSincereCarOwnerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnCheckReport;
    private javax.swing.JButton btnSetFine;
    private javax.swing.JButton btnSincereCarOwner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tableCarOwners;
    private javax.swing.JTextField textFine;
    // End of variables declaration//GEN-END:variables
}