StockForm.java
上传用户:whhzxy
上传日期:2009-12-31
资源大小:269k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. /**
  3.   * 股票窗口。
  4.   * @author 嘟嘟熊
  5.   * @version 1.0
  6.   */
  7. public class StockForm extends Form implements CommandListener {
  8.   StringItem priceAndAmplitude;
  9.   StringItem playMoney;
  10.   StringItem buyPriceAndAmount;
  11.   StringItem explain;
  12.   private KMRichMan richManObject;
  13.   private PlayCanvas playCanvas;
  14.   /**
  15.  * 股票的索引
  16.  */
  17.   int index;
  18.   /**
  19.  * 这个东东是什么呢?标尺??就算吧:)
  20.  */
  21.   Gauge gauge;
  22.   /**
  23.  * 构造一个对象
  24.  *  @param kmrichman 别问我,自己知道
  25.  *  @param playCanvas 游戏画板
  26.  *  @param index 股票的索引(第几个股票,不是有四个股票吗?0,1,2,3)
  27.  */
  28.   public StockForm(KMRichMan kmrichman,PlayCanvas playCanvas,int index) {
  29.     super("");
  30.     this.richManObject = kmrichman;
  31.     this.playCanvas = playCanvas;
  32.     this.index = index;
  33.     this.setTitle(this.playCanvas.stock_name[index]);
  34.     priceAndAmplitude = new StringItem("","价格:$" + playCanvas.stock_price[index] +
  35.                                        " [" + playCanvas.stock_amplitude[index] + "%]");
  36.     buyPriceAndAmount = new StringItem("", "持有价:$" +
  37.                               playCanvas.player_stock[playCanvas.nowPlayerID][index][1] +
  38.                               "[" + playCanvas.player_stock[playCanvas.nowPlayerID][index][0] + "]");
  39.     playMoney = new StringItem("", "现金:$" +
  40.                             playCanvas.player_money[playCanvas.nowPlayerID]);
  41.     gauge = new Gauge("", true, 10, 0);
  42.     explain = new StringItem("", "说明:n购入时,百分比指用现金的百分之多少购股票;" +
  43.                              "卖出时,指卖出购入股票的百分之多少.");
  44.     setCommandListener(this);
  45.     addCommand(new Command("返回", Command.OK, 0));
  46.     addCommand(new Command("购入", Command.OK, 1));
  47.     addCommand(new Command("卖出", Command.OK, 2));
  48.     this.append(priceAndAmplitude);
  49.     this.append(buyPriceAndAmount);
  50.     this.append(playMoney);
  51.     this.append(gauge);
  52.     this.append(explain);
  53.   }
  54.   /**
  55.    * 处理按键
  56.    */
  57.   public void commandAction(Command command, Displayable displayable) {
  58.     if (command.getLabel() == "返回") {
  59.       richManObject.setDisplayToStockList();
  60.     }
  61.     if (command.getLabel() == "购入") { //购入哦
  62.       int money = playCanvas.player_money[playCanvas.nowPlayerID]; // 角色的钱
  63.       int stock_money = money * this.gauge.getValue() / 10; //用于购买股票的钱
  64.       int stock_price = playCanvas.stock_price[index]; // 股票价格
  65.       int amount = 0;
  66.       if (stock_money >=  stock_price) {
  67.         amount = stock_money / stock_price;
  68.         playCanvas.player_money[playCanvas.nowPlayerID] -= stock_price * amount;
  69.         playCanvas.player_stock[playCanvas.nowPlayerID][index][1] = //计算购入价格
  70.             (playCanvas.player_stock[playCanvas.nowPlayerID][index][1] *
  71.             playCanvas.player_stock[playCanvas.nowPlayerID][index][0] +
  72.             stock_price * amount) / (playCanvas.player_stock[playCanvas.nowPlayerID][index][0]
  73.                                + amount);
  74.         playCanvas.player_stock[playCanvas.nowPlayerID][index][0] += amount;
  75.         this.buyPriceAndAmount.setText("持有价:$" +
  76.                               playCanvas.player_stock[playCanvas.nowPlayerID][index][1] +
  77.                               "[" + playCanvas.player_stock[playCanvas.nowPlayerID][index][0] + "]");
  78.         this.playMoney.setText("现金:$" +
  79.                             playCanvas.player_money[playCanvas.nowPlayerID]);
  80.       }
  81.     }
  82.     if (command.getLabel() == "卖出") { //卖出股票了,写的不好,请大家包涵。。。。
  83.       int totalAmount = playCanvas.player_stock[playCanvas.nowPlayerID][index][0];
  84.       int amount = totalAmount * this.gauge.getValue() / 10;
  85.       int stock_price = playCanvas.stock_price[index];
  86.       if (amount > 0) {
  87.         playCanvas.player_money[playCanvas.nowPlayerID] += stock_price * amount;
  88.         playCanvas.player_stock[playCanvas.nowPlayerID][index][0] -= amount;
  89.         if (playCanvas.player_stock[playCanvas.nowPlayerID][index][0] == 0)
  90.           playCanvas.player_stock[playCanvas.nowPlayerID][index][1] = 0;
  91.         this.buyPriceAndAmount.setText("持有价:$" +
  92.                               playCanvas.player_stock[playCanvas.nowPlayerID][index][1] +
  93.                               "[" + playCanvas.player_stock[playCanvas.nowPlayerID][index][0] + "]");
  94.         this.playMoney.setText("现金:$" +
  95.                             playCanvas.player_money[playCanvas.nowPlayerID]);
  96.       }
  97.     }
  98.   }
  99. }