import java.awt.*; import java.applet.*; import java.awt.event.*; /** * This class demonstrates an arrays of objects (Buttons and Strings) * It's a very simple cash register class. * * It also demonstrates use of a panel to create a grid of buttons */ public class CashRegister extends Applet implements ActionListener { TextArea display = new TextArea(8, 20); // register display area String current = new String(""); // current string in the display double sumSoFar = 0d; // sum of numbers entered so far String [] btnLabels = // Labels for the register buttons { "1", "2", "3", "Add", "4", "5", "6", "Clear", "7", "8", "9", "Sum", "0", "." }; // Notice that arrays can be initialised wuith values when they are declared // rather than be given a size; in this case the size is implied. /** * Method to set up the display */ public void init() { int nButtons = btnLabels.length; // number of buttons in the display (= label array size) add(display); // the text area Panel keypad = new Panel(); // put the buttons in a panel keypad.setLayout( new GridLayout(4, 3) ); // lay them out in a grid Button [] keys = new Button[nButtons]; // Array of buttons for (int i = 0; i < nButtons; i++ ) // for each label { keys[i] = new Button(btnLabels[i]); // create a button keypad.add( keys[i] ); // add it to the panel keys[i].addActionListener(this); // set listener keys[i].setActionCommand(btnLabels[i]); // and associated command } add(keypad); // add the pad } /** * actionPerformed, as ever, carries out the main processing * based on the Button's which are pressed. * * @param e carries information about the event that generated the call */ public void actionPerformed(ActionEvent e) { // a numeric key (0-9) adds the number to the current string // the . key adds a decimal point // the Add key sums in the last entry and starts a new line // the Sum key prints the sum so far and clears the sum to zero // the Clr key clears the current entry String command = e.getActionCommand(); // action string - set in init() char com = command.charAt(0); // get first character // see if the character is part of a number: if ( (com >= '0') && (com <= '9') || (com == '.') ) { current = current + com; // if it is append to the current String display.append("" + com); // and to the display } else { // check here for function buttons if ( com == 'A') // Add this entry into the sum { display.append(" +\n"); // start a new display line addThis(current); // helper method to add to sum current = ""; // re-set current String to empty } else if (com == 'C') { current = ""; // clear the current String display.append(" - err\n"); // indicate in the display } else if (com == 'S') { display.append("\n-------------\n"); // draw a line display.append("" + sumSoFar + "\n\n"); // show the current sum current = ""; // clear the current String sumSoFar = 0.0; // reset the sum } // more functions could be added! } } /** * This method converts the current string and adds to sum so far * it gives an error if the number is incorrectly formatted * * @param The String to be converted and added to the sum */ private void addThis(String s) { if (s == "") { display.append("empty string!\n"); } else { try { sumSoFar = sumSoFar + Double.parseDouble(s); } catch ( NumberFormatException n ) { display.append("Error, re-enter\n"); current = ""; } } } }