FileDocCategorySizeDatePackage
QuotesMIDlet.javaAPI DocExample5465Sat Jan 05 11:47:02 GMT 2002None

QuotesMIDlet

public class QuotesMIDlet extends MIDlet implements CommandListener

Fields Summary
Display
display
List
menu
List
choose
TextBox
input
Ticker
ticker
String
quoteServer
String
quoteFormat
static final Command
backCommand
static final Command
mainMenuCommand
static final Command
saveCommand
static final Command
exitCommand
String
currentMenu
String
name
String
date
String
price
StockDB
db
Constructors Summary
public QuotesMIDlet()


      // constructor
   
Methods Summary
public voidaddStock()

      input = new TextBox("Enter a Stock Name:", "", 5, TextField.ANY);
      input.setTicker(ticker);
      input.addCommand(saveCommand);
      input.addCommand(backCommand);
      input.setCommandListener(this);
      input.setString("");
      display.setCurrent(input);
      currentMenu = "Add";
   
public voidcommandAction(Command c, Displayable d)

      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Save")) {
         if(currentMenu.equals("Add")) {
            // add it to database
            try {
               String userInput = input.getString();
              String pr = getQuote(userInput);
              db.addNewStock(pr);
              ticker.setString(tickerString()); 
            } catch(IOException e) {
            } catch(NumberFormatException se) {
            }
            mainMenu();
          } 
      } else if (label.equals("Back")) {
         if(currentMenu.equals("List")) {
            // go back to menu
            mainMenu();
         } else if(currentMenu.equals("Add")) {
            // go back to menu
            mainMenu();
         }
      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
            case 0: listStocks();break;
            case 1: addStock();break;
         }
      }
   
public voiddestroyApp(boolean unconditional)

      try {
         db.close();
      } catch(Exception e) {}
      notifyDestroyed();
   
public java.lang.StringgetQuote(java.lang.String input)

      String url = quoteServer + input + quoteFormat;
      StreamConnection c = (StreamConnection)Connector.open(
                            url, Connector.READ_WRITE);
      InputStream is = c.openInputStream();
      StringBuffer sb = new StringBuffer();
      int ch;
      while((ch = is.read()) != -1) {
        sb.append((char)ch);
      }
      return(sb.toString());
   
public voidlistStocks()

      choose = new List("Choose Stocks", Choice.MULTIPLE);
      choose.setTicker(new Ticker(tickerString()));
      choose.addCommand(backCommand);
      choose.setCommandListener(this);
      try {
         RecordEnumeration re = db.enumerate();
         while(re.hasNextElement()) {
            String theStock = new String(re.nextRecord());
            choose.append(Stock.getName(theStock)+" @ " +
                          Stock.getPrice(theStock),null);
         }
      } catch(Exception ex) {}
      display.setCurrent(choose);
      currentMenu = "List"; 
   
voidmainMenu()

      display.setCurrent(menu);
      currentMenu = "Main"; 
   
public voidpauseApp()

      display = null;
      choose = null;
      menu = null;
      ticker = null;
       
      try {
         db.close();
         db = null;
      } catch(Exception e) {}
   
public voidstartApp()

      display = Display.getDisplay(this);
      // open a db stock file
      try {
         db = new StockDB("mystocks");
      } catch(Exception e) {}
         menu = new List("Stocks Database", Choice.IMPLICIT);
         menu.append("List Stocks", null);
         menu.append("Add A New Stock", null);
         menu.addCommand(exitCommand);
         menu.setCommandListener(this);
         menu.setTicker(ticker);

         mainMenu();
   
public java.lang.StringtickerString()

      StringBuffer ticks = null;
      try {
         RecordEnumeration enum = db.enumerate();
         ticks = new StringBuffer();
         while(enum.hasNextElement()) {
            String stock1 = new String(enum.nextRecord());
            ticks.append(Stock.getName(stock1));
            ticks.append(" @ ");
            ticks.append(Stock.getPrice(stock1));
            ticks.append("    ");
         }
      } catch(Exception ex) {}
         return (ticks.toString());