Source of StudentMarks.java


  1: //StudentMarks.java
  2: //This is the complete version of the "Student Marks" program, which
  3: //allows the user to load in a file of student names and marks, with
  4: //the names in the first 18 columns (Last, First) and the marks in
  5: //columns 19 and 20 (so no one can make 100 ... :)). If the file
  6: //does not exist a suitable message is displayed. Once the data is
  7: //loaded in and displayed (by default in the order it appeared in
  8: //the file), the user has the option of sorting it by name or by
  9: //descending mark.

 11: //Here is a list of all classes imported from the Java library.
 12: //The wildcard * can be used, but when the list is not too long,
 13: //or when we are in a pedagogical mode, it is generally better
 14: //to show explicitlly the classes being used.
 15: import java.awt.BorderLayout;
 16: import java.awt.Font;
 17: import java.awt.event.ActionEvent;
 18: import java.awt.event.ActionListener;
 19: import java.io.File;
 20: import java.io.FileNotFoundException;
 21: import java.util.ArrayList;
 22: import java.util.Collections;
 23: import java.util.Comparator;
 24: import java.util.Iterator;
 25: import java.util.NoSuchElementException;
 26: import java.util.Scanner;
 27: import javax.swing.JButton;
 28: import javax.swing.JFrame;
 29: import javax.swing.JOptionPane;
 30: import javax.swing.JPanel;
 31: import javax.swing.JScrollPane;
 32: import javax.swing.JTextArea;

 34: //The main class of the application:
 35: public class StudentMarks extends JFrame
 36: {
 37:     //Private data members of the class:
 38:     private ArrayList<String> studentList;
 39:     private JTextArea display;
 40:     private JPanel buttonPanel;
 41:     private JButton fileButton;
 42:     private JButton byNameButton;
 43:     private JButton byMarkButton;
 44:     private JButton quitButton;
 45:     private Font font;

 47:     //A single, one-parameter constructor
 48:     public StudentMarks(String title)
 49:     {
 50:         //The "title" appears in the application window title bar.
 51:         super(title);

 53:         //For storing the data for later sorting
 54:         studentList = new ArrayList<String>();

 56:         //The display area and the font to be used:
 57:         display = new JTextArea(15, 30);
 58:         font = new Font("Monospaced", Font.PLAIN, 16);
 59:         display.setFont(font);
 60:  
 61:         //Set up a "button panel" and four buttons:
 62:         buttonPanel = new JPanel();
 63:         fileButton = new JButton("Load File");
 64:         byNameButton = new JButton("Sort by Name");
 65:         byMarkButton = new JButton("Sort by Mark");
 66:         quitButton = new JButton("Quit");

 68:         //Create a "button handler" and connect it to each button:
 69:         ButtonHandler buttonHandler = new ButtonHandler();
 70:         quitButton.addActionListener(buttonHandler);
 71:         fileButton.addActionListener(buttonHandler);
 72:         byNameButton.addActionListener(buttonHandler);
 73:         byMarkButton.addActionListener(buttonHandler);

 75:         //Add the buttons to the button panel:
 76:         buttonPanel.add(fileButton);
 77:         buttonPanel.add(byNameButton);
 78:         buttonPanel.add(byMarkButton);
 79:         buttonPanel.add(quitButton);

 81:         //Add the display component and the button panel to the
 82:         //"top-level" application "window" (the JFrame).
 83:         add(new JScrollPane(display), BorderLayout.CENTER);
 84:         add(buttonPanel, BorderLayout.SOUTH);
 85:     }

 87:     //Loads the name-and-mark data for the students in from a file
 88:     public void getDataFromFile(String fileName)
 89:     {
 90:         //Each time new data is loaded, old data must be cleared:
 91:         display.setText("");
 92:         studentList.clear();
 93:         try
 94:         {
 95:             Scanner in = new Scanner(new File(fileName));
 96:             String line;
 97:             while (in.hasNext())
 98:             {
 99:                 line = in.nextLine();
100:                 display.append(line + "\n");
101:                 studentList.add(line);
102:             }
103:             in.close();
104:             display.setCaretPosition(0);
105:         }
106:         catch (FileNotFoundException e)
107:         {
108:             JOptionPane.showMessageDialog(null,
109:                 "File not found.\nTry again.");
110:         }
111:     }

113:     //The "button handler" class implements the one required
114:     //actionPerformed method, which in turn checks to see which
115:     //button has been clicked, and responds appropriately.
116:     class ButtonHandler implements ActionListener
117:     {
118:         public void actionPerformed(ActionEvent e)
119:         {
120:             if (e.getSource() == quitButton)
121:                 System.exit(0);

123:             if (e.getSource() == fileButton)
124:             {
125:                 String fileName = JOptionPane.showInputDialog(
126:                     "What is the name of the data file?");
127:                 getDataFromFile(fileName);
128:             }
129:             
130:             if (e.getSource() == byNameButton)
131:             {
132:                 display.setText("");
133:                 ArrayList<String> tempStudentList =
134:                     new ArrayList<String>(studentList);
135:                 Collections.sort(tempStudentList);
136:                 Iterator<String> i = tempStudentList.iterator();
137:                 while (i.hasNext()) display.append(i.next() + "\n");
138:                 display.setCaretPosition(0);
139:             }

141:             if (e.getSource() == byMarkButton)
142:             {
143:                 display.setText("");
144:                 ArrayList<String> tempStudentList =
145:                     new ArrayList<String>(studentList);
146:                 Collections.sort(tempStudentList, new MarkComparator());
147:                 Iterator<String> i = tempStudentList.iterator();
148:                 while (i.hasNext()) display.append(i.next() + "\n");
149:                 display.setCaretPosition(0);
150:             }
151:         }
152:     }

154:     //And object of this class is used by the ArrayList.sort() algorithm
155:     //to determine the order of String elements in an ArrayList of String
156:     //objects, when those objects are being sorted.
157:     class MarkComparator implements Comparator<String>
158:     {
159:         public int compare(String s1, String s2)
160:         {
161:             return -1 * s1.substring(18).compareTo(s2.substring(18));
162:         }
163:     }


166:     public static void main(String[] args)
167:     {
168:         StudentMarks app = new StudentMarks("Record of Student Marks");
169:         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
170:         app.pack();
171:         app.setVisible(true);
172:     }
173: }