Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import Program5.Employee;
- import Program5.SalariedEmployee;
- import Program5.CommissionBasedEmployee;
- import Program5.HourlyEmployee;
- import Program5.Name;
- public class ProgramAssignment5 extends JFrame {
- // creates buttons for GUI
- JButton btnReadFile, btnShowEmployees, btnSort, btnSave, btnShow;
- // Display area
- JTextArea jtaDisplay;
- static int counter = 0;
- private static Employee[] employeeList = new Employee[100];
- public ProgramAssignment5() {
- // Call constructor of superclass
- super("Weekly Payroll");
- // Construct a label for instructions to the user
- JLabel lblInstructions = new JLabel(
- "Click on Read File to get Payroll information,\n before clicking on View Payroll");
- // Construct button objects
- btnReadFile = new JButton("Read File");
- btnShowEmployees = new JButton("View Payroll");
- btnSort = new JButton("Sort By Last Name");
- btnSave = new JButton("Save List");
- btnShow = new JButton("Show Saved List");
- // Construct a scrollbar JTextArea
- JTextArea jtaDisplay = new JTextArea(15, 50);
- JScrollPane scrollPane = new JScrollPane(jtaDisplay);
- jtaDisplay.setEditable(false);
- // Declare and construct panels for components
- JPanel pnlInstructions = new JPanel();
- JPanel pnlButtons = new JPanel();
- JPanel pnlDisplay = new JPanel();
- // Add event handlers to the buttons
- btnReadFile.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // read file
- jtaDisplay.setText("Reading File");
- readFile();
- lblInstructions.setText("Click to view payroll");
- }// End
- });
- btnShowEmployees.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // display formatted contacts in text area on GUI
- jtaDisplay.setText(displayList());
- lblInstructions.setText("Scroll to view all employees");
- }
- });
- btnSort.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // sort by lastName using a bubble sort or another one if you wish
- sortByLastName();
- // Message shown after sort is complete
- jtaDisplay.setText("Payroll Sorted by Last Names");
- }
- });
- btnSave.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // sort by lastName using a bubble sort or another one if you wish
- writeFile();
- // Message shown after sort is complete
- jtaDisplay.setText("List Saved to Binary File");
- }
- });
- btnShow.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // sort by lastName using a bubble sort or another one if you wish
- // Message shown after sort is complete
- jtaDisplay.setText(fileDisplay());
- }
- });
- // Add instructions label to the instructions panel
- pnlInstructions.add(lblInstructions);
- // Add button components to the button panel
- pnlButtons.add(btnReadFile);
- pnlButtons.add(btnShowEmployees);
- pnlButtons.add(btnSort);
- pnlButtons.add(btnSave);
- pnlButtons.add(btnShow);
- // Add the scrollable text area to the display panel
- pnlDisplay.add(scrollPane);
- // Add the panels to the JFrame
- add(pnlInstructions, BorderLayout.NORTH);
- add(pnlButtons, BorderLayout.SOUTH);
- add(pnlDisplay, BorderLayout.CENTER);
- // Set the size of the JFrame
- setSize(750, 400);
- // Center JFrame
- setLocationRelativeTo(null);
- // Make the JFrame Visible
- setVisible(true);
- //
- }// End JFrame constructor
- public void readFile()
- {
- Scanner scanFile = null;
- try
- {
- scanFile = new Scanner(new File("payroll.txt"));
- while (scanFile.hasNext()) {
- String line = scanFile.nextLine();
- Scanner lineDivider = new Scanner(line);
- lineDivider.useDelimiter(" ");
- String code = lineDivider.next();
- if (code.equalsIgnoreCase("c"))
- {
- employeeList[counter] = new CommissionBasedEmployee(lineDivider.next(), // firstName
- lineDivider.next(), // middleName
- lineDivider.next(), // lastName
- lineDivider.next(), // namePrefix
- lineDivider.nextDouble(), // commission rate
- lineDivider.nextDouble()); // sales earned
- counter++;
- } // end commissioned branch
- else if (code.equalsIgnoreCase("s"))
- {
- employeeList[counter] = new SalariedEmployee(lineDivider.next(), // firstName
- lineDivider.next(), // middleName
- lineDivider.next(), // lastName
- lineDivider.next(), // namePrefix
- lineDivider.nextDouble()); // yearly salary
- counter++;
- } // end salaried branch
- else if (code.equalsIgnoreCase("h")) {
- employeeList[counter] = new HourlyEmployee(lineDivider.next(), // firstName
- lineDivider.next(), // middleName
- lineDivider.next(), // lastName
- lineDivider.next(), // namePrefix
- lineDivider.nextDouble(), // hourlyWage
- lineDivider.nextDouble()); // hoursWorked
- counter++;
- } // end hourly branch
- else {
- JOptionPane.showMessageDialog(null, "Error with file input found");
- break;
- }
- }
- }
- catch (IOException ioe)
- {
- JOptionPane.showMessageDialog(null, "Error cannot find file");
- }
- finally
- {
- scanFile.close();
- }
- } // end readFile ()
- public static void sortByLastName()
- {
- // One by one move boundary of unsorted subarray
- for (int i = 0; i < counter - 1; i++) {
- // Find the minimum element in unsorted array
- int min_idx = i;
- for (int j = i + 1; j < counter; j++)
- {
- if (employeeList[j].getLast().compareTo(employeeList[min_idx].getLast()) < 0)
- {
- min_idx = j;
- }
- }
- Employee temp = employeeList[min_idx];
- employeeList[min_idx] = employeeList[i];
- employeeList[i] = temp;
- }
- }// End sortByLastName()
- public void writeFile()
- {
- String fileName = "payroll.bin";
- try
- {
- ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
- os.writeObject(employeeList); // writes object
- os.close();
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- } // end writeFile
- public String fileDisplay() {
- String outputString = "";
- try {
- ObjectInputStream is = new ObjectInputStream(new FileInputStream("payroll.bin"));
- Employee[] test = (Employee[]) is.readObject();
- for (int i = 0; i < counter; i++) {
- outputString += test[i] + "\n";
- } // end for loop
- is.close();
- } // end try
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- return outputString;
- } // end displayFile()
- public String displayList()
- {
- String outputString = "";
- for (int i = 0; i < counter; i++)
- {
- outputString += employeeList[i] + " \n";
- }
- return outputString;
- }
- public static void main(String[] args)
- {
- new ProgramAssignment5();
- }// End main.
- }// End class.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement