Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. public class ProgramAssignment5 extends JFrame{
  2.  
  3.  
  4. // Declare JFrame components to be used on GUI
  5. JButton btnReadFile, btnShowName, btnSort;
  6.  
  7. // Declare output area
  8. static JTextArea jtaDisplay;
  9.  
  10. // Declare an array of create Course(class) objects to manage up to 100 Courses
  11. private static payroll [] payroll = new payroll [100];
  12.  
  13. // Declare a class variables(static) to be used count for array
  14. private static int count = 0;
  15.  
  16. /***************************************************************************************/
  17. public ProgramAssignment5()
  18. {
  19. // Call constructor of superclass JFrame to title the frame
  20. super("CSC 120: Final Program – Spring 2019 – Due last day of class(4/25) (before 11:55 pm)");
  21.  
  22. // Construct a label for instructions to the user
  23. JLabel lblInstructions = new JLabel("Click on READ FILE to get course information,\n then click on SHOW MY COURSES");
  24.  
  25. // Construct button objects
  26. btnReadFile = new JButton("READ FILE");
  27. btnShowPayroll = new JButton ("SHOW PAYROLL");
  28. btnSort = new JButton ("SORT BY COURSE PREFIX");
  29.  
  30. // Construct a scrollable JTextArea
  31. JTextArea jtaDisplay = new JTextArea(10,30);
  32. JScrollPane scrollPane = new JScrollPane(jtaDisplay);
  33.  
  34. // Make the text area uneditable
  35. jtaDisplay.setEditable(false);
  36.  
  37. // Declare and construct panels for components
  38. JPanel pnlInstructions = new JPanel();
  39. JPanel pnlButtons = new JPanel();
  40. JPanel pnlDisplay = new JPanel();
  41.  
  42. // Add event handlers to the buttons
  43. btnReadFile.addActionListener(
  44. new ActionListener() {
  45. public void actionPerformed(ActionEvent e) {
  46. // read file
  47. jtaDisplay.setText("Reading File...");
  48. readFile();
  49. lblInstructions.setText("Click on SHOW MY COURSES");
  50. }// End
  51. }
  52. );
  53.  
  54. btnShowPayroll.addActionListener(
  55. new ActionListener() {
  56. public void actionPerformed(ActionEvent e) {
  57. // display formatted contacts in text area on GUI
  58. jtaDisplay.setText("Payroll:\n\n");
  59. lblInstructions.setText("Use Scroll Bars for more courses");
  60. jtaDisplay.setText("Payroll");
  61. for (int i = 0; i < count; i++)
  62. {
  63. jtaDisplay.append("\n" + Payroll [i]);
  64. }
  65.  
  66. }
  67. }
  68. );
  69.  
  70.  
  71. btnSort.addActionListener(
  72. new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. // sort by prefix
  75. sortBy(); //call this method
  76. // Message shown after sort is complete
  77. jtaDisplay.setText("Courses Sorted by Course Prefix");
  78. for (int i = 0; i < count; i++)
  79. {
  80. jtaDisplay.append("\n\n" + Payroll[i]);
  81. }
  82. }
  83. }
  84. );
  85.  
  86. // Add instructions label to the instructions panel
  87. pnlInstructions.add(lblInstructions);
  88.  
  89. // Add button components to the button panel
  90. pnlButtons.add(btnReadFile);
  91. pnlButtons.add(btnShowPayroll);
  92. pnlButtons.add(btnSort);
  93.  
  94. // Add the scrollable text area to the display panel
  95. pnlDisplay.add(scrollPane);
  96.  
  97. // Add the panels to the JFrame
  98. add(pnlInstructions, BorderLayout.NORTH);
  99. add(pnlButtons, BorderLayout.SOUTH);
  100. add(pnlDisplay, BorderLayout.CENTER);
  101.  
  102. // Set the size of the JFrame
  103. setSize(550,300);
  104.  
  105. // Center JFrame
  106. setLocationRelativeTo(null);
  107.  
  108. // Make the JFrame Visible
  109. setVisible(true);
  110.  
  111. //
  112. }// End JFrame constructor
  113.  
  114. /***************************************************************************************/
  115.  
  116. public static void main(String[] args) {
  117. }
  118.  
  119.  
  120. public static void readFile()
  121. {
  122. // Declare a Scanner object for input
  123. Scanner scannerLine = new Scanner("");
  124.  
  125. //try-catch blocks
  126. try
  127. {
  128. // Use a text file named "courses.text" to create a Scanner object "input"
  129. Scanner input = new Scanner(new File("payroll.txt"));
  130.  
  131. while (input.hasNext())
  132. {
  133. // Store the line as a String
  134. String stringLine = input.nextLine();
  135. // Use the String to create a new Scanner for just that one line
  136. scannerLine = new Scanner (stringLine);
  137. // Use a delimeter (",") which will separate each field on the line
  138. scannerLine.useDelimiter(",");
  139.  
  140. /*Use following text file format:
  141. * Arts & Sciences,Psychology,PSY,120,3,Introduction to Psychology,A,Fall,2017,An introduction to the field of Psychology
  142. * read each token and use to instantiate an instance of the Course class (implemented in a separate file)
  143. * and assign each object of Course into an element of a Course array*/
  144. payroll [count] = new Payroll
  145. (
  146. scannerLine.nextInt(), // gets course ID from line
  147. scannerLine.next(), //gets department from line
  148. scannerLine.next(), //gets subject from line
  149. scannerLine.next(), //gets course prefix from line
  150. scannerLine.nextInt(), //gets course number from line
  151. scannerLine.nextInt(), //gets hours from line
  152. scannerLine.next(), //gets course title from line
  153. scannerLine.next(), //gets grade from line
  154. scannerLine.next(), //gets term from line
  155. scannerLine.nextInt(), //gets year from line
  156. scannerLine.next(), //gets course summary from line
  157. scannerLine.next()); // gets instructor name
  158. count ++;
  159. }// End while
  160. }// End try block
  161. catch (IOException ioe)
  162. {
  163. JOptionPane.showMessageDialog(null,"File not found");
  164. // End program
  165. System.exit(0);
  166. }// End catch block
  167. finally
  168. {
  169. scannerLine.close();
  170. { // End finally block
  171. }
  172. }
  173. }
  174. }// End readFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement