Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProgramAssignment5 extends JFrame{
- // Declare JFrame components to be used on GUI
- JButton btnReadFile, btnShowName, btnSort;
- // Declare output area
- static JTextArea jtaDisplay;
- // Declare an array of create Course(class) objects to manage up to 100 Courses
- private static payroll [] payroll = new payroll [100];
- // Declare a class variables(static) to be used count for array
- private static int count = 0;
- /***************************************************************************************/
- public ProgramAssignment5()
- {
- // Call constructor of superclass JFrame to title the frame
- super("CSC 120: Final Program – Spring 2019 – Due last day of class(4/25) (before 11:55 pm)");
- // Construct a label for instructions to the user
- JLabel lblInstructions = new JLabel("Click on READ FILE to get course information,\n then click on SHOW MY COURSES");
- // Construct button objects
- btnReadFile = new JButton("READ FILE");
- btnShowPayroll = new JButton ("SHOW PAYROLL");
- btnSort = new JButton ("SORT BY COURSE PREFIX");
- // Construct a scrollable JTextArea
- JTextArea jtaDisplay = new JTextArea(10,30);
- JScrollPane scrollPane = new JScrollPane(jtaDisplay);
- // Make the text area uneditable
- 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 on SHOW MY COURSES");
- }// End
- }
- );
- btnShowPayroll.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // display formatted contacts in text area on GUI
- jtaDisplay.setText("Payroll:\n\n");
- lblInstructions.setText("Use Scroll Bars for more courses");
- jtaDisplay.setText("Payroll");
- for (int i = 0; i < count; i++)
- {
- jtaDisplay.append("\n" + Payroll [i]);
- }
- }
- }
- );
- btnSort.addActionListener(
- new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // sort by prefix
- sortBy(); //call this method
- // Message shown after sort is complete
- jtaDisplay.setText("Courses Sorted by Course Prefix");
- for (int i = 0; i < count; i++)
- {
- jtaDisplay.append("\n\n" + Payroll[i]);
- }
- }
- }
- );
- // Add instructions label to the instructions panel
- pnlInstructions.add(lblInstructions);
- // Add button components to the button panel
- pnlButtons.add(btnReadFile);
- pnlButtons.add(btnShowPayroll);
- pnlButtons.add(btnSort);
- // 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(550,300);
- // Center JFrame
- setLocationRelativeTo(null);
- // Make the JFrame Visible
- setVisible(true);
- //
- }// End JFrame constructor
- /***************************************************************************************/
- public static void main(String[] args) {
- }
- public static void readFile()
- {
- // Declare a Scanner object for input
- Scanner scannerLine = new Scanner("");
- //try-catch blocks
- try
- {
- // Use a text file named "courses.text" to create a Scanner object "input"
- Scanner input = new Scanner(new File("payroll.txt"));
- while (input.hasNext())
- {
- // Store the line as a String
- String stringLine = input.nextLine();
- // Use the String to create a new Scanner for just that one line
- scannerLine = new Scanner (stringLine);
- // Use a delimeter (",") which will separate each field on the line
- scannerLine.useDelimiter(",");
- /*Use following text file format:
- * Arts & Sciences,Psychology,PSY,120,3,Introduction to Psychology,A,Fall,2017,An introduction to the field of Psychology
- * read each token and use to instantiate an instance of the Course class (implemented in a separate file)
- * and assign each object of Course into an element of a Course array*/
- payroll [count] = new Payroll
- (
- scannerLine.nextInt(), // gets course ID from line
- scannerLine.next(), //gets department from line
- scannerLine.next(), //gets subject from line
- scannerLine.next(), //gets course prefix from line
- scannerLine.nextInt(), //gets course number from line
- scannerLine.nextInt(), //gets hours from line
- scannerLine.next(), //gets course title from line
- scannerLine.next(), //gets grade from line
- scannerLine.next(), //gets term from line
- scannerLine.nextInt(), //gets year from line
- scannerLine.next(), //gets course summary from line
- scannerLine.next()); // gets instructor name
- count ++;
- }// End while
- }// End try block
- catch (IOException ioe)
- {
- JOptionPane.showMessageDialog(null,"File not found");
- // End program
- System.exit(0);
- }// End catch block
- finally
- {
- scannerLine.close();
- { // End finally block
- }
- }
- }
- }// End readFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement