GUILayer.CustomerStatsByGroup.java Source code

Java tutorial

Introduction

Here is the source code for GUILayer.CustomerStatsByGroup.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 GUILayer;

import ControlLayer.CustomerCtrl;
import ControlLayer.EmployeeCtrl;
import ControlLayer.MachineCtrl;
import ControlLayer.ProductCtrl;
import ModelLayer.Customer;
import ModelLayer.Employee;
import ModelLayer.Group;
import ModelLayer.Machine;
import ModelLayer.Product;
import java.util.ArrayList;
import java.util.Collections;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author viva
 */
public class CustomerStatsByGroup extends javax.swing.JFrame {

    private Registration reg;

    /**
     * Creates new form Statistics
     */
    public CustomerStatsByGroup() {
        initComponents();
    }

    protected void makeStatistics(String title, String chartTitle, PieDataset dataset) {
        JFreeChart chart = createChart(dataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        setContentPane(chartPanel);
    }

    protected PieDataset createProductDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        ProductCtrl prdCtrl = new ProductCtrl();
        for (Product prd : prdCtrl.getProducts()) {
            System.out.println(prd.getSoldProducts());
            if (prd.getSoldProducts() > 0) {
                result.setValue(prd.getCategory(), prd.getSoldProducts());
            }
        }
        return result;
    }

    protected PieDataset createEmployeeDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        EmployeeCtrl employeeCtrl = new EmployeeCtrl();
        for (Employee emp : employeeCtrl.getEmployees()) {
            if (emp.getProductsSold() > 0) {
                result.setValue(emp.getName(), emp.getProductsSold());
            }
        }
        return result;
    }

    protected PieDataset createCustomerNumberInGroupDataset() {
        reg = new Registration();
        DefaultPieDataset result = new DefaultPieDataset();
        CustomerCtrl custCtrl = new CustomerCtrl();
        ArrayList<Group> list = new ArrayList<>();
        list = reg.getGroups();
        for (Group gr : list) {
            int counter = 0;
            for (Customer cust : custCtrl.getCustomers()) {
                if (gr.getName().equals(cust.getGroup().getName())) {
                    counter++;
                }
            }
            result.setValue(gr.getName(), counter);
        }
        return result;
    }

    protected PieDataset createTop10ProductsSold() {
        DefaultPieDataset result = new DefaultPieDataset();
        ProductCtrl prdCtrl = new ProductCtrl();
        ArrayList<Product> products = prdCtrl.getProducts();
        Collections.sort(products, (Product o1, Product o2) -> o1.compareTo(o2));
        for (Product pr : products) {
            int n = 0;
            if (n != 10) {
                result.setValue(pr.getName(), pr.getSoldProducts());
                n++;
            }
        }
        return result;
    }

    protected PieDataset createMachineDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        MachineCtrl machCtrl = new MachineCtrl();
        for (Machine mach : machCtrl.getMachines()) {
        }
        return result;
    }

    protected PieDataset createCustomerDataset() {
        reg = new Registration();
        CustomerCtrl custCtrl = new CustomerCtrl();
        ArrayList<Group> list = new ArrayList<>();
        list = reg.getGroups();
        DefaultPieDataset result = new DefaultPieDataset();
        for (Group gr : list) {
            int total = 0;
            System.out.println(gr.getName());
            for (Customer cust : custCtrl.getCustomers()) {
                if (cust.getGroup().getName().equals(gr.getName())) {
                    if (cust.getProductsBought() > 0) {
                        total += cust.getProductsBought();
                    }
                }
            }
            result.setValue(gr.getName(), total);
        }
        return result;
    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, true);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setForegroundAlpha(0.5f);
        return chart;
    }

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

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 752, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 342, Short.MAX_VALUE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Statistics.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Statistics().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}