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.TMAnalystRole; import Business.Enterprise.Enterprise; import Business.Enterprise.TrainAuthorityEnterprise; import Business.TrainManagementSystem.Line; import Business.TrainManagementSystem.Route; import Business.TrainManagementSystem.TrainStatus; import Business.TrainManagementSystem.Segment; import Business.TrainManagementSystem.SimulationClass; import Business.TrainManagementSystem.TimeSlot; import Business.TrainManagementSystem.Train; import Business.TrainManagementSystem.TrainOffered; import java.awt.Color; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; 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.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Manasi Laddha */ public class TMAnaylstWorkAreaJPanel extends javax.swing.JPanel { /** * Creates new form TMAnaylstWorkAreaJPanel */ JPanel userProcessContainer; TrainAuthorityEnterprise enterprise; public TMAnaylstWorkAreaJPanel(JPanel userProcessContainer, Enterprise enterprise) { initComponents(); this.userProcessContainer = userProcessContainer; this.enterprise = (TrainAuthorityEnterprise) enterprise; populateActivelines(); } private void populateActivelines() { cboActiveLines.removeAllItems(); for (Line l : enterprise.getLineCatalog().getLinesList()) { if (l.isHasStarted() && l.isIsActive()) { cboActiveLines.addItem(l); } } if (cboActiveLines.getItemCount() <= 0) { cboActiveLines.setEnabled(false); cboTrains.setEnabled(false); btnRouteAnalysis.setEnabled(false); btnTrainAnalysis.setEnabled(false); } else { cboActiveLines.setEnabled(true); cboTrains.setEnabled(true); btnRouteAnalysis.setEnabled(true); btnTrainAnalysis.setEnabled(true); } } /** * 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(); btnTrainAnalysis = new javax.swing.JButton(); btnSimulateData = new javax.swing.JButton(); spnrDays = new javax.swing.JSpinner(); jLabel6 = new javax.swing.JLabel(); cboActiveLines = new javax.swing.JComboBox(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); cboTrains = new javax.swing.JComboBox(); spnrRecord = new javax.swing.JSpinner(); jLabel9 = new javax.swing.JLabel(); btnRouteAnalysis = new javax.swing.JButton(); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/analyst.png"))); // NOI18N jLabel1.setText("TM-Analyst Work Area"); btnTrainAnalysis.setBackground(new java.awt.Color(0, 0, 0)); btnTrainAnalysis.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N btnTrainAnalysis.setForeground(new java.awt.Color(255, 255, 255)); btnTrainAnalysis.setText("View Train Congestion History>>"); btnTrainAnalysis.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnTrainAnalysisActionPerformed(evt); } }); btnSimulateData.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N btnSimulateData.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/schedule.png"))); // NOI18N btnSimulateData.setText("Simulate Data for Past X days"); btnSimulateData.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSimulateDataActionPerformed(evt); } }); spnrDays.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N spnrDays.setModel(new javax.swing.SpinnerNumberModel(1, 1, 20, 1)); jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel6.setText("Previous No of Days:"); cboActiveLines.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N cboActiveLines.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cboActiveLinesActionPerformed(evt); } }); jLabel7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel7.setText("Active Train :"); jLabel8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel8.setText("Active Lines :"); cboTrains.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N spnrRecord.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N spnrRecord.setModel(new javax.swing.SpinnerNumberModel(1, 1, 20, 1)); jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel9.setText("No of Days Before:"); btnRouteAnalysis.setBackground(new java.awt.Color(0, 0, 0)); btnRouteAnalysis.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N btnRouteAnalysis.setForeground(new java.awt.Color(255, 255, 255)); btnRouteAnalysis.setText("View Line Congestion History>>"); btnRouteAnalysis.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRouteAnalysisActionPerformed(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().addGap(42, 42, 42).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(cboActiveLines, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7).addComponent(jLabel9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(spnrRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(cboTrains, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(33, 33, 33) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnTrainAnalysis, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(btnRouteAnalysis, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 312, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel6) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(spnrDays, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(btnSimulateData, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap()) .addGroup(layout.createSequentialGroup().addGap(279, 279, 279).addComponent(jLabel1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(25, 25, 25).addComponent(jLabel1).addGap(70, 70, 70) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9) .addComponent(spnrRecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6).addComponent(spnrDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cboActiveLines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8) .addComponent(btnRouteAnalysis, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnSimulateData, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(88, 88, 88).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7) .addComponent(cboTrains, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnTrainAnalysis, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(547, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void generateGraph(Train selectedTrain) { ArrayList<TrainOffered> selectedTrainOfferedListWEEKDAYS = new ArrayList<>(); ArrayList<TrainOffered> selectedTrainOfferedListWEEKENDS = new ArrayList<>(); int days = (int) spnrRecord.getValue(); Date today = new Date(); Date olderThanToday = new Date(); olderThanToday.setTime(today.getTime() + (long) (-days) * 1000 * 60 * 60 * 24); long todayDate = (today.getTime()) / (1000 * 60 * 60 * 24); long olderDate = (olderThanToday.getTime()) / (1000 * 60 * 60 * 24); for (TrainOffered selectedTrainOffered : enterprise.getTrainsOfferedHistory().getTrainsOffered()) { if (selectedTrainOffered.getTrain().equals(selectedTrain)) { long selectedDate = (selectedTrainOffered.getDayOffered().getTime()) / (1000 * 60 * 60 * 24); if (selectedDate >= olderDate && selectedDate <= todayDate) { Calendar date = Calendar.getInstance(); date.setTime(selectedTrainOffered.getDayOffered()); if ((date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || ((date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY))) { if (!selectedTrainOfferedListWEEKENDS.contains(selectedTrainOffered)) { selectedTrainOfferedListWEEKENDS.add(selectedTrainOffered); } } else { if (!selectedTrainOfferedListWEEKDAYS.contains(selectedTrainOffered)) { selectedTrainOfferedListWEEKDAYS.add(selectedTrainOffered); } } } } } Map<TimeSlot.TimeSlotRange, Integer> weekendData = new HashMap<TimeSlot.TimeSlotRange, Integer>(); for (TrainOffered selectedTrainOfferedOnWeekend : selectedTrainOfferedListWEEKENDS) { for (TrainStatus rt : selectedTrainOfferedOnWeekend.getRunningTrains()) { if (weekendData.containsKey(rt.getTrainSchedule().getTimeSlot())) { Integer previousValue = weekendData.get(rt.getTrainSchedule().getTimeSlot()); weekendData.put(rt.getTrainSchedule().getTimeSlot(), previousValue + rt.getRunningCapacity()); } else { weekendData.put(rt.getTrainSchedule().getTimeSlot(), rt.getRunningCapacity()); } } } ArrayList<Integer> sortedWeekendData = new ArrayList<>(); if (weekendData.size() > 0) { for (TimeSlot.TimeSlotRange tsr : TimeSlot.TimeSlotRange.values()) { sortedWeekendData.add(weekendData.get(tsr)); } } Map<TimeSlot.TimeSlotRange, Integer> weekdayData = new HashMap<>(); for (TrainOffered selectedTrainOfferedOnWeekDays : selectedTrainOfferedListWEEKDAYS) { for (TrainStatus rt : selectedTrainOfferedOnWeekDays.getRunningTrains()) { if (weekdayData.containsKey(rt.getTrainSchedule().getTimeSlot())) { Integer previousValue = weekdayData.get(rt.getTrainSchedule().getTimeSlot()); weekdayData.put(rt.getTrainSchedule().getTimeSlot(), previousValue + rt.getRunningCapacity()); } else { weekdayData.put(rt.getTrainSchedule().getTimeSlot(), rt.getRunningCapacity()); } } } ArrayList<Integer> sortedWeekDayDataList = new ArrayList<>(); if (weekdayData.size() > 0) { for (TimeSlot.TimeSlotRange tsr : TimeSlot.TimeSlotRange.values()) { sortedWeekDayDataList.add(weekdayData.get(tsr)); } } CategoryDataset dataset; String series1 = "Weekdays"; String series2 = "Weekends"; String series3 = "Maximum Capcity"; DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(); for (int i = 0; i < sortedWeekendData.size(); i++) { dataset1.addValue(Math.round(sortedWeekendData.get(i) / selectedTrainOfferedListWEEKENDS.size()), series2, TimeSlot.TimeSlotRange.values()[i].getValue()); } for (int i = 0; i < sortedWeekDayDataList.size(); i++) { // System.out.println("Key = " + entry.getKey().getValue() + ", Value = " + Math.round(entry.getValue()/selectedTrainOfferedListWEEKENDS.size())); dataset1.addValue(Math.round(sortedWeekDayDataList.get(i) / selectedTrainOfferedListWEEKDAYS.size()), series1, TimeSlot.TimeSlotRange.values()[i].getValue()); } for (int i = 0; i < 15; i++) { // System.out.println("Key = " + entry.getKey().getValue() + ", Value = " + Math.round(entry.getValue()/selectedTrainOfferedListWEEKENDS.size())); dataset1.addValue(selectedTrain.getTrainCapacity(), series3, TimeSlot.TimeSlotRange.values()[i].getValue()); } dataset = dataset1; final JFreeChart chart = ChartFactory.createLineChart( "Average Congestion of Train : " + selectedTrain.getTrainName(), // chart title "Time Slot", // domain axis label "Congestion Level", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); chart.setBackgroundPaint(Color.white); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setRangeGridlinePaint(Color.white); ChartFrame chartPanel = new ChartFrame("Line Chart of Train", chart); chartPanel.setSize(this.getWidth(), this.getHeight() + 200); chartPanel.setVisible(true); } private void btnTrainAnalysisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrainAnalysisActionPerformed // TODO add your handling code here: Train selectedTrain = (Train) cboTrains.getSelectedItem(); generateGraph(selectedTrain); }//GEN-LAST:event_btnTrainAnalysisActionPerformed SimulationClass objSimulation; private void btnSimulateDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimulateDataActionPerformed // TODO add your handling code here: int days = (int) spnrDays.getValue(); objSimulation = new SimulationClass(); Date today = new Date(); Date olderThanToday = new Date(); olderThanToday.setTime(today.getTime() + (long) (-days) * 1000 * 60 * 60 * 24); for (Train t : enterprise.getTrainCatalog().getTrainsList()) { if (t.getLineOnTrain().isIsActive() && t.getLineOnTrain().isHasStarted()) { for (int i = 1; i <= days; i++) { Date thistoday = new Date(); thistoday.setTime(olderThanToday.getTime()); TrainOffered selectedTrainOffered = enterprise.getTrainsOfferedHistory().fetchOrAddTrain(t, thistoday); objSimulation.generatePreviousCrowdedData(selectedTrainOffered); olderThanToday.setTime(olderThanToday.getTime() + (long) (1) * 1000 * 60 * 60 * 24); } olderThanToday.setTime(today.getTime() + (long) (-days) * 1000 * 60 * 60 * 24); } } JOptionPane.showMessageDialog(this, "Data generated successfully.", "Simulation", JOptionPane.INFORMATION_MESSAGE); spnrDays.setValue(1); }//GEN-LAST:event_btnSimulateDataActionPerformed private void cboActiveLinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboActiveLinesActionPerformed // TODO add your handling code here: Line l = (Line) cboActiveLines.getSelectedItem(); if (l == null) { return; } populateTrains(l); }//GEN-LAST:event_cboActiveLinesActionPerformed private void btnRouteAnalysisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRouteAnalysisActionPerformed // TODO add your handling code here: Line selectedLine = (Line) cboActiveLines.getSelectedItem(); ArrayList<Route> routeList = new ArrayList<>(); for (Segment s : selectedLine.getSegments()) { routeList.add(s.getRoutes().get(0)); } ArrayList<Segment> segmentList = selectedLine.getSegments(); for (int i = segmentList.size() - 1; i >= 0; i--) { routeList.add(segmentList.get(i).getRoutes().get(1)); } int days = (int) spnrRecord.getValue(); Date today = new Date(); Date olderThanToday = new Date(); olderThanToday.setTime(today.getTime() + (long) (-days) * 1000 * 60 * 60 * 24); long todayDate = (today.getTime()) / (1000 * 60 * 60 * 24); long olderDate = (olderThanToday.getTime()) / (1000 * 60 * 60 * 24); Map<Route, Integer> routesWithCapacityList = new LinkedHashMap<>(); for (Route r : routeList) { int routeCount = 0; for (TimeSlot.TimeSlotRange ts : TimeSlot.TimeSlotRange.values()) { Train selectedTrain = r.fetchRouteSchedule(ts).getTrainOffered(); int intermediateCount = 0; int trainsFound = 0; for (TrainOffered selectedTrainOffered : enterprise.getTrainsOfferedHistory().getTrainsOffered()) { if (selectedTrainOffered.getTrain().equals(selectedTrain)) { long selectedDate = (selectedTrainOffered.getDayOffered().getTime()) / (1000 * 60 * 60 * 24); if (selectedDate >= olderDate && selectedDate <= todayDate) { intermediateCount += selectedTrainOffered.fetchRunningTrainByTimeSlot(ts) .getRunningCapacity(); trainsFound++; } } } if (trainsFound != 0) { intermediateCount = intermediateCount / trainsFound; routeCount += intermediateCount; } } routeCount = Math.round(routeCount / 15); routesWithCapacityList.putIfAbsent(r, routeCount); } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Map.Entry<Route, Integer> entry : routesWithCapacityList.entrySet()) { dataset.setValue(entry.getValue(), "Routes", entry.getKey().getRouteName()); } JFreeChart chart = ChartFactory.createBarChart( "Congestion Levels of Line : " + selectedLine.getLineName() + " for " + days + " days", "Routes", "Average Congestion Levels", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot plot = chart.getCategoryPlot(); plot.setRangeGridlinePaint(Color.BLUE); ChartFrame frame = new ChartFrame("Bar Chart for Congestion Analysis", chart); frame.setVisible(true); frame.setSize(this.getWidth(), this.getHeight() + 200); }//GEN-LAST:event_btnRouteAnalysisActionPerformed private void populateTrains(Line l) { cboTrains.removeAllItems(); for (Train t : enterprise.getTrainCatalog().getTrainsList()) { if (t.getLineOnTrain().equals(l)) { cboTrains.addItem(t); } } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnRouteAnalysis; private javax.swing.JButton btnSimulateData; private javax.swing.JButton btnTrainAnalysis; private javax.swing.JComboBox cboActiveLines; private javax.swing.JComboBox cboTrains; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JSpinner spnrDays; private javax.swing.JSpinner spnrRecord; // End of variables declaration//GEN-END:variables }