Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 34.85 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JList;
  5. import javax.swing.JTextField;
  6. import javax.swing.JButton;
  7. import javax.swing.ListModel;
  8.  
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.io.File;
  12. import java.io.IOException;
  13.  
  14. import javax.swing.JScrollPane;
  15. import javax.swing.AbstractListModel;
  16. import javax.swing.JLabel;
  17.  
  18. import java.awt.Font;
  19.  
  20. import javax.swing.SwingConstants;
  21. import javax.swing.ListSelectionModel;
  22. import javax.swing.JPanel;
  23. import javax.swing.border.BevelBorder;
  24. import javax.swing.border.LineBorder;
  25. import javax.swing.event.ListSelectionEvent;
  26. import javax.swing.event.ListSelectionListener;
  27.  
  28. import java.awt.Color;
  29. import java.awt.Desktop;
  30.  
  31. import javax.swing.JTextArea;
  32. import javax.swing.ScrollPaneConstants;
  33.  
  34.  
  35. public class PayrollWindow {
  36.  
  37.     JFrame frame;
  38.     JFrame sortFrame;
  39.     JFrame resetAllConfirmationFrame;
  40.     JFrame printFrame;
  41.     JFrame printConfirmationFrame;
  42.     JFrame addFrame;
  43.     JFrame editFrame;
  44.     JFrame resetConfirmationFrame;
  45.     JFrame deleteConfirmationFrame;
  46.     JFrame saveFrame;
  47.  
  48.     JList<String> list;
  49.     // initialize database
  50.     Database JPAS = new Database("rawdata.txt");
  51.     private JTextField hourlyRateTextField;
  52.     private JTextField hoursWorkedTextField;
  53.     private JTextField totalOwedTextField;
  54.     private JTextField nameTextField;
  55.     private JScrollPane scrollPane_1;
  56.     private JTextField changePositionTextField;
  57.     private JTextField addHoursTextField;
  58.     private JTextField removeHoursTextField;
  59.     private JTextField newRateDollarTextField;
  60.     private JTextField newRateCentTextField;
  61.     private JLabel labelh;
  62.     private JLabel label;
  63.     /**
  64.      * Launch the application.
  65.      */
  66.     public static void main(String[] args) {
  67.         EventQueue.invokeLater(new Runnable() {
  68.             public void run() {
  69.                 try {
  70.                     PayrollWindow window = new PayrollWindow();
  71.                     window.frame.setVisible(true);
  72.                 } catch (Exception e) {
  73.                     e.printStackTrace();
  74.                 }
  75.             }
  76.         });
  77.     }
  78.  
  79.     /**
  80.      * Create the application.
  81.      * @throws IOException
  82.      */
  83.  
  84.     public PayrollWindow() throws IOException {
  85.         initialize();
  86.         //sortFrame();
  87.         //resetConfirmationFrame(0, null, "Bob","$19.85");
  88.         //printFrame();
  89.         //printConfirmationFrame("yo", "yo.csv");
  90.         //addFrame();
  91.         //editFrame(0);
  92.         //deleteConfirmationFrame(0, null,"Bob", "$19.85");
  93.         //saveFrame();
  94.     }
  95.  
  96.     /**
  97.      * Initialize the contents of the frame.
  98.      * @throws IOException
  99.      */
  100.  
  101.     public void refreshList(JList<String> list)
  102.     {
  103.         list.setModel(new AbstractListModel<String>() {
  104.             String[] values = JPAS.getEmployeeList();
  105.             public int getSize() {
  106.                 return values.length;
  107.             }
  108.             public String getElementAt(int index) {
  109.                 return values[index];
  110.             }
  111.         });
  112.     }
  113.  
  114.     private void addListenerToList()
  115.     {
  116.         list.addListSelectionListener(new ListSelectionListener()
  117.         {
  118.             public void valueChanged(ListSelectionEvent e)
  119.             {
  120.                 if (e.getValueIsAdjusting() == false)
  121.                 {
  122.                 }
  123.                 else
  124.                 {                  
  125.                     if(list.getSelectedIndex()!=-1)
  126.                     {
  127.                         // get hourly rate
  128.                         String rateString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate());
  129.                         String rateCents = rateString.substring(rateString.length()-2,rateString.length());
  130.                         String rateDollars = rateString.substring(0,rateString.length()-2);
  131.                         rateString = ("$"+rateDollars+"."+rateCents);
  132.                         hourlyRateTextField.setText(rateString);
  133.  
  134.                         // get hours worked
  135.                         String hoursString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getHoursWorked());
  136.                         hoursWorkedTextField.setText(hoursString);
  137.  
  138.                         // get total owed
  139.                         String totalString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate()*JPAS.employeeDatabase[list.getSelectedIndex()].getHoursWorked());
  140.                         // ensures a case for $0 owed
  141.                         if (totalString.length()==1)
  142.                         {
  143.                             totalString = "000";
  144.                         }      
  145.                         String totalCents = totalString.substring(totalString.length()-2,totalString.length());
  146.                         String totalDollars = totalString.substring(0,totalString.length()-2);
  147.                         totalString = ("$"+totalDollars+"."+totalCents);
  148.                         totalOwedTextField.setText(totalString);
  149.                     }
  150.  
  151.                    
  152.                 }
  153.             }
  154.         });
  155.     }
  156.  
  157.  
  158.  
  159.     private void initialize() throws IOException {
  160.         frame = new JFrame();
  161.         frame.setBounds(100, 100, 579, 401);
  162.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163.         frame.getContentPane().setLayout(null);
  164.         frame.setTitle("JPAS Payroll Manager");
  165.  
  166.         // store database
  167.         JPAS.readFile();
  168.         JPAS.storeDatabase();
  169.  
  170.         JScrollPane scrollPane = new JScrollPane();
  171.         scrollPane.setBounds(34, 36, 243, 265);
  172.         frame.getContentPane().add(scrollPane);
  173.  
  174.         list = new JList<String>();
  175.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  176.         list.setModel(new AbstractListModel<String>() {
  177.             String[] values = JPAS.getEmployeeList();
  178.             public int getSize() {
  179.                 return values.length;
  180.             }
  181.             public String getElementAt(int index) {
  182.                 return values[index];
  183.             }
  184.         });
  185.         addListenerToList();
  186.  
  187.         scrollPane.setViewportView(list);
  188.  
  189.  
  190.         JLabel lblEmployees = new JLabel("Employees");
  191.         lblEmployees.setHorizontalAlignment(SwingConstants.CENTER);
  192.         lblEmployees.setFont(new Font("Tahoma", Font.PLAIN, 13));
  193.         lblEmployees.setBounds(34, 11, 243, 21);
  194.         frame.getContentPane().add(lblEmployees);
  195.  
  196.         JButton btnAdd = new JButton("Add Employee");
  197.         btnAdd.addActionListener(new ActionListener() {
  198.             public void actionPerformed(ActionEvent e) {
  199.                 addFrame();
  200.                 frame.setVisible(false);
  201.             }
  202.         });
  203.         btnAdd.setBounds(348, 36, 144, 23);
  204.         frame.getContentPane().add(btnAdd);
  205.  
  206.         JButton btnSaveDatabase = new JButton("Save Database");
  207.         btnSaveDatabase.addActionListener(new ActionListener() {
  208.             public void actionPerformed(ActionEvent e) {
  209.                 try {
  210.                     saveFrame();
  211.                 } catch (IOException e1) {
  212.                     // TODO Auto-generated catch block
  213.                     e1.printStackTrace();
  214.                 }
  215.                 frame.setVisible(false);
  216.             }
  217.         });
  218.         btnSaveDatabase.setBounds(348, 315, 144, 23);
  219.         frame.getContentPane().add(btnSaveDatabase);
  220.  
  221.         JPanel infoPanel = new JPanel();
  222.         infoPanel.setBorder(new LineBorder(Color.GRAY));
  223.         infoPanel.setBounds(318, 78, 207, 92);
  224.         frame.getContentPane().add(infoPanel);
  225.         infoPanel.setLayout(null);
  226.  
  227.         JLabel lblTotalOwed = new JLabel("Total Owed:");
  228.         lblTotalOwed.setBounds(20, 64, 88, 14);
  229.         infoPanel.add(lblTotalOwed);
  230.  
  231.         totalOwedTextField = new JTextField();
  232.         totalOwedTextField.setBounds(118, 61, 73, 20);
  233.         infoPanel.add(totalOwedTextField);
  234.         totalOwedTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  235.         totalOwedTextField.setEditable(false);
  236.         totalOwedTextField.setColumns(10);
  237.  
  238.         hoursWorkedTextField = new JTextField();
  239.         hoursWorkedTextField.setBounds(118, 36, 73, 20);
  240.         infoPanel.add(hoursWorkedTextField);
  241.         hoursWorkedTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  242.         hoursWorkedTextField.setEditable(false);
  243.         hoursWorkedTextField.setColumns(10);
  244.  
  245.         JLabel lblHoursWorked = new JLabel("Hours Worked:");
  246.         lblHoursWorked.setBounds(20, 39, 88, 14);
  247.         infoPanel.add(lblHoursWorked);
  248.  
  249.         JLabel lblHourlyRate = new JLabel("Hourly Rate:");
  250.         lblHourlyRate.setBounds(20, 14, 88, 14);
  251.         infoPanel.add(lblHourlyRate);
  252.  
  253.         hourlyRateTextField = new JTextField();
  254.         hourlyRateTextField.setBounds(118, 11, 73, 20);
  255.         infoPanel.add(hourlyRateTextField);
  256.         hourlyRateTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  257.         hourlyRateTextField.setEditable(false);
  258.         hourlyRateTextField.setColumns(10);
  259.  
  260.         JButton btnEditEmployee = new JButton("Edit Employee");
  261.         btnEditEmployee.addActionListener(new ActionListener() {
  262.             public void actionPerformed(ActionEvent e) {
  263.                 if(list.getSelectedIndex() != -1)
  264.                 {
  265.                     editFrame(list.getSelectedIndex());
  266.                     frame.setVisible(false);
  267.                 }
  268.  
  269.             }
  270.         });
  271.         btnEditEmployee.setBounds(348, 186, 144, 23);
  272.         frame.getContentPane().add(btnEditEmployee);
  273.  
  274.         JButton btnPrintStatements = new JButton("Print Statements");
  275.         btnPrintStatements.addActionListener(new ActionListener() {
  276.             public void actionPerformed(ActionEvent e) {
  277.                 printFrame();
  278.                 frame.setVisible(false);
  279.             }
  280.         });
  281.         btnPrintStatements.setBounds(348, 228, 144, 23);
  282.         frame.getContentPane().add(btnPrintStatements);
  283.  
  284.         JButton btnSort = new JButton("Sort");
  285.         btnSort.addActionListener(new ActionListener() {
  286.             public void actionPerformed(ActionEvent e) {
  287.                 sortFrame();
  288.                 frame.setVisible(false);;
  289.             }
  290.         });
  291.         btnSort.setBounds(34, 316, 80, 23);
  292.         frame.getContentPane().add(btnSort);
  293.  
  294.         JButton btnResetAllHours = new JButton("Reset All Hours");
  295.         btnResetAllHours.addActionListener(new ActionListener() {
  296.             public void actionPerformed(ActionEvent e) {
  297.                 resetAllConfirmationFrame();
  298.                 frame.setVisible(false);
  299.             }
  300.         });
  301.         btnResetAllHours.setBounds(124, 316, 150, 23);
  302.         frame.getContentPane().add(btnResetAllHours);
  303.  
  304.        
  305.         labelh = new JLabel();
  306.         labelh.setHorizontalAlignment(SwingConstants.CENTER);
  307.         labelh.setBounds(287, 252, 266, 29);
  308.         frame.getContentPane().add(labelh);
  309.  
  310.         label = new JLabel();
  311.         label.setHorizontalAlignment(SwingConstants.CENTER);
  312.         label.setBounds(287, 272, 266, 29);
  313.         frame.getContentPane().add(label);
  314.  
  315.         resetTotals();
  316.     }
  317.    
  318.     public void resetTotals()
  319.     {
  320.         labelh.setText("Total Hours Worked: "+JPAS.totalHours()+" hours");
  321.         label.setText("Total Hours Worked: "+JPAS.totalOwed());
  322.     }
  323.    
  324.     public void sortFrame()
  325.     {
  326.         sortFrame = new JFrame();
  327.         sortFrame.setBounds(100, 100, 450, 300);
  328.         sortFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  329.         sortFrame.setVisible(true);
  330.         sortFrame.setTitle("Sort Employees");
  331.  
  332.         final String filename = "statement.csv";
  333.  
  334.         JButton btnBack = new JButton("Back");
  335.         btnBack.setBounds(20, 10, 65, 23);
  336.         btnBack.addActionListener(new ActionListener() {
  337.             public void actionPerformed(ActionEvent e) {
  338.                 frame.setVisible(true);
  339.                 sortFrame.dispose();
  340.             }
  341.         });
  342.         sortFrame.getContentPane().setLayout(null);
  343.         sortFrame.getContentPane().add(btnBack);
  344.  
  345.         JLabel lblPrintInAlphabetical = new JLabel("Sort in alphabetical order of last names");
  346.         lblPrintInAlphabetical.setFont(new Font("Tahoma", Font.PLAIN, 14));
  347.         lblPrintInAlphabetical.setBounds(54, 70, 261, 17);
  348.         sortFrame.getContentPane().add(lblPrintInAlphabetical);
  349.  
  350.         JLabel lblPrintInAscending = new JLabel("Sort in ascending order of hourly rate");
  351.         lblPrintInAscending.setFont(new Font("Tahoma", Font.PLAIN, 14));
  352.         lblPrintInAscending.setBounds(54, 105, 261, 17);
  353.         sortFrame.getContentPane().add(lblPrintInAscending);
  354.  
  355.         JLabel lblSortInAscending = new JLabel("Sort in ascending order of hours worked");
  356.         lblSortInAscending.setFont(new Font("Tahoma", Font.PLAIN, 14));
  357.         lblSortInAscending.setBounds(54, 140, 261, 17);
  358.         sortFrame.getContentPane().add(lblSortInAscending);
  359.  
  360.         JLabel lblPrintInAscending_1 = new JLabel("Sort in ascending order of amount owed");
  361.         lblPrintInAscending_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  362.         lblPrintInAscending_1.setBounds(54, 175, 261, 17);
  363.         sortFrame.getContentPane().add(lblPrintInAscending_1);
  364.  
  365.         JButton btnPrintAlphabetical = new JButton("Sort");
  366.         btnPrintAlphabetical.addActionListener(new ActionListener() {
  367.             public void actionPerformed(ActionEvent arg0) {
  368.                 JPAS.sortLastName();
  369.                 refreshList(list);
  370.                 frame.setVisible(true);
  371.                 sortFrame.dispose();
  372.             }
  373.         });
  374.         btnPrintAlphabetical.setBounds(317, 70, 65, 23);
  375.         sortFrame.getContentPane().add(btnPrintAlphabetical);
  376.  
  377.         JButton btnPrintHourlyRate = new JButton("Sort");
  378.         btnPrintHourlyRate.addActionListener(new ActionListener() {
  379.             public void actionPerformed(ActionEvent e) {
  380.                 JPAS.sortHourlyRate();
  381.                 refreshList(list);
  382.                 frame.setVisible(true);
  383.                 sortFrame.dispose();
  384.             }
  385.         });
  386.         btnPrintHourlyRate.setBounds(317, 104, 65, 23);
  387.         sortFrame.getContentPane().add(btnPrintHourlyRate);
  388.  
  389.         JButton btnPrintHoursWorked = new JButton("Sort");
  390.         btnPrintHoursWorked.addActionListener(new ActionListener() {
  391.             public void actionPerformed(ActionEvent e) {
  392.                 JPAS.sortHoursWorked();
  393.                 refreshList(list);
  394.                 frame.setVisible(true);
  395.                 sortFrame.dispose();
  396.             }
  397.         });
  398.         btnPrintHoursWorked.setBounds(317, 140, 65, 23);
  399.         sortFrame.getContentPane().add(btnPrintHoursWorked);
  400.  
  401.         JButton btnPrintOwed = new JButton("Sort");
  402.         btnPrintOwed.addActionListener(new ActionListener() {
  403.             public void actionPerformed(ActionEvent e) {
  404.                 JPAS.sortTotalOwed();
  405.                 refreshList(list);
  406.                 frame.setVisible(true);
  407.                 sortFrame.dispose();
  408.             }
  409.         });
  410.         btnPrintOwed.setBounds(317, 175, 65, 23);
  411.         sortFrame.getContentPane().add(btnPrintOwed);
  412.     }
  413.  
  414.  
  415.     public void resetAllConfirmationFrame()
  416.     {
  417.         resetAllConfirmationFrame = new JFrame();
  418.         resetAllConfirmationFrame.setBounds(125, 200, 400, 150);
  419.         resetAllConfirmationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  420.         resetAllConfirmationFrame.getContentPane().setLayout(null);
  421.         resetAllConfirmationFrame.setVisible(true);
  422.         resetAllConfirmationFrame.setTitle("Warning - Reset All Hours?");
  423.  
  424.         JLabel confirmation = new JLabel("<html>Have you fully paid all employees what is<br>owed and would like to reset all hours to 0?</html>");
  425.         confirmation.setFont(new Font("Tahoma", Font.PLAIN, 13));
  426.         confirmation.setHorizontalAlignment(SwingConstants.CENTER);
  427.         confirmation.setBounds(10, 11, 364, 47);
  428.         resetAllConfirmationFrame.getContentPane().add(confirmation);
  429.  
  430.         JButton btnYes = new JButton("Yes");
  431.         btnYes.addActionListener(new ActionListener() {
  432.             public void actionPerformed(ActionEvent arg0) {
  433.                 for(int i=0; i<JPAS.getEmployeeCount(); i++)
  434.                 {
  435.                     JPAS.employeeDatabase[i].setHoursWorked(0);
  436.                 }
  437.                 frame.setVisible(true);
  438.                 resetAllConfirmationFrame.dispose();
  439.  
  440.                 list.setModel(new AbstractListModel<String>() {
  441.                     String[] values = JPAS.getEmployeeList();
  442.                     public int getSize() {
  443.                         return values.length;
  444.                     }
  445.                     public String getElementAt(int index) {
  446.                         return values[index];
  447.                     }
  448.                 });
  449.             }
  450.         });
  451.         btnYes.setBounds(96, 69, 67, 23);
  452.         resetAllConfirmationFrame.getContentPane().add(btnYes);
  453.  
  454.         JButton btnNo = new JButton("No");
  455.         btnNo.addActionListener(new ActionListener() {
  456.             public void actionPerformed(ActionEvent n) {
  457.                 frame.setVisible(true);
  458.                 resetAllConfirmationFrame.dispose();
  459.             }
  460.         });
  461.         btnNo.setBounds(210, 69, 67, 23);
  462.         resetAllConfirmationFrame.getContentPane().add(btnNo);
  463.  
  464.     }
  465.  
  466.     public void printFrame()
  467.     {
  468.         printFrame = new JFrame();
  469.         printFrame.setBounds(100, 100, 450, 300);
  470.         printFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  471.         printFrame.setVisible(true);
  472.         printFrame.setTitle("Print Employee Information");
  473.  
  474.         final String filename = "statement.csv";
  475.  
  476.         JButton btnBack = new JButton("Back");
  477.         btnBack.setBounds(20, 10, 65, 23);
  478.         btnBack.addActionListener(new ActionListener() {
  479.             public void actionPerformed(ActionEvent e) {
  480.                 frame.setVisible(true);
  481.                 printFrame.dispose();
  482.             }
  483.         });
  484.         printFrame.getContentPane().setLayout(null);
  485.         printFrame.getContentPane().add(btnBack);
  486.  
  487.         JLabel lblPrintInAlphabetical = new JLabel("Print in alphabetical order of last names");
  488.         lblPrintInAlphabetical.setFont(new Font("Tahoma", Font.PLAIN, 14));
  489.         lblPrintInAlphabetical.setBounds(54, 70, 261, 17);
  490.         printFrame.getContentPane().add(lblPrintInAlphabetical);
  491.  
  492.         JLabel lblPrintInAscending = new JLabel("Print in ascending order of hourly rate");
  493.         lblPrintInAscending.setFont(new Font("Tahoma", Font.PLAIN, 14));
  494.         lblPrintInAscending.setBounds(54, 105, 261, 17);
  495.         printFrame.getContentPane().add(lblPrintInAscending);
  496.  
  497.         JLabel label = new JLabel("Print in ascending order of hours worked");
  498.         label.setFont(new Font("Tahoma", Font.PLAIN, 14));
  499.         label.setBounds(54, 140, 261, 17);
  500.         printFrame.getContentPane().add(label);
  501.  
  502.         JLabel lblPrintInAscending_1 = new JLabel("Print in ascending order of amount owed");
  503.         lblPrintInAscending_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  504.         lblPrintInAscending_1.setBounds(54, 175, 261, 17);
  505.         printFrame.getContentPane().add(lblPrintInAscending_1);
  506.  
  507.         JButton btnPrintAlphabetical = new JButton("Print");
  508.         btnPrintAlphabetical.addActionListener(new ActionListener() {
  509.             public void actionPerformed(ActionEvent arg0) {
  510.                 printConfirmationFrame("alphabetical",filename);
  511.                 printFrame.dispose();
  512.             }
  513.         });
  514.         btnPrintAlphabetical.setBounds(317, 70, 65, 23);
  515.         printFrame.getContentPane().add(btnPrintAlphabetical);
  516.  
  517.         JButton btnPrintHourlyRate = new JButton("Print");
  518.         btnPrintHourlyRate.addActionListener(new ActionListener() {
  519.             public void actionPerformed(ActionEvent e) {
  520.                 printConfirmationFrame("hourlyRate",filename);
  521.                 printFrame.dispose();
  522.             }
  523.         });
  524.         btnPrintHourlyRate.setBounds(317, 104, 65, 23);
  525.         printFrame.getContentPane().add(btnPrintHourlyRate);
  526.  
  527.         JButton btnPrintHoursWorked = new JButton("Print");
  528.         btnPrintHoursWorked.addActionListener(new ActionListener() {
  529.             public void actionPerformed(ActionEvent e) {
  530.                 printConfirmationFrame("hoursWorked",filename);
  531.                 printFrame.dispose();
  532.             }
  533.         });
  534.         btnPrintHoursWorked.setBounds(317, 140, 65, 23);
  535.         printFrame.getContentPane().add(btnPrintHoursWorked);
  536.  
  537.         JButton btnPrintOwed = new JButton("Print");
  538.         btnPrintOwed.addActionListener(new ActionListener() {
  539.             public void actionPerformed(ActionEvent e) {
  540.                 printConfirmationFrame("totalOwed",filename);
  541.                 printFrame.dispose();
  542.             }
  543.         });
  544.         btnPrintOwed.setBounds(317, 175, 65, 23);
  545.         printFrame.getContentPane().add(btnPrintOwed);
  546.     }
  547.  
  548.     public void printConfirmationFrame(final String order, final String filename)
  549.     {
  550.         printConfirmationFrame = new JFrame();
  551.         printConfirmationFrame.setBounds(125, 200, 400, 150);
  552.         printConfirmationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  553.         printConfirmationFrame.getContentPane().setLayout(null);
  554.         printConfirmationFrame.setTitle("Print CSV Info");
  555.  
  556.         JScrollPane scrollPane = new JScrollPane();
  557.         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  558.         scrollPane.setBounds(10, 11, 364, 57);
  559.         printConfirmationFrame.getContentPane().add(scrollPane);
  560.  
  561.         JTextArea textArea = new JTextArea();
  562.         scrollPane.setViewportView(textArea);
  563.         textArea.setLineWrap(true);
  564.         textArea.setEditable(false);
  565.         textArea.setText("Payroll Manager will close and a statement will be saved and opened. It is titled '"+filename+"' and is stored at '"+System.getProperty("user.dir")+"/"+filename+"'.");
  566.  
  567.         JButton btnOkay = new JButton("Okay");
  568.         btnOkay.addActionListener(new ActionListener() {
  569.             public void actionPerformed(ActionEvent e) {
  570.                 try {
  571.                     JPAS.saveDatabase();
  572.                 } catch (IOException e2) {
  573.                     // TODO Auto-generated catch block
  574.                     e2.printStackTrace();
  575.                 }
  576.                 if(order.equals("alphabetical"))
  577.                 {
  578.                     JPAS.sortLastName();
  579.                 }
  580.                 else if(order.equals("hourlyRate"))
  581.                 {
  582.                     JPAS.sortHourlyRate();
  583.                 }
  584.                 else if(order.equals("hoursWorked"))
  585.                 {
  586.                     JPAS.sortHoursWorked();
  587.                 }
  588.                 else if(order.equals("totalOwed"))
  589.                 {
  590.                     JPAS.sortTotalOwed();
  591.                 }
  592.                 try {
  593.                     JPAS.saveCSV(filename);
  594.                     if (Desktop.isDesktopSupported())
  595.                     {
  596.                         Desktop.getDesktop().open(new File("statement.csv"));
  597.                     }
  598.                 } catch (IOException e1) {
  599.                     // TODO Auto-generated catch block
  600.                     e1.printStackTrace();
  601.                 }
  602.                 printConfirmationFrame.dispose();
  603.                 frame.dispose();
  604.             }
  605.         });
  606.         btnOkay.setBounds(156, 77, 66, 23);
  607.         printConfirmationFrame.getContentPane().add(btnOkay);
  608.         printConfirmationFrame.setVisible(true);
  609.     }
  610.  
  611.     public void addFrame()
  612.     {
  613.         addFrame = new JFrame();
  614.         addFrame.setBounds(100, 100, 450, 300);
  615.         addFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  616.         addFrame.getContentPane().setLayout(null);
  617.         addFrame.setVisible(true);
  618.         addFrame.setTitle("Add Employee");
  619.  
  620.         JLabel lblAddEmployee = new JLabel("Add Employee");
  621.         lblAddEmployee.setFont(new Font("Tahoma", Font.PLAIN, 14));
  622.         lblAddEmployee.setVerticalAlignment(SwingConstants.BOTTOM);
  623.         lblAddEmployee.setHorizontalAlignment(SwingConstants.CENTER);
  624.         lblAddEmployee.setBounds(10, 11, 414, 17);
  625.         addFrame.getContentPane().add(lblAddEmployee);
  626.  
  627.         JLabel lblNewLabel = new JLabel("Last Name");
  628.         lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
  629.         lblNewLabel.setBounds(101, 54, 96, 26);
  630.         addFrame.getContentPane().add(lblNewLabel);
  631.  
  632.         JLabel lblFirstName = new JLabel("First Name");
  633.         lblFirstName.setFont(new Font("Tahoma", Font.PLAIN, 12));
  634.         lblFirstName.setBounds(101, 86, 96, 26);
  635.         addFrame.getContentPane().add(lblFirstName);
  636.  
  637.         JLabel lblPosition = new JLabel("Position");
  638.         lblPosition.setFont(new Font("Tahoma", Font.PLAIN, 12));
  639.         lblPosition.setBounds(101, 118, 96, 26);
  640.         addFrame.getContentPane().add(lblPosition);
  641.  
  642.         JLabel lblHourlyRate = new JLabel("Hourly Rate");
  643.         lblHourlyRate.setFont(new Font("Tahoma", Font.PLAIN, 12));
  644.         lblHourlyRate.setBounds(101, 150, 96, 26);
  645.         addFrame.getContentPane().add(lblHourlyRate);
  646.  
  647.         final JTextField lastNameField = new JTextField();
  648.         lastNameField.setBounds(207, 59, 124, 17);
  649.         addFrame.getContentPane().add(lastNameField);
  650.         lastNameField.setColumns(10);
  651.  
  652.         final JTextField firstNameField = new JTextField();
  653.         firstNameField.setColumns(10);
  654.         firstNameField.setBounds(207, 91, 124, 17);
  655.         addFrame.getContentPane().add(firstNameField);
  656.  
  657.         final JTextField positionField = new JTextField();
  658.         positionField.setColumns(10);
  659.         positionField.setBounds(207, 123, 124, 17);
  660.         addFrame.getContentPane().add(positionField);
  661.  
  662.         final JTextField hourlyRateDollarsField = new JTextField();
  663.         hourlyRateDollarsField.setColumns(10);
  664.         hourlyRateDollarsField.setBounds(207, 155, 55, 17);
  665.         addFrame.getContentPane().add(hourlyRateDollarsField);
  666.  
  667.         final JTextField hourlyRateCentsField = new JTextField();
  668.         hourlyRateCentsField.setColumns(10);
  669.         hourlyRateCentsField.setBounds(276, 155, 55, 17);
  670.         addFrame.getContentPane().add(hourlyRateCentsField);
  671.  
  672.         JButton btnAdd = new JButton("Add");
  673.         btnAdd.addActionListener(new ActionListener() {
  674.             public void actionPerformed(ActionEvent e) {
  675.                 try {
  676.                     JPAS.addEmployee(lastNameField.getText(), firstNameField.getText(), positionField.getText(), Integer.parseInt((hourlyRateDollarsField.getText())+(hourlyRateCentsField.getText())), 0);
  677.                     list.setModel(new AbstractListModel<String>() {
  678.                         String[] values = JPAS.getEmployeeList();
  679.                         public int getSize() {
  680.                             return values.length;
  681.                         }
  682.                         public String getElementAt(int index) {
  683.                             return values[index];
  684.                         }
  685.                     });
  686.                 } catch (Exception e1) {
  687.                     // TODO Auto-generated catch block
  688.                     e1.printStackTrace();
  689.                 }
  690.                 resetTotals();
  691.                 frame.setVisible(true);
  692.                 addFrame.dispose();
  693.             }
  694.         });
  695.         btnAdd.setBounds(182, 204, 65, 23);
  696.         addFrame.getContentPane().add(btnAdd);
  697.  
  698.         JLabel label = new JLabel(".");
  699.         label.setHorizontalAlignment(SwingConstants.CENTER);
  700.         label.setFont(new Font("Tahoma", Font.PLAIN, 12));
  701.         label.setBounds(262, 150, 14, 26);
  702.         addFrame.getContentPane().add(label);
  703.  
  704.         JButton btnBack = new JButton("Back");
  705.         btnBack.addActionListener(new ActionListener() {
  706.             public void actionPerformed(ActionEvent e) {
  707.                 frame.setVisible(true);
  708.                 addFrame.dispose();
  709.             }
  710.         });
  711.         btnBack.setBounds(20, 10, 65, 23);
  712.         addFrame.getContentPane().add(btnBack);
  713.     }
  714.  
  715.     public void refreshInfoTextArea(int index, JTextArea infoTextArea)
  716.     {
  717.  
  718.         // get hourly rate
  719.         String rateString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate());
  720.         String rateCents = rateString.substring(rateString.length()-2,rateString.length());
  721.         String rateDollars = rateString.substring(0,rateString.length()-2);
  722.  
  723.         // get total owed
  724.         String totalString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate()*JPAS.employeeDatabase[list.getSelectedIndex()].getHoursWorked());
  725.         // ensures a case for $0 owed
  726.         if (totalString.length()==1)
  727.         {
  728.             totalString = "000";
  729.         }
  730.         // put together total owed
  731.  
  732.         String totalCents = totalString.substring(totalString.length()-2,totalString.length());
  733.         String totalDollars = totalString.substring(0,totalString.length()-2);
  734.  
  735.         String newline = System.getProperty("line.separator");
  736.         infoTextArea.setText(   "Position: "+JPAS.employeeDatabase[index].getJobTitle() + newline
  737.                 +   "Hourly Rate: $"+rateDollars+"."+rateCents + newline
  738.                 +   "Hours Worked: "+String.valueOf(JPAS.employeeDatabase[index].getHoursWorked()) + newline
  739.                 +   "Total Owed: $"+totalDollars+"."+totalCents);
  740.     }
  741.  
  742.     public void editFrame(final int index)
  743.     {
  744.         // clear get info text fields
  745.         hourlyRateTextField.setText("");
  746.         hoursWorkedTextField.setText("");
  747.         totalOwedTextField.setText("");
  748.        
  749.         editFrame = new JFrame();
  750.         editFrame.setBounds(100, 100, 450, 300);
  751.         editFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  752.         editFrame.getContentPane().setLayout(null);
  753.         editFrame.setTitle(JPAS.employeeDatabase[index].getFirstName()+" "+JPAS.employeeDatabase[index].getLastName());
  754.  
  755.         nameTextField = new JTextField();
  756.         nameTextField.setHorizontalAlignment(SwingConstants.CENTER);
  757.         nameTextField.setEditable(false);
  758.         nameTextField.setColumns(1);
  759.         nameTextField.setBounds(20, 64, 152, 20);
  760.         editFrame.getContentPane().add(nameTextField);
  761.  
  762.         scrollPane_1 = new JScrollPane();
  763.         scrollPane_1.setBounds(20, 95, 152, 83);
  764.         editFrame.getContentPane().add(scrollPane_1);
  765.         editFrame.setVisible(true);
  766.  
  767.         // get hourly rate
  768.         String rateString = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate());
  769.         String rateCents = rateString.substring(rateString.length()-2,rateString.length());
  770.         String rateDollars = rateString.substring(0,rateString.length()-2);
  771.  
  772.         // get total owed
  773.         String totalTemp = String.valueOf(JPAS.employeeDatabase[list.getSelectedIndex()].getRate()*JPAS.employeeDatabase[list.getSelectedIndex()].getHoursWorked());
  774.         // ensures a 0 case
  775.         if(totalTemp.length()==1)
  776.         {
  777.             totalTemp=totalTemp+"00";
  778.         }
  779.  
  780.         // put together total owed
  781.         String totalCents = totalTemp.substring(totalTemp.length()-2,totalTemp.length());
  782.         String totalDollars = totalTemp.substring(0,totalTemp.length()-2);
  783.         final String totalString = "$"+totalDollars+"."+totalCents;
  784.  
  785.         final JTextArea infoTextArea = new JTextArea();
  786.         scrollPane_1.setViewportView(infoTextArea);
  787.         infoTextArea.setEditable(false);
  788.  
  789.         refreshInfoTextArea(index, infoTextArea);
  790.  
  791.         nameTextField.setText(JPAS.employeeDatabase[index].getFirstName()+" "+JPAS.employeeDatabase[index].getLastName());
  792.  
  793.         JPanel panel = new JPanel();
  794.         panel.setBounds(200, 52, 224, 142);
  795.         panel.setBorder(new LineBorder(Color.GRAY));
  796.         editFrame.getContentPane().add(panel);
  797.         panel.setLayout(null);
  798.  
  799.         JLabel lblChangePosition = new JLabel("New Position");
  800.         lblChangePosition.setBounds(6, 11, 90, 14);
  801.         panel.add(lblChangePosition);
  802.  
  803.         JLabel lblAddHours = new JLabel("Add Hours");
  804.         lblAddHours.setBounds(6, 36, 90, 14);
  805.         panel.add(lblAddHours);
  806.  
  807.         JLabel lblRemoveHours = new JLabel("Remove Hours");
  808.         lblRemoveHours.setBounds(6, 61, 90, 14);
  809.         panel.add(lblRemoveHours);
  810.  
  811.         JLabel lblNewRate = new JLabel("New Rate");
  812.         lblNewRate.setBounds(6, 86, 90, 14);
  813.         panel.add(lblNewRate);
  814.  
  815.         changePositionTextField = new JTextField();
  816.         changePositionTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  817.         changePositionTextField.setBounds(120, 8, 94, 20);
  818.         panel.add(changePositionTextField);
  819.         changePositionTextField.setColumns(10);
  820.  
  821.         addHoursTextField = new JTextField();
  822.         addHoursTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  823.         addHoursTextField.setColumns(10);
  824.         addHoursTextField.setBounds(120, 33, 94, 20);
  825.         panel.add(addHoursTextField);
  826.  
  827.         removeHoursTextField = new JTextField();
  828.         removeHoursTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  829.         removeHoursTextField.setColumns(10);
  830.         removeHoursTextField.setBounds(120, 58, 94, 20);
  831.         panel.add(removeHoursTextField);
  832.  
  833.         newRateDollarTextField = new JTextField();
  834.         newRateDollarTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  835.         newRateDollarTextField.setColumns(10);
  836.         newRateDollarTextField.setBounds(120, 83, 44, 20);
  837.         panel.add(newRateDollarTextField);
  838.  
  839.         newRateCentTextField = new JTextField();
  840.         newRateCentTextField.setHorizontalAlignment(SwingConstants.RIGHT);
  841.         newRateCentTextField.setColumns(10);
  842.         newRateCentTextField.setBounds(170, 83, 44, 20);
  843.         panel.add(newRateCentTextField);
  844.  
  845.         changePositionTextField.setText(JPAS.employeeDatabase[index].getJobTitle());
  846.         addHoursTextField.setText("0");
  847.         removeHoursTextField.setText("0");
  848.         newRateDollarTextField.setText(rateDollars);
  849.         newRateCentTextField.setText(rateCents);
  850.  
  851.         JButton btnMakeChanges = new JButton("Make Changes");
  852.         btnMakeChanges.addActionListener(new ActionListener() {
  853.             public void actionPerformed(ActionEvent e) {
  854.                 JPAS.employeeDatabase[index].setJobTitle(changePositionTextField.getText());
  855.                 JPAS.employeeDatabase[index].setHoursWorked(JPAS.employeeDatabase[index].getHoursWorked()+(Integer.valueOf(addHoursTextField.getText()))-(Integer.valueOf(removeHoursTextField.getText())));
  856.                 JPAS.employeeDatabase[index].setRate(Integer.valueOf(newRateDollarTextField.getText()+newRateCentTextField.getText()));
  857.                 refreshInfoTextArea(index, infoTextArea);
  858.                 resetTotals();
  859.             }
  860.         });
  861.         btnMakeChanges.setBounds(52, 111, 118, 23);
  862.         panel.add(btnMakeChanges);
  863.  
  864.  
  865.         JLabel label = new JLabel(".");
  866.         label.setBounds(156, 86, 20, 14);
  867.         panel.add(label);
  868.         label.setHorizontalAlignment(SwingConstants.CENTER);
  869.  
  870.         JButton btnBack = new JButton("Back");
  871.         btnBack.addActionListener(new ActionListener() {
  872.             public void actionPerformed(ActionEvent e) {
  873.                 frame.setVisible(true);
  874.                 editFrame.dispose();
  875.             }
  876.         });
  877.         btnBack.setBounds(20, 10, 65, 23);
  878.         editFrame.getContentPane().add(btnBack);
  879.  
  880.         // get name string
  881.         final String name = JPAS.employeeDatabase[index].getFirstName()+" "+JPAS.employeeDatabase[index].getLastName();
  882.  
  883.         JButton btnResetHours = new JButton("Reset Hours");
  884.         btnResetHours.addActionListener(new ActionListener() {
  885.             public void actionPerformed(ActionEvent arg0) {
  886.                 resetConfirmationFrame(index, infoTextArea, name, totalString);
  887.                 editFrame.setVisible(false);
  888.             }
  889.         });
  890.         btnResetHours.setBounds(36, 211, 118, 23);
  891.         editFrame.getContentPane().add(btnResetHours);
  892.  
  893.         JButton btnFireEmployee = new JButton("Fire Employee");
  894.         btnFireEmployee.addActionListener(new ActionListener() {
  895.             public void actionPerformed(ActionEvent e) {
  896.                 deleteConfirmationFrame(index, infoTextArea, name, totalString);
  897.                 editFrame.setVisible(false);
  898.             }
  899.         });
  900.         btnFireEmployee.setBounds(252, 211, 118, 23);
  901.         editFrame.getContentPane().add(btnFireEmployee);
  902.  
  903.     }
  904.  
  905.     public void resetConfirmationFrame (final int index, final JTextArea infoTextArea , String name, String owed)
  906.     {
  907.         resetConfirmationFrame = new JFrame();
  908.         resetConfirmationFrame.setBounds(125, 200, 400, 150);
  909.         resetConfirmationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  910.         resetConfirmationFrame.getContentPane().setLayout(null);
  911.         resetConfirmationFrame.setVisible(true);
  912.         resetConfirmationFrame.setTitle("Reset "+JPAS.employeeDatabase[index].getFirstName()+" "+JPAS.employeeDatabase[index].getLastName()+"'s Hours");
  913.  
  914.         JLabel confirmation = new JLabel("<html>Have you fully paid "+name+" the "+owed+"<br>that is owed and would like to reset his hours to 0?</html>");
  915.         confirmation.setFont(new Font("Tahoma", Font.PLAIN, 13));
  916.         confirmation.setHorizontalAlignment(SwingConstants.CENTER);
  917.         confirmation.setBounds(10, 11, 364, 47);
  918.         resetConfirmationFrame.getContentPane().add(confirmation);
  919.  
  920.         JButton btnYes = new JButton("Yes");
  921.         btnYes.addActionListener(new ActionListener() {
  922.             public void actionPerformed(ActionEvent arg0) {
  923.                 JPAS.employeeDatabase[index].setHoursWorked(0);
  924.                 refreshInfoTextArea(index, infoTextArea);
  925.  
  926.                 resetTotals();
  927.                 editFrame.setVisible(true);
  928.                 resetConfirmationFrame.dispose();
  929.             }
  930.         });
  931.         btnYes.setBounds(96, 69, 67, 23);
  932.         resetConfirmationFrame.getContentPane().add(btnYes);
  933.  
  934.         JButton btnNo = new JButton("No");
  935.         btnNo.addActionListener(new ActionListener() {
  936.             public void actionPerformed(ActionEvent n) {
  937.                 editFrame.setVisible(true);
  938.                 resetConfirmationFrame.dispose();
  939.             }
  940.         });
  941.         btnNo.setBounds(210, 69, 67, 23);
  942.         resetConfirmationFrame.getContentPane().add(btnNo);
  943.  
  944.     }
  945.  
  946.     public void deleteConfirmationFrame (final int index, final JTextArea infoTextArea , String name, String owed)
  947.     {
  948.         deleteConfirmationFrame = new JFrame();
  949.         deleteConfirmationFrame.setBounds(125, 200, 400, 150);
  950.         deleteConfirmationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  951.         deleteConfirmationFrame.getContentPane().setLayout(null);
  952.         deleteConfirmationFrame.setVisible(true);
  953.         deleteConfirmationFrame.setTitle("Warning: Fire "+JPAS.employeeDatabase[index].getFirstName()+" "+JPAS.employeeDatabase[index].getLastName()+"?");
  954.  
  955.         JLabel confirmation = new JLabel("<html>Have you fully paid "+name+" the "+owed+"<br>that is owed and would like to remove<br>this employee and all data associated?</html>");
  956.         confirmation.setFont(new Font("Tahoma", Font.PLAIN, 13));
  957.         confirmation.setHorizontalAlignment(SwingConstants.CENTER);
  958.         confirmation.setBounds(10, 11, 364, 47);
  959.         deleteConfirmationFrame.getContentPane().add(confirmation);
  960.  
  961.         JButton btnYes = new JButton("Yes");
  962.         btnYes.addActionListener(new ActionListener() {
  963.             public void actionPerformed(ActionEvent arg0) {
  964.                 JPAS.removeEmployee(index);
  965.                 list.setModel(new AbstractListModel<String>() {
  966.                     String[] values = JPAS.getEmployeeList();
  967.                     public int getSize() {
  968.                         return values.length;
  969.                     }
  970.                     public String getElementAt(int index) {
  971.                         return values[index];
  972.                     }
  973.                 });
  974.  
  975.                 resetTotals();
  976.                 frame.setVisible(true);
  977.                 editFrame.dispose();
  978.                 deleteConfirmationFrame.dispose();
  979.             }
  980.         });
  981.         btnYes.setBounds(96, 69, 67, 23);
  982.         deleteConfirmationFrame.getContentPane().add(btnYes);
  983.  
  984.         JButton btnNo = new JButton("No");
  985.         btnNo.addActionListener(new ActionListener() {
  986.             public void actionPerformed(ActionEvent n) {
  987.                 editFrame.setVisible(true);
  988.                 deleteConfirmationFrame.dispose();
  989.             }
  990.         });
  991.         btnNo.setBounds(210, 69, 67, 23);
  992.         deleteConfirmationFrame.getContentPane().add(btnNo);
  993.     }
  994.  
  995.     public void saveFrame() throws IOException
  996.     {
  997.         saveFrame = new JFrame();
  998.         saveFrame.setBounds(125, 200, 400, 150);
  999.         saveFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1000.         saveFrame.getContentPane().setLayout(null);
  1001.         saveFrame.setTitle("Database Saved");
  1002.  
  1003.         JPAS.saveDatabase();
  1004.  
  1005.         JButton btnOkay = new JButton("Okay");
  1006.         btnOkay.addActionListener(new ActionListener() {
  1007.             public void actionPerformed(ActionEvent e) {
  1008.                 saveFrame.dispose();
  1009.                 frame.setVisible(true);
  1010.             }
  1011.         });
  1012.         btnOkay.setBounds(156, 77, 66, 23);
  1013.         saveFrame.getContentPane().add(btnOkay);
  1014.  
  1015.         JLabel lblNewLabel_1 = new JLabel("Database has been saved for future use.");
  1016.         lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
  1017.         lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
  1018.         lblNewLabel_1.setBounds(10, 26, 364, 34);
  1019.         saveFrame.getContentPane().add(lblNewLabel_1);
  1020.         saveFrame.setVisible(true);
  1021.     }
  1022. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement