// This simple application acts as a driver for testing the Student class

import javax.swing.*;
import java.awt.event.*;

public class StudentInterface extends JFrame implements ActionListener {

  private JButton addButton;
  private JTextField courseField; // Type in course code here
  private JTextArea responseArea; // Display results here

  private Student s;  // Student object to test

  public StudentInterface() {
    addButton = new JButton("ADD");
    courseField = new JTextField(6);
    responseArea = new JTextArea(10, 20);
    JPanel p = new JPanel();
    getContentPane().add(p);
    p.add(addButton);
    p.add(courseField);
    p.add(responseArea);
    addButton.addActionListener(this);

    // Construct a Student object to test
    s = new Student("Fred");

    // Display its initial state
    responseArea.setText(s.inspect());

    // Construct a new CourseInventory stub and pass it to the Student
    s.setInventory(new CourseInventory());
    }

  public void actionPerformed(ActionEvent e) {

    // Call method for testing and display results of call
    try {
      s.addCourse(Integer.parseInt(courseField.getText()));
      responseArea.setText("Course added");
      }
    catch (AlreadyInCourseException ex) {
      responseArea.setText("Already in course");
      }
    catch (CourseClosedException ex) {
      responseArea.setText("Course closed");
      }
    catch (NoSuchCourseException ex) {
      responseArea.setText("No such course");
      }

    // Display current course state
    responseArea.append("\n"+s.inspect());
    }

  public static void main(String[] args) {
    StudentInterface si = new StudentInterface();
    si.setSize(250, 250);
    si.show();
    }

  }

