返回顶部

收藏

ObtainQuoteMIDlet

更多
//Listing 7.5
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class ObtainQuoteMIDlet extends MIDlet {

   private Display displayMngr = null;
   private EntryForm entryForm = null;
   private Alert resultsAlert = null;
   private Ticker adTicker =
   new Ticker("Track your investments with Investment Tracker");

   public ObtainQuoteMIDlet () {
   }

   private void initListener () {
      ItemStateListener itemListener = new ItemStateListener () {
         public void itemStateChanged (Item item) {
            if ((item == entryForm.getInvestmentChoice()) && (entryForm.getInvestmentChoice().getSelectedIndex() == 1) && !(entryForm.getSymbolField().getString().toUpperCase().endsWith("X"))) {
               Alert symbolAlert = new Alert("Check Symbol","Mutual Funds end in 'X'", null, AlertType.WARNING);
               symbolAlert.setTimeout(Alert.FOREVER);
               displayMngr.setCurrent(symbolAlert, entryForm);
            }
         }
      };
      CommandListener commandListener = new CommandListener() {
         public void commandAction(Command c, Displayable d) {
            if (c == entryForm.getExitCommand()) {
               destroyApp(true);
            } else if (c == entryForm.getGetCommand()) {
               if ((entryForm.getInvestmentChoice().getSelectedIndex() == 1) && !(entryForm.getSymbolField().getString().toUpperCase().endsWith("X"))){
                  Alert symbolAlert = new Alert("Check Symbol","Mutual Funds end in 'X'", null, AlertType.WARNING);
                  symbolAlert.setTimeout(Alert.FOREVER);
                  displayMngr.setCurrent(symbolAlert, entryForm);
               } else if (entryForm.getSymbolField().getString().length() > 0) {
                  String sym = entryForm.getSymbolField().getString();
                  int type = entryForm.getInvestmentChoice().getSelectedIndex();
                  int[] price = QuoteService.getPrice(sym, type);
                  if (price != null) {
                     storePrice(sym, price);
                     displayPrice("The price of " + sym + " is $" + price[0] + "." + price[1]);
                  } else {
                     Alert symbolAlert = new Alert("Check Symbol/Type", "No quote found.", null, AlertType.WARNING);
                     symbolAlert.setTimeout(Alert.FOREVER);
                     displayMngr.setCurrent(symbolAlert, entryForm);
                  }
               }
            }
         }
      };
      entryForm.setItemStateListener(itemListener);
      entryForm.setCommandListener(commandListener);
   }

   private void displayEntryForm () {
      if (entryForm == null) {
         entryForm = new EntryForm("ObtainQuote");
      }
      initListener();
      displayMngr.setCurrent(entryForm);
   }

   private void displayPrice(String quoteString) {
      if (resultsAlert == null) {
         resultsAlert = new Alert("Quote Price", null, null,
         AlertType.CONFIRMATION);
         resultsAlert.setTicker(adTicker);
         resultsAlert.setTimeout(Alert.FOREVER);
      }
      resultsAlert.setString(quoteString);
      displayMngr.setCurrent(resultsAlert, entryForm);
   }

   private void storePrice(String symbol, int[] price) {
      String newRecord = symbol + ";" + price[0] + ";" + price[1];
      byte[] byteRec;
      try {
         RecordStore anRMS = RecordStore.openRecordStore("Quotes" , true);
         RecordFilter rf = new QuoteFilter(symbol);
         RecordEnumeration rEnum = anRMS.enumerateRecords(rf,null,false);
         if (rEnum.hasNextElement()) {
            int recId = rEnum.nextRecordId();
            newRecord += ';' + getLastPrice(anRMS.getRecord(recId));
            byteRec = newRecord.getBytes();
            anRMS.setRecord(recId,byteRec,0,byteRec.length);
         } else {
            byteRec = newRecord.getBytes();
            anRMS.addRecord(byteRec,0,byteRec.length);
         }
         rEnum.destroy();
         anRMS.closeRecordStore();
      } catch (RecordStoreFullException fullStore) {
         //handle a full record store problem
      } catch (RecordStoreNotFoundException notFoundException) {
         //handle store not found which should not happen with the
      } catch (RecordStoreException recordStoreException) {
         //handling record store problems
      }
   }

   private int[] parsePrices(byte[] quoteRec) {
      String rec = new String(quoteRec);
      int dollar1Pos = rec.indexOf(';');
      int cent1Pos = rec.indexOf(';',dollar1Pos+1);
      int dollar2Pos = rec.indexOf(';',cent1Pos + 1);
      if (dollar2Pos > 0) { //had a historical price
         int cent2Pos = rec.indexOf(';',dollar2Pos + 1);
         int currentDollars = Integer.parseInt(rec.substring(dollar1Pos + 1,cent1Pos));
         int currentCents = Integer.parseInt(rec.substring(cent1Pos + 1,dollar2Pos));
         int historicalDollars = Integer.parseInt(rec.substring(dollar2Pos + 1,cent2Pos));
         int historicalCents = Integer.parseInt(rec.substring(cent2Pos + 1));
         int[] returnPrices = {currentDollars, currentCents, historicalDollars, historicalCents};
         return returnPrices;
      } else { //no previous historical price
         int currentDollars = Integer.parseInt(rec.substring(dollar1Pos + 1, cent1Pos));
         int currentCents = Integer.parseInt(rec.substring(cent1Pos + 1));
         int[] returnPrices = {currentDollars, currentCents};
         return returnPrices;
      }
   }

   private String getLastPrice(byte[] rec) {
      String recString = new String(rec);
      int dollarPos = recString.indexOf(';');
      int centPos = recString.indexOf(';',dollarPos+1);
      int centEnd = recString.indexOf(';',centPos + 1);
      if (centEnd > 0) //had a historical price
         return recString.substring(dollarPos+1,centEnd);
      else //no previous historical price
         return recString.substring(dollarPos+1);
   }

   protected void startApp() {
      displayMngr = Display.getDisplay(this);
      displayEntryForm();
   }

   protected void pauseApp() {
   }

   protected void destroyApp(boolean unconditional) {
      notifyDestroyed();
   }

   public void commandAction(Command c, Displayable s) {
   }
}
//该片段来自于http://outofmemory.cn

标签:java,基础

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. Reverse Engineering 发表 2015-07-27 02:24:29 逆向基础(十三) JAVA (二)
  2. Terry Zhang 发表 2014-05-09 15:21:58 Java 中的访问修饰符
  3. ticmy 发表 2012-11-08 12:42:26 对象实例化的顺序
  4. root 发表 2015-04-27 07:10:01 Java 基础:hashCode方法
  5. 博主 发表 2015-07-15 02:27:34 读取文件的正确方式
  6. ImportNew读者投稿 发表 2013-03-14 09:48:51 Java到底是传引用还是传值?
  7. Terry Zhang 发表 2014-04-22 03:30:42 [正则] 数量词
  8. root 发表 2015-04-19 01:56:01 黑马程序员——java基础—IO(input output)流字符流
  9. 博主 发表 2015-04-10 02:35:00 titanium module java基础: 记得使用annotation
  10. Terry Zhang 发表 2014-04-22 16:22:45 [正则] 捕获组
  11. Terry Zhang 发表 2014-05-06 04:18:19 [集合框架] Map 接口
  12. root 发表 2015-04-26 08:48:01 黑马程序员_JavaSE学习总结第02天_Java基础语法

发表评论