Interface.CommunityAdminWorkArea.BestRestaurant.java Source code

Java tutorial

Introduction

Here is the source code for Interface.CommunityAdminWorkArea.BestRestaurant.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 Interface.CommunityAdminWorkArea;

import Business.Community.Community;
import Business.Employee.CitizenEmployee;
import Business.Employee.Employee;
import Business.Employee.RestaurantEmployee;
import Business.Organization.Organization;
import Business.UserAccount.UserAccount;
import Business.WorkQueue.WorkRequest;
import java.awt.CardLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Date;
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 kunal
 */
public class BestRestaurant extends javax.swing.JPanel {

    /**
     * Creates new form BestRestaurant
     */
    private JPanel userProcessContainer;
    private UserAccount account;
    private Employee ee;
    private Community community;
    private Organization organization;

    public BestRestaurant(JPanel userProcessContainer, UserAccount account) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.account = account;
        ee = account.getEmployee();
        community = ee.getCommunityAssociated();
        organization = community.getFoodDistributionOrg();

    }

    public ArrayList<RestaurantEmployee> calculateTopThreeRestaurantByRewardPoints(
            ArrayList<RestaurantEmployee> restaurantList) {

        int val = 0;

        RestaurantEmployee ce1 = new RestaurantEmployee();
        RestaurantEmployee ce2 = new RestaurantEmployee();
        RestaurantEmployee ce3 = new RestaurantEmployee();

        for (RestaurantEmployee ce : restaurantList) {

            val = ce.getCurrentRewardPoints();
            if (val > ce1.getCurrentRewardPoints()) {
                ce3 = ce2;
                ce2 = ce1;
                ce1 = ce;
            } else if (val > ce2.getCurrentRewardPoints()) {
                if (ce1.getCurrentRewardPoints() == val) {
                    if (ce1.getCurrentRewardPoints() != ce.getCurrentRewardPoints()) {
                        ce3 = ce2;
                        ce2 = ce;
                    }
                } else {
                    ce3 = ce2;
                    ce2 = ce;
                }
            }

            else if (val > ce3.getCurrentRewardPoints()) {
                if (ce1.getCurrentRewardPoints() == val || ce2.getCurrentRewardPoints() == val) {

                } else {
                    ce3 = ce;
                }
            }

        }

        ArrayList<RestaurantEmployee> ceList = new ArrayList<RestaurantEmployee>();
        ceList.add(ce1);
        ceList.add(ce2);
        ceList.add(ce3);

        return ceList;

    }

    /**
     * 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();
        jLabel3 = new javax.swing.JLabel();
        btnCalculate = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Top 3 Restaurant eligible for Tax Redemption");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("To Date");

        btnCalculate.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        btnCalculate.setText("View Results >>");
        btnCalculate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalculateActionPerformed(evt);
            }
        });

        btnBack.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        btnBack.setText("<< Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        jDateChooser1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        jDateChooser2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setText("From Date");

        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().addContainerGap().addComponent(btnBack))
                        .addGroup(layout.createSequentialGroup().addGap(59, 59, 59)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2).addComponent(jLabel3))
                                .addGap(58, 58, 58)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnCalculate)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jDateChooser2,
                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 190,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel1))))
                        .addContainerGap(309, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addGap(24, 24, 24).addComponent(jLabel1).addGap(50, 50, 50).addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addGroup(layout
                                .createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2).addComponent(jDateChooser1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(36, 36, 36).addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        29, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(42, 42, 42).addComponent(btnCalculate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 375, Short.MAX_VALUE)
                        .addComponent(btnBack).addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCalculateActionPerformed
        // TODO add your handling code here:

        Date toDate1 = jDateChooser1.getDate();
        Date toDate2 = jDateChooser2.getDate();
        if ((toDate1 == null) || (toDate2 == null)) {
            JOptionPane.showMessageDialog(null, "Invalid date..Kindly enter valid date.");
            return;
        }

        long fromDate = (jDateChooser1.getDate().getTime()) / (1000 * 60 * 60 * 24);
        long toDate = (jDateChooser2.getDate().getTime()) / (1000 * 60 * 60 * 24);
        //String citizenRewards = null;
        ArrayList<RestaurantEmployee> restaurantEmployeeList = new ArrayList<>();

        for (WorkRequest request : organization.getWorkQueue().getWorkRequestList()) {

            long requestDate = (request.getRequestDate().getTime()) / (1000 * 60 * 60 * 24);

            if ((requestDate >= fromDate) && (requestDate <= toDate)) {

                Employee ee = request.getSender().getEmployee();
                if (ee instanceof RestaurantEmployee) {
                    RestaurantEmployee c = (RestaurantEmployee) ee;
                    if (!restaurantEmployeeList.contains(c)) {
                        c.calculateTotalRewardPointsForCitizen();
                        restaurantEmployeeList.add(c);
                    }
                }
            }
            //            else{
            //
            //                JOptionPane.showMessageDialog(null, "There are no records for this search criteria.");
            //            }

        }

        if (restaurantEmployeeList.isEmpty()) {
            JOptionPane.showMessageDialog(null, "There are no records for this search criteria.");
        }

        ArrayList<RestaurantEmployee> restaurantEmployee = calculateTopThreeRestaurantByRewardPoints(
                restaurantEmployeeList);

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(restaurantEmployee.get(0).getCurrentRewardPoints(), "Commercial body",
                restaurantEmployee.get(0).getName());
        dataset.setValue(restaurantEmployee.get(1).getCurrentRewardPoints(), "Commercial body",
                restaurantEmployee.get(1).getName());
        dataset.setValue(restaurantEmployee.get(2).getCurrentRewardPoints(), "Commercial body",
                restaurantEmployee.get(2).getName());

        JFreeChart chart = ChartFactory.createBarChart3D("Types of food collected", "Top 3 Restaurant",
                "Number of reward points", dataset, PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setRangeGridlinePaint(Color.BLUE);
        ChartFrame frame = new ChartFrame("Bar Chart for best Restaurant", chart);
        frame.setVisible(true);
        frame.setSize(450, 350);
    }//GEN-LAST:event_btnCalculateActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO add your handling code here:
        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.JButton btnBack;
    private javax.swing.JButton btnCalculate;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}