import java.awt.*; import java.awt.event.*; import java.io.*; /** * Example application that stores Biscuit's in an array * * @author Mr J * @version 1.0 */ public class BiscuitWindow extends Frame implements ActionListener { // Filename to load and save records from private static final String FILENAME = "c:\\biscuits.txt"; private static final String EOF = "XXXX"; // GUI objects private BiscuitDisplay display = new BiscuitDisplay(); private Button add = new Button("add"); private Button del = new Button("delete"); private Button find = new Button("find"); private Button next = new Button("next"); private Button prev = new Button("previous"); private Label messages = new Label("Any system messages will appear here...."); // We'll need an array to keep track of our collection private static final int MAX = 10; private Biscuit2[] biscuits = new Biscuit2[MAX]; // pointers into the biscuit collection int lastBiscuit = -1; int current = -1; /** * Constructor */ public BiscuitWindow() { // inner class to intercept window events addWindowListener ( new WindowAdapter() { // we are only interested in window closing public void windowClosing(WindowEvent e) { // save the array contents saveToFile(); // Exit the system, 0 is a normal exit code System.exit(0); } } ); // Helper method to set up the display makeDisplay(); // load the array contents readFromFile(); } /** * Helper method to set up the display */ private void makeDisplay() { // set up the Biscuit display setLayout(new FlowLayout()); add(display.getPanel()); // Make a Panel for the buttons Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(5,1)); buttonPanel.add(add); buttonPanel.add(del); buttonPanel.add(find); buttonPanel.add(next); buttonPanel.add(prev); add(buttonPanel); // set up the button listeners add.addActionListener(this); del.addActionListener(this); find.addActionListener(this); next.addActionListener(this); prev.addActionListener(this); add(messages); setSize(500,200); setTitle("Biscuit Collection Manager v 1.0"); setVisible(true); } /** * main method to call the Constructor */ public static void main(String[] args) { BiscuitWindow theApp = new BiscuitWindow(); } /** * Our old buddy, actionPerformed will detect the button presses * * @param the object that caused the event to occur */ public void actionPerformed(ActionEvent e) { if (e.getSource() == add) { addBiscuit(); } else if (e.getSource() == del) { //messages.setText("My students will try their best"); } else if (e.getSource() == find) { messages.setText("My brilliant programmers will whizz through this"); } else if (e.getSource() == next) { messages.setText("My serfs will try to do this"); } else if (e.getSource() == prev) { messages.setText("My best and brightest will have a go at this"); } } /** * add a Biscuit object to the array * */ public void addBiscuit() { // Check if the array is full if ( lastBiscuit < (MAX-1) ) { lastBiscuit = lastBiscuit + 1; Biscuit2 b = display.getBiscuit(); // check if a valid biscuit was got from display if (b != null) { biscuits[lastBiscuit] = b; messages.setText("Biscuit added " + lastBiscuit); current = lastBiscuit; display.showBiscuit(biscuits[current]); } else { messages.setText("Could not add the biscuit, check details"); } } else { messages.setText("No more space to add biscuits"); } } /** * Save the array contents to a text file */ private void saveToFile() { try { SimpleWriter writer = new SimpleWriter(FILENAME); for(int x = 0; x <= lastBiscuit; x++) { writer.writeLine(biscuits[x].toString()); } writer.writeLine(EOF); writer.close(); } catch(IOException io) { messages.setText("Some error writing to file"); System.exit(1); } } /** * load the array contents from text file */ private void readFromFile() { try { SimpleReader reader = new SimpleReader(FILENAME); // read first line of file, if file is empty should be EOF String next = reader.readLine(); while (!next.equals(EOF)) { lastBiscuit = lastBiscuit + 1; biscuits[lastBiscuit] = new Biscuit2(next); next = reader.readLine(); } // show the last one, check for none if (lastBiscuit >=0) { current = lastBiscuit; display.showBiscuit(biscuits[current]); messages.setText("File opened with " + lastBiscuit + 1 + " records"); } else { messages.setText("The file is empty"); } } catch(IOException io) { messages.setText("Error reading from file"); } } }