import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * This Applet demonstrates an inner class being used for data records * The inner class keeps data about video tapes * * @ Mr J * @ 20040308 */ public class VideoTapeInner extends Applet implements ActionListener { // display fields for record data private TextField vtTitle = new TextField(25); private TextField vtLength = new TextField(6); private Checkbox vtLent = new Checkbox(); private Choice vtType = new Choice(); // labels for field data private Label fdTitle = new Label("Video title:", Label.RIGHT); private Label fdLength = new Label("Length (min):", Label.RIGHT); private Label fdLent = new Label("Lent:", Label.RIGHT); private Label fdType = new Label("Type:", Label.RIGHT); // control buttons private Button add = new Button("add tape"); private Button next = new Button("next"); private Button prev = new Button("previous"); // system messages display private Label messages = new Label("System messages will appear here"); // Set up an array to hold the tape collection private static final int MAX_TAPES = 5; VideoTape[] collection = new VideoTape[MAX_TAPES]; private int last = -1; // pointer to last record in collection private int current = -1; // pointer to record in display // define the possible movie types (could be more flexible): static final int ROMANCE = 0; static final int COMEDY = 1; static final int ACTION = 2; static final int[] TYPE = { ROMANCE, COMEDY, ACTION}; static final String[] DESC = {"Romance", "Comedy", "Action"}; static final char[] CODE = {'R', 'C', 'A'}; // inner class definition class VideoTape { // data members private String title; private int length; private boolean lent; private char type; // Constructors public VideoTape() { super(); } public VideoTape( String title, int length, boolean lent, char type ) { setTitle( title ); setLength( length ); setLent( lent ); setType( type ); } // mutator methods public void setTitle( String title ) { this.title = title; } public void setLength( int length ) { this.length = length; } public void setLent( boolean lent ) { this.lent = lent; } public void setType( char type ) { this.type = type; } // accessor methods public String getTitle() { return this.title; } public int getLength() { return this.length; } public boolean isLent() { return this.lent; } public char getType() { return this.type; } }; // end of inner class definition /** * Add objects to the Applet */ public void init() { Panel recordPanel = new Panel(); recordPanel.setLayout( new GridLayout(4, 2, 10, 10) ); recordPanel.add(fdTitle); recordPanel.add(vtTitle); recordPanel.add(fdLength); recordPanel.add(vtLength); recordPanel.add(fdLent); recordPanel.add(vtLent); recordPanel.add(fdType); recordPanel.add(vtType); Panel buttonPanel = new Panel(); buttonPanel.setLayout( new GridLayout(3, 1, 10, 10) ); buttonPanel.add(add); buttonPanel.add(prev); buttonPanel.add(next); setLayout( new BorderLayout(25, 25) ); setSize(400, 180); add(recordPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.EAST); add(messages, BorderLayout.SOUTH); // Causes button presses to be detected add.addActionListener(this); prev.addActionListener(this); next.addActionListener(this); // set up the choice list setUpChoices(); } /** * When an event occurs on an object with an ActionListener attached, this * method is carried out. * * @param e carries details about the event that occurred */ public void actionPerformed(ActionEvent e) { if (e.getSource() == add) { // add a new video tape to the collection VideoTape theTape = getTapeDetails(); if (theTape != null) { last = last + 1; if (last == MAX_TAPES) { messages.setText("No more space to store tapes"); } else { collection[last] = theTape; current = last; messages.setText("tape added"); clearDisplay(); } } else { messages.setText("Invalid tape details"); } } else if (e.getSource() == next) { if (current < last) { current = current + 1; setDisplay(current); messages.setText("At tape " + (current + 1) + " of " + (last + 1)); } else { messages.setText("At last record"); } } else if (e.getSource() == prev) { if (current > 0) { current = current - 1; setDisplay(current); messages.setText("At tape " + (current + 1) + " of " + (last+1)); } else { messages.setText("At first record"); } } } /** * set up some types in the choice list */ private void setUpChoices() { for (int i = 0; i < TYPE.length; i++) { vtType.addItem(DESC[i]); } } /** * collect tape details and create new tape * * @return a VideoTape object - null if details are invalid */ private VideoTape getTapeDetails() { try { String title = vtTitle.getText(); int length = Integer.parseInt(vtLength.getText()); boolean lent = vtLent.getState(); char type = vtType.getSelectedItem().charAt(0); if ( (title.length() == 0) || (length == 0) ) { return null; } return new VideoTape(title, length, lent, type); } catch(Exception e) { return null; } } /** * display tape details * * @param the current VideoTape object */ private void setDisplay(int current) { vtTitle.setText(collection[current].getTitle()); vtLength.setText("" + collection[current].getLength()); vtLent.setState(collection[current].isLent()); vtType.select(getTypeDescription(collection[current].getType())); } /** * clear record display panel */ private void clearDisplay() { vtTitle.setText(""); vtLength.setText(""); vtLent.setState(false); vtType.select(0); } /** * Given a code, return the corresponding description * * @param the code to look up * @returns a String with the description */ private String getTypeDescription(char code) { int pos = 0; boolean found = false; while ( (pos < CODE.length) && !(found) ) { found = (CODE[pos] == code); pos = pos + 1; } if (!found) { return "none"; } else { return DESC[pos-1]; } } }