Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import java.io.ObjectOutputStream;
  12. import java.util.Scanner;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JScrollPane;
  20. import javax.swing.JTextArea;
  21. import Program5.Employee;
  22. import Program5.SalariedEmployee;
  23. import Program5.CommissionBasedEmployee;
  24. import Program5.HourlyEmployee;
  25. import Program5.Name;
  26.  
  27. public class ProgramAssignment5 extends JFrame {
  28.  
  29.  
  30. // creates buttons for GUI
  31. JButton btnReadFile, btnShowEmployees, btnSort, btnSave, btnShow;
  32.  
  33. // Display area
  34. JTextArea jtaDisplay;
  35.  
  36. static int counter = 0;
  37. private static Employee[] employeeList = new Employee[100];
  38.  
  39. public ProgramAssignment5() {
  40. // Call constructor of superclass
  41. super("Weekly Payroll");
  42.  
  43. // Construct a label for instructions to the user
  44. JLabel lblInstructions = new JLabel(
  45. "Click on Read File to get Payroll information,\n before clicking on View Payroll");
  46.  
  47. // Construct button objects
  48. btnReadFile = new JButton("Read File");
  49. btnShowEmployees = new JButton("View Payroll");
  50. btnSort = new JButton("Sort By Last Name");
  51. btnSave = new JButton("Save List");
  52. btnShow = new JButton("Show Saved List");
  53.  
  54. // Construct a scrollbar JTextArea
  55. JTextArea jtaDisplay = new JTextArea(15, 50);
  56. JScrollPane scrollPane = new JScrollPane(jtaDisplay);
  57.  
  58.  
  59. jtaDisplay.setEditable(false);
  60.  
  61. // Declare and construct panels for components
  62. JPanel pnlInstructions = new JPanel();
  63. JPanel pnlButtons = new JPanel();
  64. JPanel pnlDisplay = new JPanel();
  65.  
  66. // Add event handlers to the buttons
  67. btnReadFile.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. // read file
  70. jtaDisplay.setText("Reading File");
  71. readFile();
  72. lblInstructions.setText("Click to view payroll");
  73. }// End
  74. });
  75.  
  76. btnShowEmployees.addActionListener(new ActionListener() {
  77. public void actionPerformed(ActionEvent e) {
  78. // display formatted contacts in text area on GUI
  79. jtaDisplay.setText(displayList());
  80. lblInstructions.setText("Scroll to view all employees");
  81.  
  82. }
  83. });
  84.  
  85. btnSort.addActionListener(new ActionListener() {
  86. public void actionPerformed(ActionEvent e) {
  87. // sort by lastName using a bubble sort or another one if you wish
  88. sortByLastName();
  89. // Message shown after sort is complete
  90. jtaDisplay.setText("Payroll Sorted by Last Names");
  91. }
  92. });
  93.  
  94. btnSave.addActionListener(new ActionListener() {
  95. public void actionPerformed(ActionEvent e) {
  96. // sort by lastName using a bubble sort or another one if you wish
  97. writeFile();
  98. // Message shown after sort is complete
  99. jtaDisplay.setText("List Saved to Binary File");
  100. }
  101. });
  102.  
  103. btnShow.addActionListener(new ActionListener() {
  104. public void actionPerformed(ActionEvent e) {
  105. // sort by lastName using a bubble sort or another one if you wish
  106. // Message shown after sort is complete
  107. jtaDisplay.setText(fileDisplay());
  108. }
  109. });
  110.  
  111. // Add instructions label to the instructions panel
  112. pnlInstructions.add(lblInstructions);
  113.  
  114. // Add button components to the button panel
  115. pnlButtons.add(btnReadFile);
  116. pnlButtons.add(btnShowEmployees);
  117. pnlButtons.add(btnSort);
  118. pnlButtons.add(btnSave);
  119. pnlButtons.add(btnShow);
  120.  
  121. // Add the scrollable text area to the display panel
  122. pnlDisplay.add(scrollPane);
  123.  
  124. // Add the panels to the JFrame
  125. add(pnlInstructions, BorderLayout.NORTH);
  126. add(pnlButtons, BorderLayout.SOUTH);
  127. add(pnlDisplay, BorderLayout.CENTER);
  128.  
  129. // Set the size of the JFrame
  130. setSize(750, 400);
  131.  
  132. // Center JFrame
  133. setLocationRelativeTo(null);
  134.  
  135. // Make the JFrame Visible
  136. setVisible(true);
  137.  
  138. //
  139. }// End JFrame constructor
  140.  
  141.  
  142. public void readFile()
  143. {
  144. Scanner scanFile = null;
  145.  
  146. try
  147.  
  148. {
  149. scanFile = new Scanner(new File("payroll.txt"));
  150. while (scanFile.hasNext()) {
  151. String line = scanFile.nextLine();
  152. Scanner lineDivider = new Scanner(line);
  153. lineDivider.useDelimiter(" ");
  154. String code = lineDivider.next();
  155.  
  156. if (code.equalsIgnoreCase("c"))
  157.  
  158. {
  159. employeeList[counter] = new CommissionBasedEmployee(lineDivider.next(), // firstName
  160. lineDivider.next(), // middleName
  161. lineDivider.next(), // lastName
  162. lineDivider.next(), // namePrefix
  163. lineDivider.nextDouble(), // commission rate
  164. lineDivider.nextDouble()); // sales earned
  165. counter++;
  166.  
  167. } // end commissioned branch
  168.  
  169. else if (code.equalsIgnoreCase("s"))
  170.  
  171. {
  172.  
  173. employeeList[counter] = new SalariedEmployee(lineDivider.next(), // firstName
  174. lineDivider.next(), // middleName
  175. lineDivider.next(), // lastName
  176. lineDivider.next(), // namePrefix
  177. lineDivider.nextDouble()); // yearly salary
  178. counter++;
  179.  
  180. } // end salaried branch
  181.  
  182. else if (code.equalsIgnoreCase("h")) {
  183. employeeList[counter] = new HourlyEmployee(lineDivider.next(), // firstName
  184. lineDivider.next(), // middleName
  185. lineDivider.next(), // lastName
  186. lineDivider.next(), // namePrefix
  187. lineDivider.nextDouble(), // hourlyWage
  188. lineDivider.nextDouble()); // hoursWorked
  189. counter++;
  190. } // end hourly branch
  191.  
  192. else {
  193. JOptionPane.showMessageDialog(null, "Error with file input found");
  194. break;
  195. }
  196.  
  197. }
  198.  
  199. }
  200.  
  201. catch (IOException ioe)
  202.  
  203. {
  204.  
  205. JOptionPane.showMessageDialog(null, "Error cannot find file");
  206.  
  207. }
  208.  
  209. finally
  210.  
  211. {
  212.  
  213. scanFile.close();
  214.  
  215. }
  216. } // end readFile ()
  217.  
  218.  
  219. public static void sortByLastName()
  220.  
  221. {
  222.  
  223. // One by one move boundary of unsorted subarray
  224. for (int i = 0; i < counter - 1; i++) {
  225. // Find the minimum element in unsorted array
  226. int min_idx = i;
  227. for (int j = i + 1; j < counter; j++)
  228.  
  229. {
  230.  
  231. if (employeeList[j].getLast().compareTo(employeeList[min_idx].getLast()) < 0)
  232.  
  233. {
  234.  
  235. min_idx = j;
  236.  
  237. }
  238. }
  239. Employee temp = employeeList[min_idx];
  240. employeeList[min_idx] = employeeList[i];
  241. employeeList[i] = temp;
  242.  
  243. }
  244. }// End sortByLastName()
  245.  
  246.  
  247. public void writeFile()
  248.  
  249. {
  250.  
  251. String fileName = "payroll.bin";
  252.  
  253. try
  254. {
  255.  
  256. ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
  257. os.writeObject(employeeList); // writes object
  258. os.close();
  259.  
  260. }
  261.  
  262. catch (FileNotFoundException e)
  263. {
  264.  
  265. e.printStackTrace();
  266.  
  267. }
  268.  
  269. catch (IOException e)
  270. {
  271.  
  272. e.printStackTrace();
  273.  
  274. }
  275. } // end writeFile
  276.  
  277.  
  278. public String fileDisplay() {
  279.  
  280. String outputString = "";
  281.  
  282. try {
  283.  
  284. ObjectInputStream is = new ObjectInputStream(new FileInputStream("payroll.bin"));
  285.  
  286. Employee[] test = (Employee[]) is.readObject();
  287. for (int i = 0; i < counter; i++) {
  288. outputString += test[i] + "\n";
  289. } // end for loop
  290.  
  291. is.close();
  292. } // end try
  293.  
  294. catch (FileNotFoundException e)
  295.  
  296. {
  297.  
  298. e.printStackTrace();
  299.  
  300. }
  301.  
  302. catch (IOException e)
  303.  
  304. {
  305.  
  306. e.printStackTrace();
  307.  
  308. }
  309.  
  310. catch (ClassNotFoundException e)
  311.  
  312. {
  313.  
  314. e.printStackTrace();
  315.  
  316. }
  317.  
  318. return outputString;
  319. } // end displayFile()
  320.  
  321. public String displayList()
  322.  
  323. {
  324.  
  325. String outputString = "";
  326.  
  327. for (int i = 0; i < counter; i++)
  328.  
  329. {
  330.  
  331. outputString += employeeList[i] + " \n";
  332.  
  333. }
  334.  
  335. return outputString;
  336.  
  337. }
  338.  
  339.  
  340. public static void main(String[] args)
  341.  
  342. {
  343.  
  344. new ProgramAssignment5();
  345.  
  346. }// End main.
  347.  
  348. }// End class.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement