Java tutorial
/* * 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.ManufacturerRole; import business.Enterprise.CDCEnterprise; import business.Inventory.Batch; import business.business.Business; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.util.HashMap; import java.util.Map; import javax.swing.JPanel; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Rachita */ public class ViewInventoryJPanel extends javax.swing.JPanel { JPanel userProcessContainer; private Business business; private CDCEnterprise enterprise; private int totalInventory; Map<String, Integer> vaccineNameVsNumber; public ViewInventoryJPanel(JPanel userProcessContainer, Business business, CDCEnterprise enterprise) { initComponents(); this.userProcessContainer = userProcessContainer; this.business = business; this.enterprise = enterprise; populateTable(); txtTotalQuantity.setText(totalInventory + ""); printVaccineNameVsNumber(); printBarChart(); } private void populateTable() { DefaultTableModel dtm = (DefaultTableModel) VaacineJTable.getModel(); dtm.setRowCount(0); for (Batch b : enterprise.getInventory().getBatchList()) { Object[] row = new Object[7]; row[0] = b.getBatchNumber(); row[1] = b.getVaccine().getCode(); row[2] = b.getVaccine().getName(); row[3] = b.getVaccine().getQuantity(); row[4] = b.getVaccine().getStock(); row[5] = b.getVaccine().getPrice() * b.getVaccine().getQuantity(); row[6] = b.getVaccine().getFixedQty(); dtm.addRow(row); totalInventory = b.getVaccine().getQuantity() + totalInventory; enterprise.getInventory().setTotalQuantity(totalInventory); } } private void printBarChart() { DefaultCategoryDataset barChartData = new DefaultCategoryDataset(); for (Map.Entry<String, Integer> entry : vaccineNameVsNumber.entrySet()) { System.out.println("< " + entry.getKey() + " " + entry.getValue() + " >"); barChartData.addValue(entry.getValue(), "Vaccine", entry.getKey()); } JFreeChart barChart = ChartFactory.createBarChart("Vaccine Inventory", "Vaccines", "Qty", barChartData, PlotOrientation.VERTICAL, false, true, false); CategoryPlot barChrt = barChart.getCategoryPlot(); barChrt.setRangeGridlinePaint(Color.BLUE); ChartPanel panel = new ChartPanel(barChart); PanelChart.removeAll(); PanelChart.add(panel, BorderLayout.CENTER); PanelChart.validate(); } private void printVaccineNameVsNumber() { vaccineNameVsNumber = new HashMap<String, Integer>(); for (Batch b : enterprise.getInventory().getBatchList()) { System.out.println("Batch :" + b.getVaccine().getName() + " Number : " + b.getVaccine().getQuantity()); if (vaccineNameVsNumber.containsKey(b.getVaccine().getName())) { String VaccineName = b.getVaccine().getName(); Integer temp = vaccineNameVsNumber.get(b.getVaccine().getName()); System.out.println("qty from previous hashmap : " + temp); temp += b.getVaccine().getQuantity(); //vaccineNameVsNumber.put(b.getVaccine().getName()), temp); System.out.println("PUT: " + VaccineName + " :: " + temp); vaccineNameVsNumber.put(VaccineName, temp); } else { System.out.println("PUT2: " + b.getVaccine().getName() + " :: " + b.getVaccine().getQuantity()); vaccineNameVsNumber.put(b.getVaccine().getName(), b.getVaccine().getQuantity()); } } System.out.println("PRINT HASHMAP FOR INV"); for (Map.Entry<String, Integer> entry : vaccineNameVsNumber.entrySet()) { System.out.println("< " + entry.getKey() + " " + entry.getValue() + " >"); } } /** * 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() { jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); VaacineJTable = new javax.swing.JTable(); jLabel2 = new javax.swing.JLabel(); txtTotalQuantity = new javax.swing.JTextField(); btnBack = new javax.swing.JButton(); PanelChart = new javax.swing.JPanel(); setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N jLabel1.setText(" View National Inventory StockPile"); jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 40, 380, 37)); VaacineJTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Batch Number", "Vaccine Code", "Vaccine Name", "Vaccine#", "Stock Info", "Cost# per Batch", "Batch#" }) { Class[] types = new Class[] { java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class }; boolean[] canEdit = new boolean[] { false, false, false, false, false, false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(VaacineJTable); if (VaacineJTable.getColumnModel().getColumnCount() > 0) { VaacineJTable.getColumnModel().getColumn(0).setResizable(false); VaacineJTable.getColumnModel().getColumn(1).setResizable(false); VaacineJTable.getColumnModel().getColumn(2).setResizable(false); VaacineJTable.getColumnModel().getColumn(3).setResizable(false); VaacineJTable.getColumnModel().getColumn(4).setResizable(false); VaacineJTable.getColumnModel().getColumn(5).setResizable(false); VaacineJTable.getColumnModel().getColumn(6).setResizable(false); } add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 130, 920, 190)); jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N jLabel2.setText("Total Quantity"); add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 360, -1, -1)); txtTotalQuantity.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N txtTotalQuantity.setEnabled(false); add(txtTotalQuantity, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 350, 180, 30)); btnBack.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N btnBack.setText("<<Back"); btnBack.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBackActionPerformed(evt); } }); add(btnBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 110, 30)); add(PanelChart, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 380, 770, 440)); }// </editor-fold>//GEN-END:initComponents private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed userProcessContainer.remove(this); CardLayout layout = (CardLayout) userProcessContainer.getLayout(); layout.previous(userProcessContainer); }//GEN-LAST:event_btnBackActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel PanelChart; private javax.swing.JTable VaacineJTable; private javax.swing.JButton btnBack; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField txtTotalQuantity; // End of variables declaration//GEN-END:variables }