1. /*Filename: P_Supplemental_11.java
  2. * Created: November 7, 2012
  3. * Author: Dustin Hunt
  4. * Purpose: Create a form using a border layout with a FileChooser and two buttons*/
  5. package p_supplemental_11;
  6.  
  7. import java.awt.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.*;
  11. import javax.swing.*;
  12. import java.util.*;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. public class P_Supplemental_11 extends JFrame {
  17.  
  18. JPanel jpnl1 = new JPanel();
  19. JPanel southPanel = new JPanel();
  20. JButton jbtReadFile1 = new JButton("Get Student Names");
  21. JButton jbtReadFile2 = new JButton("Get Student Grades");
  22. JTextField jtxtFilePath = new JTextField();
  23. JLabel jlblDesc = new JLabel("Click a button to open each file!");
  24. JTextArea jtxtAfileContents = new JTextArea();
  25.  
  26. P_Supplemental_11() {
  27. this.setLayout(new BorderLayout(5, 10));
  28. jpnl1.setLayout(new GridLayout(2, 2));
  29. jpnl1.add(jlblDesc);
  30. //jpnl1.add(jtxtFilePath);
  31. southPanel.add(jbtReadFile1);
  32. southPanel.add(jbtReadFile2);
  33. add(southPanel, BorderLayout.SOUTH);
  34. add(jpnl1, BorderLayout.NORTH);
  35. add(jtxtAfileContents, BorderLayout.CENTER);
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. jbtReadFile1.addActionListener(new ActionListener() {
  44. public void actionPerformed(ActionEvent evt) {
  45.  
  46. jbtReadFileActionPerformed(evt);
  47. }
  48. });
  49.  
  50.  
  51. jbtReadFile2.addActionListener(new ActionListener() {
  52. public void actionPerformed(ActionEvent evt) {
  53.  
  54. jbtReadFileActionPerformed2(evt);
  55. }
  56. });
  57.  
  58.  
  59. } // end constructor
  60.  
  61. class Student {
  62.  
  63. private int stuId;
  64. private String stuName;
  65. private ArrayList<Double> grades;
  66.  
  67. Student(int idIn, String nameIn) {
  68.  
  69. this.stuId = idIn;
  70. this.stuName = nameIn;
  71. } // end student class
  72.  
  73. public int getStuId() {
  74. return stuId;
  75. }
  76.  
  77. public void setStuId(int stuId) {
  78. this.stuId = stuId;
  79. }
  80.  
  81. String getStuName() {
  82. return stuName;
  83. }
  84.  
  85. public void setStuName(String stuName) {
  86. this.stuName = stuName;
  87. }
  88.  
  89. public ArrayList<Double> getGrades() {
  90. return grades;
  91. }
  92.  
  93. public void setGrades(ArrayList grades) {
  94. this.grades = grades;
  95. }
  96. } // end class Student
  97. ArrayList<Student> students = new ArrayList();
  98.  
  99. private void jbtReadFileActionPerformed(ActionEvent evt) {
  100. try {
  101. JFileChooser fileChooser = new JFileChooser();
  102. if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  103. java.io.File file = fileChooser.getSelectedFile();
  104.  
  105. Scanner input = new Scanner(file);
  106.  
  107. StringBuilder output = new StringBuilder();
  108.  
  109. while(input.hasNext()) {
  110. students.add(new Student(input.nextInt(),input.nextLine()));
  111.  
  112. } // end while
  113.  
  114.  
  115.  
  116. jtxtAfileContents.setText(output.toString());
  117. input.close();
  118.  
  119. } // end if
  120. }// end action method for jbtReadFile button
  121. catch (FileNotFoundException ex) {
  122. Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
  123. }
  124. }
  125.  
  126. private void jbtReadFileActionPerformed2(ActionEvent evt) {
  127. try {
  128. JFileChooser fileChooser = new JFileChooser();
  129. if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  130. java.io.File file = fileChooser.getSelectedFile();
  131.  
  132. Scanner input = new Scanner(file);
  133.  
  134. while (input.hasNext()) {
  135. students.add(new Student(input.nextInt(), input.nextLine()));
  136.  
  137. } // end while
  138.  
  139. for (int o = 0; o < students.size(); o++) {
  140. students.get(o).setGrades(collectGrades(students.get(o).getStuId()));
  141.  
  142. StringBuilder output = new StringBuilder();
  143. output.append(students.get(o).getStuId());
  144. output.append(students.get(o).getStuName());
  145. output.append(averageGrade(students.get(o).getGrades()));
  146.  
  147. jtxtAfileContents.setText(output.toString());
  148.  
  149. }
  150. } // end for
  151. } // end action method for jbtReadFile button
  152. catch (FileNotFoundException ex) {
  153. Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
  154. }
  155. }
  156.  
  157. public static ArrayList<Double> collectGrades(int inId) {
  158.  
  159. ArrayList<Double> outGrade = new ArrayList();
  160. try {
  161. File inFile = new File("c:/temp/studentScores.txt");
  162. Scanner input = new Scanner(inFile);
  163. while (input.hasNext()) {
  164. int tmpInt = input.nextInt();
  165. double tmpDbl = input.nextDouble();
  166. if (tmpInt == inId) {
  167. outGrade.add(tmpDbl);
  168. } // end if
  169. } // end while
  170.  
  171. } catch (FileNotFoundException ex) {
  172. Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
  173. } // end catch
  174.  
  175. return outGrade;
  176. } // end method
  177.  
  178. public static double averageGrade(ArrayList<Double> gradeIn) {
  179. double outAvg = 0.0;
  180.  
  181. for (int y = 0; y < gradeIn.size(); y++) {
  182. outAvg += gradeIn.get(y);
  183. } // end for
  184.  
  185. return outAvg / gradeIn.size();
  186.  
  187. }
  188.  
  189. public static void main(String[] args) {
  190. P_Supplemental_11 frame = new P_Supplemental_11();
  191. frame.setTitle("P_Supplemenetal_10");
  192. frame.setSize(410, 520);
  193. frame.setLocationRelativeTo(null);
  194. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  195. frame.setVisible(true);
  196.  
  197. } // end main
  198. } // end public class P_Supp_11