frames.MainGUI.java Source code

Java tutorial

Introduction

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

import datatype.Fee;
import static datatype.GlobalVariable.COLUMN;
import static datatype.GlobalVariable.REPORT_FILES;
import static datatype.GlobalVariable.REPORT_TABLES;
import static datatype.GlobalVariable.RESIDENTS;
import static datatype.GlobalVariable.ROW;
import datatype.Resident;
import functions.LoadCSVdata;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

/**
 *
 * @author s145633
 */
public class MainGUI extends javax.swing.JFrame {

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

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

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        jButton6 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable5 = new javax.swing.JTable();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("NB4? v1.0.0");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "???", "" }));
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("?");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel1Layout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jButton2).addGap(270, 270, 270)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 253, Short.MAX_VALUE)
                        .addComponent(jButton2).addContainerGap()));

        jTabbedPane1.addTab("??", jPanel1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null },
                        { null, null, null, null } },
                new String[] { "", "?", "", "" }));
        jTable2.getSelectionModel().addListSelectionListener(new FeeTableSelectionListener());
        jScrollPane2.setViewportView(jTable2);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("?"));

        jTextField1.setText("???");
        jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField1MouseClicked(evt);
            }
        });
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField2.setText("??");
        jTextField2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField2MouseClicked(evt);
            }
        });

        jLabel1.setText("");

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jCheckBox1.setSelected(true);

        jCheckBox2.setSelected(true);

        jCheckBox3.setSelected(true);

        jCheckBox4.setSelected(true);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(jPanel5Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jCheckBox1).addComponent(jCheckBox2).addComponent(jCheckBox3)
                                .addComponent(jCheckBox4))
                        .addGap(0, 425, Short.MAX_VALUE)));
        jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup().addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox4)));

        jButton3.setText("");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("");
        jButton5.setVisible(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout
                .setHorizontalGroup(
                        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        37, Short.MAX_VALUE)
                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(jPanel4Layout.createSequentialGroup().addGap(115, 115, 115)
                                        .addComponent(jButton3).addGap(18, 18, 18).addComponent(jButton4)
                                        .addGap(18, 18, 18).addComponent(jButton5).addGap(0, 0, Short.MAX_VALUE)));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap()
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel4Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1)))
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3).addComponent(jButton4).addComponent(jButton5))
                        .addGap(0, 0, Short.MAX_VALUE)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout
                .setHorizontalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
                                        Short.MAX_VALUE)
                                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 173,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel4,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)));

        jTabbedPane1.addTab("", jPanel2);

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null, null, null }, { null, null, null, null, null, null },
                        { null, null, null, null, null, null }, { null, null, null, null, null, null } },
                new String[] { "??", "", "?", "?",
                        "", "" }));
        jScrollPane3.setViewportView(jTable3);
        this.jButton4.setVisible(false);

        jButton1.setText("??");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout
                .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createSequentialGroup().addGap(271, 271, 271).addComponent(jButton1)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 171,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                        .addComponent(jButton1).addContainerGap()));

        jTabbedPane1.addTab("??", jPanel3);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("?"));

        jTable4.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] {

        }));
        jScrollPane4.setViewportView(jTable4);

        jButton6.setText("?");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout
                .setHorizontalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
                        .addGroup(jPanel7Layout.createSequentialGroup().addGap(254, 254, 254).addComponent(jButton6)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel7Layout.setVerticalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton6)
                        .addGap(7, 7, 7)));

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("?"));

        jTable5.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] {

        }));
        jScrollPane5.setViewportView(jTable5);

        jButton7.setText("?");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(jPanel8Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane5)
                .addGroup(jPanel8Layout.createSequentialGroup().addGap(251, 251, 251).addComponent(jButton7)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel8Layout.setVerticalGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 96,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                        .addComponent(jButton7)));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(jPanel6Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel8,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));

        jTabbedPane1.addTab("??", jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addComponent(jTabbedPane1,
                        javax.swing.GroupLayout.PREFERRED_SIZE, 632, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING));

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

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void addRowToResidentInfoTable(Resident r) {
        DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();
        model.addRow(new Object[] { r.name, String.valueOf(r.basic_rent) });
    }

    private void addAllResidentInfoTable() {
        DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();
        model.setRowCount(0);
        for (Resident r : datatype.GlobalVariable.RESIDENTS) {
            this.addRowToResidentInfoTable(r);
        }
    }

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

        LoadCSVdata load = new LoadCSVdata();
        datatype.GlobalVariable.RESIDENTS = new ArrayList<>();
        load.LoadData();
        this.setJcheckBoxText();
        this.setJcomboBoxText();
        this.addAllResidentInfoTable();
        load.LoadFeeDataToJTable(this.jTable2, datatype.GlobalVariable.FEE_FILE_PATH);
        JTable[] tmp = { this.jTable2, this.jTable3 };
        datatype.GlobalVariable.REPORT_TABLES = tmp;

    }//GEN-LAST:event_jButton2ActionPerformed

    private void addItemToFeeTable(String s1, String s2, String s3, String s4) {
        DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();
        model.addRow(new Object[] { s1, s2, s3, s4 });
    }

    private String removeItemFromFeeTable(int i) {
        DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();
        String name = (String) model.getValueAt(i, 0);
        model.removeRow(i);
        this.resetButtons();
        return name;
    }

    public void addNewFee() {
        Fee new_fee = new Fee();
        int c = 0;
        String payer = "";
        if (this.jCheckBox1.isSelected()) {
            c++;
            payer = payer + datatype.GlobalVariable.RESIDENTS.get(0).name + ";";
        }
        if (this.jCheckBox2.isSelected()) {
            c++;
            payer = payer + datatype.GlobalVariable.RESIDENTS.get(1).name + ";";
        }
        if (this.jCheckBox3.isSelected()) {
            c++;
            payer = payer + datatype.GlobalVariable.RESIDENTS.get(2).name + ";";
        }
        if (this.jCheckBox4.isSelected()) {
            c++;
            payer = payer + datatype.GlobalVariable.RESIDENTS.get(3).name;
        }
        new_fee.number_of_payer = c;
        new_fee.amount = Double.valueOf(this.jTextField2.getText());
        new_fee.name = this.jTextField1.getText();

        int paid_by_index = this.jComboBox1.getSelectedIndex();
        datatype.GlobalVariable.RESIDENTS.get(paid_by_index).paid.add(new_fee);

        this.addItemToFeeTable(this.jTextField1.getText(), this.jTextField2.getText(),
                datatype.GlobalVariable.RESIDENTS.get(paid_by_index).name, payer);

        if (this.jCheckBox1.isSelected()) {
            datatype.GlobalVariable.RESIDENTS.get(0).extra_fee.add(new_fee);
        }
        if (this.jCheckBox2.isSelected()) {
            datatype.GlobalVariable.RESIDENTS.get(1).extra_fee.add(new_fee);
        }
        if (this.jCheckBox3.isSelected()) {
            datatype.GlobalVariable.RESIDENTS.get(2).extra_fee.add(new_fee);
        }
        if (this.jCheckBox4.isSelected()) {
            datatype.GlobalVariable.RESIDENTS.get(3).extra_fee.add(new_fee);
        }

        this.jTextField1.setText("???");
        this.jTextField2.setText("??");
    }

    public void removeFee(int i) {
        //DefaultTableModel model = (DefaultTableModel) this.jTable2.getModel();

        String name = this.removeItemFromFeeTable(i);
        for (Resident r : datatype.GlobalVariable.RESIDENTS) {
            r.removeExtraFee(name);
            r.removePaid(name);
        }
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (jButton3.getText().equals("")) {
            this.addNewFee();
            //        this.jTable2.getSelectionModel().setSelectionInterval(this.jTable2.getModel().getRowCount()-1, this.jTable2.getModel().getRowCount()-1);
        } else {
            this.removeFee(ROW);
            this.addNewFee();
            //            this.jTable2.getSelectionModel().setSelectionInterval(this.jTable2.getModel().getRowCount()-1, this.jTable2.getModel().getRowCount()-1);
        }
        this.jTable2.getSelectionModel().setSelectionInterval(this.jTable2.getModel().getRowCount() - 1,
                this.jTable2.getModel().getRowCount() - 1);
    }//GEN-LAST:event_jButton3ActionPerformed

    private double computeAvgFee(Resident r) {
        double avg = 0.0;
        System.out.println(r.name + "'s extra fees: " + r.extra_fee.size());
        for (Fee f : r.extra_fee) {
            if (f.name.equals(""))
                continue;
            System.out.println(f.name + ", " + f.amount + ", #: " + f.number_of_payer);
            avg = avg + f.amount / f.number_of_payer;
        }
        return avg;
    }

    private double computePaid(Resident r) {
        double paid = 0.0;
        for (Fee f : r.paid) {
            paid = paid + f.amount;
        }
        return paid;
    }

    private double computeShouldPay(Resident r) {
        return r.basic_rent + this.computeAvgFee(r) - this.computePaid(r);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        for (int i = 0; i < datatype.GlobalVariable.RESIDENTS.size(); i++) {
            System.out.println(datatype.GlobalVariable.RESIDENTS.get(i).name);
            for (Fee f : datatype.GlobalVariable.RESIDENTS.get(i).extra_fee) {
                System.out.println(f.name);
            }
        }
        DefaultTableModel model = (DefaultTableModel) this.jTable3.getModel();
        model.setRowCount(0);
        for (Resident r : datatype.GlobalVariable.RESIDENTS) {
            model.addRow(new Object[] { r.name, String.valueOf(r.basic_rent),
                    String.format("%.2f", this.computeAvgFee(r)),
                    String.format("%.2f", this.computeAvgFee(r) + r.basic_rent),
                    String.format("%.2f", this.computePaid(r)), String.format("%.2f", this.computeShouldPay(r)) });
        }

        this.writeReport();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField1MouseClicked
        // TODO add your handling code here:
        this.jTextField1.setText("");
    }//GEN-LAST:event_jTextField1MouseClicked

    private void jTextField2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField2MouseClicked
        // TODO add your handling code here:
        this.jTextField2.setText("");
    }//GEN-LAST:event_jTextField2MouseClicked

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.resetButtons();
        jTextField1.setText("???");
        jTextField2.setText("??");
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.removeFee(ROW);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        this.openFeeFile(this.jTable4);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        this.openRentFile(this.jTable5);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void setJcomboBoxText() {
        this.jComboBox1.addItem(datatype.GlobalVariable.RESIDENTS.get(0).name);
        this.jComboBox1.addItem(datatype.GlobalVariable.RESIDENTS.get(1).name);
        this.jComboBox1.addItem(datatype.GlobalVariable.RESIDENTS.get(2).name);
        this.jComboBox1.addItem(datatype.GlobalVariable.RESIDENTS.get(3).name);
        //this.jComboBox1.setSelectedIndex(3);
    }

    private void setJcheckBoxText() {
        this.jCheckBox1.setText(datatype.GlobalVariable.RESIDENTS.get(0).name);
        this.jCheckBox2.setText(datatype.GlobalVariable.RESIDENTS.get(1).name);
        this.jCheckBox3.setText(datatype.GlobalVariable.RESIDENTS.get(2).name);
        this.jCheckBox4.setText(datatype.GlobalVariable.RESIDENTS.get(3).name);
    }

    /**
     * @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(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainGUI.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 MainGUI().setVisible(true);
                //LoadCSVdata load = new LoadCSVdata();
                //load.LoadData();
                //setJcomboBoxText();
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable4;
    private javax.swing.JTable jTable5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    private class FeeTableSelectionListener implements ListSelectionListener {

        public FeeTableSelectionListener() {

        }

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                // resetButtons();
                return;
            }
            datatype.GlobalVariable.ROW = jTable2.getSelectedRow();
            System.out.println("row: " + ROW);
            if (ROW < 0) {
                resetButtons();
                return;
            }
            datatype.GlobalVariable.COLUMN = jTable2.getSelectedColumn();
            System.out.println("coloum: " + COLUMN);
            if (COLUMN < 0) {
                resetButtons();
                return;
            }

            DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

            jButton4.setVisible(true);
            jButton5.setVisible(true);
            jButton3.setText("");
            jTextField1.setText((String) model.getValueAt(ROW, 0));
            jTextField2.setText((String) model.getValueAt(ROW, 1));

            String name = (String) model.getValueAt(ROW, 0);
            setJcheckBox(name, RESIDENTS.get(0), jCheckBox1);
            setJcheckBox(name, RESIDENTS.get(1), jCheckBox2);
            setJcheckBox(name, RESIDENTS.get(2), jCheckBox3);
            setJcheckBox(name, RESIDENTS.get(3), jCheckBox4);

            setJcomboBox(name);
        }
    }

    public void setJcheckBox(String name, Resident r, JCheckBox c) {
        if (r.hasFeeToPay(name)) {
            c.setSelected(true);
        } else {
            c.setSelected(false);
        }

    }

    public void setJcomboBox(String name) {
        for (Resident r : RESIDENTS) {
            if (r.hasFeePaid(name)) {
                this.jComboBox1.setSelectedIndex(RESIDENTS.indexOf(r));
                break;
            }
        }
    }

    public void resetButtons() {
        jButton4.setVisible(false);
        jButton5.setVisible(false);
        jButton3.setText("");
        //jTextField1.setText("???");
        //jTextField2.setText("??");
    }

    public String getTableRow(JTable j, int row) {
        DefaultTableModel model = (DefaultTableModel) j.getModel();
        int count = model.getColumnCount();
        String s = "";
        for (int i = 0; i < count; i++) {
            s += model.getValueAt(row, i) + ",";
        }
        s += "\n";
        return s;
    }

    public void writeReport() {
        for (int i = 0; i < REPORT_FILES.length; i++) {
            try {
                //                FileWriter fw = new FileWriter(REPORT_FILES[i]);
                //                File f = new File(REPORT_FILES[i]);
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(REPORT_FILES[i]), "UTF-8"));
                //                BufferedWriter writer = new BufferedWriter(fw);
                DefaultTableModel model = (DefaultTableModel) REPORT_TABLES[i].getModel();
                String s = "";
                for (int k = 0; k < model.getColumnCount(); k++) {
                    s += model.getColumnName(k) + ",";
                }
                s += "\n";
                int rows = model.getRowCount();

                for (int j = 0; j < rows; j++) {
                    s += this.getTableRow(REPORT_TABLES[i], j);
                }
                writer.write(s);
                writer.close();

            } catch (Exception ex) {
            }

        }
    }

    private void openFeeFile(JTable t) {
        File file = this.getSelectedFile();
        if (file != null) {
            this.LoadFeeDataToJTable(t, file);
        }
    }

    private void openRentFile(JTable t) {
        File file = this.getSelectedFile();
        if (file != null) {
            this.LoadRentToJtable(t, file);
        }
    }

    public void LoadFeeDataToJTable(JTable t, File file) {
        try {
            CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT);
            //t.setModel(tm);
            DefaultTableModel model = new DefaultTableModel();
            //model.setRowCount(0);
            for (CSVRecord c : parser) {
                if (c.getRecordNumber() == 1) {

                    model.addColumn(c.get(datatype.GlobalVariable.TYPE));
                    model.addColumn(c.get(datatype.GlobalVariable.AMOUNT));
                    model.addColumn(c.get(datatype.GlobalVariable.PAID_BY));
                    model.addColumn(c.get(datatype.GlobalVariable.PAYER));
                    t.setModel(model);
                    model = (DefaultTableModel) t.getModel();
                    continue;
                }
                model.addRow(
                        new Object[] { c.get(datatype.GlobalVariable.TYPE), c.get(datatype.GlobalVariable.AMOUNT),
                                c.get(datatype.GlobalVariable.PAID_BY), c.get(datatype.GlobalVariable.PAYER) });
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void LoadRentToJtable(JTable t, File file) {
        try {
            CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT);
            //t.setModel(tm);
            DefaultTableModel model = new DefaultTableModel();
            //model.setRowCount(0);
            for (CSVRecord c : parser) {
                if (c.getRecordNumber() == 1) {
                    model.addColumn("??");
                    model.addColumn("");
                    model.addColumn("?");
                    model.addColumn("?");
                    model.addColumn("");
                    model.addColumn("");
                    t.setModel(model);
                    model = (DefaultTableModel) t.getModel();
                    continue;
                }
                model.addRow(new Object[] { c.get(0), c.get(1), c.get(2), c.get(3), c.get(4), c.get(5) });
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private File getSelectedFile() {
        // get file path
        FileFilter filter = new FileNameExtensionFilter("soccer data files", "csv");

        JFileChooser chooser = new JFileChooser(System.getProperty("user.dir"));

        chooser.addChoosableFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        } else
            return null;
    }
}