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 Interface.ApplicationAdmin; import Business.Enterprise.Enterprise; import Business.Organization.Organization; import Business.UserAccount.UserAccount; import java.awt.CardLayout; import java.awt.Color; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Vivek */ public class Analytics extends javax.swing.JPanel { /** * Creates new form Analytics */ JPanel container; UserAccount userAccount; Organization organization; Enterprise enterprise; public static boolean ASC = true; public static boolean DESC = false; Map<String, Integer> uservaluemap = new HashMap<String, Integer>(); public Analytics(JPanel container, UserAccount useraccount, Organization organization, Enterprise enterprise) { initComponents(); this.userAccount = useraccount; this.organization = organization; this.container = container; this.enterprise = enterprise; fillform(); } /** * 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() { jLabel2 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); MaxcarownernamejLabel = new javax.swing.JLabel(); MincarownernamejLabel = new javax.swing.JLabel(); MaxcarownerridejLabel = new javax.swing.JLabel(); MincarownerridejLabel = new javax.swing.JLabel(); caruserdatajButton = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); Maxcarusername = new javax.swing.JLabel(); MinCarusername = new javax.swing.JLabel(); MaxCaruserride = new javax.swing.JLabel(); MinCarUserride = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); setBackground(new java.awt.Color(255, 255, 255)); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel2.setText("Analytics"); jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/back.png"))); // NOI18N jButton3.setBorder(null); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jPanel1.setBackground(new java.awt.Color(240, 238, 221)); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("Car Owner with maximum number of rides :"); jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel6.setText("Car Owner with minimum number of rides :"); MaxcarownernamejLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MaxcarownernamejLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MincarownernamejLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MincarownernamejLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MaxcarownerridejLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MaxcarownerridejLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MincarownerridejLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MincarownerridejLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); caruserdatajButton.setBackground(new java.awt.Color(0, 0, 0)); caruserdatajButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N caruserdatajButton.setForeground(new java.awt.Color(255, 255, 255)); caruserdatajButton.setText("Graph"); caruserdatajButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { caruserdatajButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1) .addGap(18, 18, 18).addComponent(MaxcarownernamejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(415, 415, 415) .addComponent(MincarownernamejLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(jLabel6)).addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(MaxcarownerridejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(MincarownerridejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(270, 270, 270).addComponent( caruserdatajButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(23, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(MaxcarownernamejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(MaxcarownerridejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(35, 35, 35) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6) .addComponent(MincarownernamejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(MincarownerridejLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE) .addComponent(caruserdatajButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); jPanel2.setBackground(new java.awt.Color(240, 238, 221)); jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel4.setText("Car User with maximum number of rides :"); jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel7.setText("Car User with minimum number of rides :"); Maxcarusername.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N Maxcarusername.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MinCarusername.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MinCarusername.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MaxCaruserride.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MaxCaruserride.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MinCarUserride.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N MinCarUserride.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jButton2.setBackground(new java.awt.Color(0, 0, 0)); jButton2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 255, 255)); jButton2.setText("Graph"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel7)) .addGap(37, 37, 37) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(MinCarusername, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(MinCarUserride, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(Maxcarusername, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(MaxCaruserride, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel2Layout.createSequentialGroup().addGap(269, 269, 269).addComponent( jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4) .addComponent(Maxcarusername, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(MaxCaruserride, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout.createSequentialGroup().addGap(14, 14, 14) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(MinCarUserride, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(MinCarusername, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(59, 59, 59).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup( layout.createSequentialGroup().addGap(340, 340, 340).addComponent(jLabel2))) .addContainerGap(82, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(24, 24, 24) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(43, 43, 43) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(33, 33, 33) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26))); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed int x = Integer.parseInt(MaxCaruserride.getText()); int y = Integer.parseInt(MinCarUserride.getText()); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(x, "Max Car ride", "Rides"); dataset.setValue(y, "Min Car ride", "Rides"); JFreeChart chart = ChartFactory.createBarChart("Car owner Min - Max ride chart", "Car ride", "Number of rides", dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.black); ChartFrame frame = new ChartFrame("Ride chart ", chart); frame.setVisible(true); frame.setSize(450, 350); frame.setLocation(400, 150); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed container.remove(this); CardLayout layout = (CardLayout) container.getLayout(); layout.previous(container); }//GEN-LAST:event_jButton3ActionPerformed private void caruserdatajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_caruserdatajButtonActionPerformed int x = Integer.parseInt(MaxcarownerridejLabel.getText()); int y = Integer.parseInt(MincarownerridejLabel.getText()); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(x, "Max Car ride", "Rides"); dataset.setValue(y, "Min Car ride", "Rides"); JFreeChart chart = ChartFactory.createBarChart("User Min - Max ride chart", "Car ride", "Number of rides", dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot p = chart.getCategoryPlot(); p.setRangeGridlinePaint(Color.black); ChartFrame frame = new ChartFrame("User Ride chart ", chart); frame.setVisible(true); frame.setSize(450, 350); frame.setLocation(400, 150); }//GEN-LAST:event_caruserdatajButtonActionPerformed public void fillform() { try { String carownermaxname; String carownermaxride; String carownerminname; String carownerminride; String caruserrmaxname; String carusermaxride; String caruserminname; String caruserminride; String carownermaxamountname; String carownermaxamount; String caruserminamountname; String caruserminamount; String temp; for (Organization org : enterprise.getOrganizationDirectory().getOrganizationList()) if (org.getName().equals("Car Owner Organization")) for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) uservaluemap.put(ua.getPerson().getPersonName(), ua.getTrips()); temp = gettopdata(); String[] a = temp.split(":"); String[] a1 = a[0].split("="); String[] a2 = a[1].split("="); carownermaxname = a1[0]; carownermaxride = a1[1]; carownerminname = a2[0]; carownerminride = a2[1]; MaxcarownernamejLabel.setText(carownermaxname); MaxcarownerridejLabel.setText(carownermaxride); MincarownernamejLabel.setText(carownerminname); MincarownerridejLabel.setText(carownerminride); uservaluemap.clear(); /**/ for (Organization org : enterprise.getOrganizationDirectory().getOrganizationList()) if (org.getName().equals("Caruser Organization")) for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) uservaluemap.put(ua.getPerson().getPersonName(), ua.getTrips()); temp = gettopdata(); String[] x = temp.split(":"); String[] x1 = x[0].split("="); String[] x2 = x[1].split("="); caruserrmaxname = x1[0]; carusermaxride = x1[1]; caruserminname = x2[0]; caruserminride = x2[1]; Maxcarusername.setText(caruserrmaxname); MaxCaruserride.setText(carusermaxride); MinCarusername.setText(caruserminname); MinCarUserride.setText(caruserminride); uservaluemap.clear(); /**/ for (Organization org : enterprise.getOrganizationDirectory().getOrganizationList()) if (org.getName().equals("Car Owner Organization")) for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) // uservaluemap.put(ua.getPerson().getPersonName(), ua.getPerson().getAmount()); uservaluemap.put(ua.getPerson().getPersonName(), ua.getPerson().getAmount()); temp = gettopdata(); String[] y = temp.split(":"); String[] y1 = y[0].split("="); String[] y2 = y[1].split("="); carownermaxamountname = y1[0]; carownermaxamount = y1[1]; // MaxCarOwnername.setText(carownermaxamountname); // MaxCarOwnerAmount.setText(carownermaxamount); uservaluemap.clear(); /**/ for (Organization org : enterprise.getOrganizationDirectory().getOrganizationList()) if (org.getName().equals("Caruser Organization")) for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) uservaluemap.put(ua.getPerson().getPersonName(), ua.getPerson().getAmount()); temp = gettopdata(); x = temp.split(":"); x1 = x[0].split("="); x2 = x[1].split("="); caruserminamountname = x1[0]; caruserminamount = x1[1]; // MaxCarOwnername.setText(carownermaxamountname); // MaxCarOwnerAmount.setText(carownermaxamount); // MinCarUserName.setText(caruserminamountname); // MaxCarUseramount.setText(caruserminamount); } catch (Exception e) { } } public String gettopdata() { try { Map<String, Integer> sortedMapAsc = sortByComparator(uservaluemap, DESC); Object[] ff = sortedMapAsc.entrySet().toArray(); String a1 = ff[0].toString(); Map<String, Integer> sortedMapAsc1 = sortByComparator(uservaluemap, ASC); Object[] ff1 = sortedMapAsc1.entrySet().toArray(); String b1 = ff1[0].toString(); String x = a1 + ":" + b1; return x; } catch (Exception e) { JOptionPane.showMessageDialog(this, "No data to disaply"); } return null; } private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order) { List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (order) return o1.getValue().compareTo(o2.getValue()); else return o2.getValue().compareTo(o1.getValue()); } }); // Maintaining insertion order with the help of LinkedList Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> entry : list) sortedMap.put(entry.getKey(), entry.getValue()); return sortedMap; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel MaxCaruserride; private javax.swing.JLabel MaxcarownernamejLabel; private javax.swing.JLabel MaxcarownerridejLabel; private javax.swing.JLabel Maxcarusername; private javax.swing.JLabel MinCarUserride; private javax.swing.JLabel MinCarusername; private javax.swing.JLabel MincarownernamejLabel; private javax.swing.JLabel MincarownerridejLabel; private javax.swing.JButton caruserdatajButton; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration//GEN-END:variables }