📄 stockmidlet.java
字号:
* <p>Display the main menu of the program</p> */ private void mainMenu() { display.setCurrent(menu); currentMenu = "Main"; } /** * <p>Show the list of stocks to pick from and set the menu type to indicate * to the Listener what to do with the list choice</p> * * @param reload Indicates whether the list should be reloaded * which should * only happen if it is possible that the stocks have changed * since it was last shown * @param menuType Which menu is this list representing * @param type Type of Choice to display * @param prices Indicates whether or not to show prices on the list */ private void chooseStock(boolean reload, String menuType, int type, boolean prices) { if (reload) { choose = new List("Choose Stocks", type); choose.setTicker(stockTicker); choose.addCommand(BACK_COMMAND); if (menuType.equals("RemoveStock")) { choose.addCommand(DONE_COMMAND); } else if (menuType.equals("WhatChoose") || menuType.equals("AddAlert")) { choose.addCommand(MAIN_MENU_COMMAND); } choose.setCommandListener(new StockCommandListener()); try { RecordEnumeration re = stocks.enumerateRecords(); while (re.hasNextElement()) { String theStock = new String(re.nextRecord()); if (prices) { choose.append(Stock.getName(theStock) + " @ " + Stock.getStringPrice(theStock), null); } else { choose.append(Stock.getName(theStock), null); } } } catch (RecordStoreNotOpenException rsno) { } catch (RecordStoreException rs) { } } display.setCurrent(choose); currentMenu = menuType; } /** * <p>Display the stock selected on the View menu with all its * attributes shown (ie. Name, Price, etc.)</p> * * @param tkrSymbol The name of the stock to be deleted */ private void displayStock(String tkrSymbol) { try { String theStock = stocks.search(tkrSymbol); Form stockInfo = new Form(Stock.getName(theStock)); stockInfo.setTicker(stockTicker); StringBuffer sb = new StringBuffer() .append("Last Trade:\n ") .append(Stock.getTime(theStock)) .append("\n ") .append(Stock.getStringPrice(theStock)) .append("\nChange: ") .append(Stock.getStringChange(theStock)) .append("\nHigh: ") .append(Stock.getStringHigh(theStock)) .append("\nLow: ") .append(Stock.getStringLow(theStock)) .append("\nOpen: ") .append(Stock.getStringOpen(theStock)) .append("\nPrev: ") .append(Stock.getStringPrevious(theStock)); stockInfo.append(sb.toString()); stockInfo.addCommand(BACK_COMMAND); stockInfo.addCommand(MAIN_MENU_COMMAND); stockInfo.setCommandListener(new StockCommandListener()); display.setCurrent(stockInfo); currentMenu = "stockInfo"; } catch (RecordStoreNotOpenException rsno) { error("Could not display stock. ", 2000); } catch (RecordStoreException rs) { error("Could not display stock. ", 2000); } catch (NullPointerException npe) { error("Could not display stock. ", 2000); } } /** * <p>Show the What If? form to investigate a hypothetical stock deal</p> * * @param tkrSymbol The name of the stock to perform the query with */ private void whatIfForm(String tkrSymbol) { display.setCurrent(whatif); currentMenu = "WhatIfForm"; stockSymbol = tkrSymbol; } /** * <p>Show the alert management menu</p> * * @param showAlert Indicate whether we should show an alert to indicate * that we have just successfully added an alert */ private void alertMenu(boolean showAlert) { display.setCurrent(alertList); currentMenu = "AlertMenu"; if (showAlert) { Alert a = new Alert("", "\n\n\n Saved!", null, null); a.setTimeout(2000); display.setCurrent(a, alertList); } } /** * <p>Show a list of all active alerts</p> */ private void viewAlerts() { choose = new List("Current Alerts", Choice.MULTIPLE); choose.setTicker(stockTicker); try { // Get all the Alert records RecordEnumeration re = alerts.enumerateRecords("", 0); while (re.hasNextElement()) { String a = new String(re.nextRecord()); String price = Stock.convert(Integer.valueOf(a.substring(a.indexOf(';')+1, a.length())).intValue()); choose.append(a.substring(0, a.indexOf(';')) + " @ $" + price, null); } } catch (Exception e) { error("Error reading alerts", 2500); } choose.addCommand(BACK_COMMAND); choose.addCommand(DONE_COMMAND); choose.setCommandListener(new StockCommandListener()); display.setCurrent(choose); currentMenu = "RemoveAlert"; } /** * <p>Show the form to add an alert</p> * * @param tkrSymbol The ticker symbol of the stock we're adding an alert to */ private void alertForm(String tkrSymbol) { display.setCurrent(alertPriceBox); currentMenu = "AlertForm"; stockSymbol = tkrSymbol; } /** * <p>Remove the alert from our RecordStore referenced by index</p> * * @param choose_data A string with the symbol and price of the alert * to remove in it */ private void removeAlert(String choose_data) { try { // Separate the symbol and price from the data String symbol = choose_data.substring(0, choose_data.indexOf('@')-1); int sPrice = Stock.makeInt( choose_data.substring(choose_data.indexOf('@')+3, choose_data.length())); System.out.println("Remove Alert: " + symbol + ";" + sPrice); // Remove the alert alerts.delete(symbol + ";" + sPrice); } catch (Exception e) { error("Failed to remove alert", 2000); } } /** * <p>Show the settings menu</p> * * @param showAlert Indicate whether we should show an alert to indicate * that we have just successfully saved changes to the * settings */ private void settings(boolean showAlert) { display.setCurrent(settingsList); currentMenu = "Settings"; if (showAlert) { Alert a = new Alert("", "\n\n\n Saved!", null, null); a.setTimeout(1500); display.setCurrent(a, settingsList); } } /** * <p>Show the updates choices</p> */ private void updates() { display.setCurrent(updatesForm); currentMenu = "Updates"; } /** * <p>Show the screen to add a stock</p> */ private void addStock() { stockSymbolBox.setString(""); display.setCurrent(stockSymbolBox); currentMenu = "AddStock"; } /** * <p>Add the stock to the database</p> * <li> first contact the quote server to get the stock quote</li> * <li> if the stock doesn't not exist, alert the user and return to * the add screen</li> * <li> if the stock exists then add it to our list</li> * <li> if the addition of the stock was successful, return true * otherwise, return false</li> * <BR> * <p>This is the format returned by the quote.yahoo.com server in * the format * specified in the instance variable:</p> * <pre> * NAME TIME PRICE CHANGE LOW HIGH OPEN PREV * "SUNW","11:24AM - <b>79.0625</b>",-3.0625,"26.9375 - 106.75",80.5,82.125 * </pre> * <BR> * <p>This is what is returned if the stock is not found:</p> * <pre> * "NAME" ,"N/A - <b>0.00</b>" ,N/A ,"N/A - N/A" ,N/A ,N/A * </pre> * * @return whether or not the addition of the stock was successful * @param tkrSymbol The ticker symbol of the stock to add */ private boolean addNewStock(String tkrSymbol) { try { // When stocks.search() returns null, the stock doesn't yet exist if (stocks.search(tkrSymbol) == null) { try { stocks.add(getStockQuote(tkrSymbol)); stockTicker.setString(makeTickerString()); } catch (RecordStoreFullException rsf) { error("Database is full.", 2000); return false; } catch (RecordStoreException rs) { error("Failed to add " + tkrSymbol, 2000); return false; } catch (IOException ioe) { error("Failed to download stock quote for \"" + tkrSymbol + "\"", 2000); return false; } catch (NumberFormatException nfe) { error("\"" + tkrSymbol + "\" not found on server, or invalid data " + "received from server", 2000); return false; } // The stock already exists so we'll just update it } else { try { stocks.update(tkrSymbol, getStockQuote(tkrSymbol).getBytes()); stockTicker.setString(makeTickerString()); } catch (RecordStoreFullException rsf) { error("Database is full.", 2000); return false; } catch (RecordStoreException rs) { error("Failed to update " + tkrSymbol, 2000); return false; } catch (IOException ioe) { error("Failed to download stock quote for " + tkrSymbol, 2000); return false; } } } catch (RecordStoreException rs) { error("Error accessing database.", 2000); return false; } return true; } /** * <p>This method actually contacts the server, downloads and returns * the stock quote.</p> * * NOTE: If PROXY support is added to HttpConnection that switch the code * over to that as it will be pure MIDP instead of this hack * * @return the stock quote * @param tkrSymbol The Stock to be requested from the server * @throws <code>IOException</code> is thrown if there is a problem * negotiating a connection with the server * @throws <code>NumberFormatException</code> is thrown if trashed data * is received from the server (or the Stock could not be found) */ private String getStockQuote(String tkrSymbol) throws IOException, NumberFormatException { String quoteURL = quoteServerURL + tkrSymbol + quoteFormat; StreamConnection c = (StreamConnection) Connector.open(quoteURL, Connector.READ_WRITE); InputStream is = c.openInputStream(); int ch; StringBuffer sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb.append((char)ch); } Stock.parse(sb.toString()); is.close(); c.close(); return sb.toString(); } /** * <p>Remove the stock from the <code>StockDatabase</code></p> * * @param tkrSymbol The ticker symbol of the stock to delete */ private void deleteStock(String tkrSymbol) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -