UserInterface.VaccineManufacturer.TrackVaccineWastage.java Source code

Java tutorial

Introduction

Here is the source code for UserInterface.VaccineManufacturer.TrackVaccineWastage.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.VaccineManufacturer;

import Business.Enterprise.CDCEnterprise;
import Business.Enterprise.VaccineManufacturerEnterprise;
import Business.Vaccine.Vaccine;
import java.awt.CardLayout;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author Rishika Idnani
 */
public class TrackVaccineWastage extends javax.swing.JPanel {

    /**
     * Creates new form TrackVaccineWastage
     */
    private JPanel userProcessContainer;
    private VaccineManufacturerEnterprise enterprise;
    private CDCEnterprise cdcEnterprise;

    public TrackVaccineWastage(JPanel userProcessContainer, VaccineManufacturerEnterprise enterprise,
            CDCEnterprise cdcEnterprise) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.enterprise = enterprise;
        this.cdcEnterprise = cdcEnterprise;

        populateVaccineTable();
    }

    public void populateVaccineTable() {
        DefaultTableModel model = (DefaultTableModel) vaccineJTable.getModel();
        model.setRowCount(0);

        for (Vaccine vaccine : enterprise.getVaccineCatalog().getVaccineList()) {
            Object row[] = new Object[2];
            row[0] = vaccine;
            row[1] = vaccine.getDisease();
            model.addRow(row);
        }
    }

    public void populateWastageTable(Vaccine vaccine) {
        DefaultTableModel model = (DefaultTableModel) wastageJTable.getModel();
        model.setRowCount(0);
        int totalWastage = 0;

        for (Vaccine vaccineForSale : enterprise.getVaccineInventory().getVaccineInventoryList()) {
            if (vaccineForSale.getBatchAvailability() > 0 && vaccineForSale.getExpiryDate().before(new Date())
                    && vaccineForSale.getName().equals(vaccine.getName())) {
                Object row[] = new Object[2];
                row[0] = vaccineForSale.getManufacturingBatch();
                totalWastage = totalWastage + vaccineForSale.getBatchAvailability();
                row[1] = vaccineForSale.getBatchAvailability();
                model.addRow(row);
            }
        }
        totalWastageJTextField.setText(String.valueOf(totalWastage));
    }

    /**
     * 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() {

        panelDescriptionJLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        vaccineJTable = new javax.swing.JTable();
        viewJLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        wastageJTable = new javax.swing.JTable();
        backJButton = new javax.swing.JButton();
        clickJButton = new javax.swing.JButton();
        listJLabel = new javax.swing.JLabel();
        totalWastageJLabel = new javax.swing.JLabel();
        totalWastageJTextField = new javax.swing.JTextField();
        summarizeJButton = new javax.swing.JButton();

        panelDescriptionJLabel.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        panelDescriptionJLabel.setText("Vaccine Wastage Record");

        vaccineJTable.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        vaccineJTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Vaccines", "Disease" }));
        vaccineJTable.setRowHeight(25);
        jScrollPane1.setViewportView(vaccineJTable);

        viewJLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        viewJLabel.setText("View Wastage Details");

        wastageJTable.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        wastageJTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Manufacturing Batch", "Wasted Quantity" }));
        wastageJTable.setRowHeight(25);
        jScrollPane2.setViewportView(wastageJTable);

        backJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        backJButton.setText("<< Back");
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        clickJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        clickJButton.setText("Click Here!");
        clickJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clickJButtonActionPerformed(evt);
            }
        });

        listJLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        listJLabel.setText("List of vaccines:");

        totalWastageJLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        totalWastageJLabel.setText("Total Wastage");

        totalWastageJTextField.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        summarizeJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        summarizeJButton.setText("Summarize graphically");
        summarizeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                summarizeJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 817,
                                        Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(panelDescriptionJLabel)
                                        .addGroup(layout.createSequentialGroup().addComponent(viewJLabel)
                                                .addGap(18, 18, 18).addComponent(clickJButton))
                                        .addComponent(listJLabel)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        542, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(31, 31, 31).addComponent(summarizeJButton,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 201,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE).addComponent(totalWastageJLabel).addGap(31, 31, 31)
                                .addComponent(totalWastageJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 92,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(121, 121, 121))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(backJButton)
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(panelDescriptionJLabel)
                        .addGap(24, 24, 24).addComponent(listJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 127,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(28, 28, 28))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(summarizeJButton).addGap(69, 69,
                                                69)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(viewJLabel).addComponent(clickJButton))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 127,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(totalWastageJLabel).addComponent(totalWastageJTextField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30).addComponent(backJButton).addContainerGap(23, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void clickJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clickJButtonActionPerformed
        // TODO add your handling code here:
        int selectedRow = vaccineJTable.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Kindly select a vaccine");
            return;
        }

        Vaccine vaccine = (Vaccine) vaccineJTable.getValueAt(selectedRow, 0);
        populateWastageTable(vaccine);
    }//GEN-LAST:event_clickJButtonActionPerformed

    private void summarizeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_summarizeJButtonActionPerformed
        // TODO add your handling code here:
        int totalWastage = 0;
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        for (Vaccine vaccine : enterprise.getVaccineCatalog().getVaccineList()) {
            populateWastageTable(vaccine);
            totalWastage = Integer.parseInt(totalWastageJTextField.getText());
            pieDataset.setValue(vaccine.getName(), totalWastage);
        }

        JFreeChart chart = ChartFactory.createPieChart("Pie Chart", pieDataset, true, true, true);
        PiePlot p = (PiePlot) chart.getPlot();
        ChartFrame frame = new ChartFrame("Wastage Summary", chart);
        frame.setVisible(true);
        frame.setSize(450, 500);
    }//GEN-LAST:event_summarizeJButtonActionPerformed

    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed
        // TODO add your handling code here:
        userProcessContainer.remove(this);
        CardLayout layout = (CardLayout) userProcessContainer.getLayout();
        layout.previous(userProcessContainer);
    }//GEN-LAST:event_backJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backJButton;
    private javax.swing.JButton clickJButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel listJLabel;
    private javax.swing.JLabel panelDescriptionJLabel;
    private javax.swing.JButton summarizeJButton;
    private javax.swing.JLabel totalWastageJLabel;
    private javax.swing.JTextField totalWastageJTextField;
    private javax.swing.JTable vaccineJTable;
    private javax.swing.JLabel viewJLabel;
    private javax.swing.JTable wastageJTable;
    // End of variables declaration//GEN-END:variables
}