/** This program is intended to be used with the book "Java -- What Do You Want To Do?"
 * by Steven P. Warr and is distributed free of charge and without and expectations of remuneration.
 * Chapter CELL   NOTE most program names begin with the title of the chapter in which
 * they are intended to be used.   Copyright 2010 Steven P. Warr All rights reserved
 */

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.*;  //Added all the components with *
import net.rim.device.api.ui.container.FlowFieldManager;
class Cell03 extends UiApplication 
{
    public static void main(String[] args)  //Start point
    {
    
        Cell03 theApp = new Cell03();
        theApp.enterEventDispatcher();
    }

    public Cell03()
     {
        pushScreen(new Cell03Screen());
     }    
}

final class Cell03Screen extends MainScreen  //draws components on the screen
{
    FlowFieldManager loc;  //Enable more than one button on a line
    LabelField title, prompt,valueField, sumField, listField, dummyField;
    ButtonField[] button= new ButtonField[13];
    int value = 0, sum = 0, average = 0, count = 0;
    String textVal = "";
    String list = "";
    Cell03Screen()   // constructor
    {
        title = new LabelField("Cell03 Averager" , 
             LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        setTitle(title);
        dummyField = new LabelField( );  //Add space 
        add(dummyField);
        dummyField = new LabelField( );
        add(dummyField);
        dummyField = new LabelField( );
        add(dummyField);
        dummyField = new LabelField( );
        add(dummyField);
        dummyField = new LabelField( );
        add(dummyField);
        listField = new LabelField( );
        add(listField);
        prompt = new LabelField("Enter numbers to total and average." , 
             LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        add(prompt);     
        valueField = new LabelField();
        add(valueField); 
        sumField = new LabelField( );
        add(sumField);
        dummyField = new LabelField( );
        add(dummyField);
        FieldChangeListener listener = new FieldChangeListener() //inner class
            {
                public void fieldChanged(Field field, int context) // get value of button
                {
                    if (field.getIndex() >= 0 &&field.getIndex() < 9 )
                        {
                            textVal += (field.getIndex() + 1);
                            valueField.setText(textVal);
                        }
                    else
                    if (field.getIndex() == 9)
                        {
                           textVal += "0";
                           valueField.setText(textVal);
                        }   
                    else       
                    if (field.getIndex() == 10)   // end number entry
                        {
                           value = Integer.parseInt(textVal);
                           sum += value;
                           count ++;
                           average = sum / count;
                           value = 0;
                           list += (textVal + " ");
                           textVal = "";
                           sumField.setText(count + "  total = " + sum + "  Average = " + average);
                           listField.setText(list);
                       }   
                    else       
                    if (field.getIndex() == 11)  // new list
                        {
                           sum = 0;
                           count = 0;
                           average = 0;
                           value = 0;
                           textVal = "";
                           list = "";
                           sumField.setText("");
                           listField.setText("");
                           valueField.setText("");
                        }   
                    else       
                    if (field.getIndex() == 12)
                        {
                          //System.exit(0);
                          close();
                        }   
     
                }
            };
      loc = new FlowFieldManager(); 
      for(int x = 1; x <10;x++)  // set up number buttons
        {
            
            button[x] = new ButtonField("    " + x + "          ",ButtonField.CONSUME_CLICK);
            button[x].setChangeListener(listener);
            loc.add(button[x]);
        } 
     button[0] = new ButtonField("    0          ",ButtonField.CONSUME_CLICK );   //zero button
     button[0].setChangeListener(listener);
     loc.add(button[0]);
    
     button[10] = new ButtonField("    Enter                          ",ButtonField.CONSUME_CLICK );  // Enter
     button[10].setChangeListener(listener);
     loc.add(button[10]);
    
     button[11] = new ButtonField(" New                   ",ButtonField.CONSUME_CLICK );   //New
     button[11].setChangeListener(listener);
     loc.add(button[11]);
    
     button[12] = new ButtonField("    Exit                  ",ButtonField.CONSUME_CLICK );   //New
     button[12].setChangeListener(listener);
     loc.add(button[12]);
    
     add(loc);
    }

  public void close()
    {
        Dialog.alert("Goodbye!");
        System.exit(0);
        super.close();
    }   
}
