1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package peaklau.eaglefund;
  17. import javax.microedition.lcdui.Alert;
  18. import javax.microedition.lcdui.AlertType;
  19. import javax.microedition.lcdui.Command;
  20. import javax.microedition.lcdui.CommandListener;
  21. import javax.microedition.lcdui.Displayable;
  22. import javax.microedition.lcdui.Form;
  23. import javax.microedition.lcdui.TextField;
  24. /**
  25. * User Interface for adding funds.<br>
  26. *
  27. * <br>
  28. * @author peaklau <br>
  29. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  30. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  31. * @version $Revision: 1.1 $ $Date: 2007/05/09 16:07:03 $
  32. */
  33. public class FundAddScreen extends Form implements CommandListener {
  34. private Command back=new Command("返回",Command.BACK,1);
  35. private Command add=new Command("确认添加",Command.OK,1);
  36. private Alert alert=null;
  37. private INavigator navigator=null;
  38. private TextField code=null;
  39. private TextField quantity=null;
  40. public FundAddScreen(INavigator navigator){
  41. super("添加基金");
  42. this.navigator=navigator;
  43. code=new TextField("基金代码:","",6,TextField.NUMERIC);
  44. quantity=new TextField("份额(格式123.12):","",9,TextField.ANY);
  45. this.append(code);
  46. this.append(quantity);
  47. addCommand(add);
  48. addCommand(back);
  49. setCommandListener(this);
  50. alert=new Alert("提示信息");
  51. }
  52. public void commandAction(Command command, Displayable displayable) {
  53. if(command == back){
  54. if(navigator!=null){
  55. navigator.getDisplay().setCurrent(navigator.getDisplayable("mainScreen"));
  56. }
  57. }else if(command == add){
  58. String ccode="000000";
  59. if(code.getString().length()!=0){
  60. ccode=code.getString();
  61. }
  62. String squantity=quantity.getString();
  63. boolean error=false;
  64. for(int i=0;i<squantity.length();i++){
  65. char c=squantity.charAt(i);
  66. if(((c>='0')&&(c<='9'))||(c=='.')){
  67. }else{
  68. error=true;
  69. }
  70. }
  71. if(error){
  72. alert.setString("数量格式错误");
  73. alert.setType(AlertType.ERROR);
  74. navigator.getDisplay().setCurrent(alert,this);
  75. return;
  76. }
  77. int qquantity=0;
  78. int index=squantity.indexOf(".");
  79. if(index>=0){
  80. qquantity+=100*Integer.parseInt(squantity.substring(0,index));
  81. if(index+1<squantity.length()){
  82. qquantity+=10*Integer.parseInt(squantity.substring(index+1,index+2));
  83. }
  84. if(index+2<squantity.length()){
  85. qquantity+=Integer.parseInt(squantity.substring(index+2,index+3));
  86. }
  87. }else{
  88. if(squantity.length()==0){
  89. qquantity=0;
  90. }else{
  91. int tmp=Integer.parseInt(squantity);
  92. if(tmp>20000000){
  93. alert.setString("数量太大");
  94. alert.setType(AlertType.ERROR);
  95. navigator.getDisplay().setCurrent(alert,this);
  96. return;
  97. }
  98. qquantity=100*Integer.parseInt(squantity);
  99. }
  100. }
  101. if(Integer.parseInt(ccode)==0){
  102. alert.setString("基金代码有误!");
  103. alert.setType(AlertType.ERROR);
  104. navigator.getDisplay().setCurrent(alert,this);
  105. }
  106. if(DataCenter.addFund(ccode,null,qquantity)){
  107. alert.setString("添加成功!");
  108. alert.setType(AlertType.INFO);
  109. navigator.getDisplay().setCurrent(alert,navigator.getDisplayable("valueScreen"));
  110. navigator.getCanvas("valueScreen").refresh();
  111. }else{
  112. alert.setString("该基金已经存在了!");
  113. alert.setType(AlertType.ERROR);
  114. navigator.getDisplay().setCurrent(alert,this);
  115. }
  116. //System.out.println(code.getString()+" "+quantity.getString());
  117. }
  118. }
  119. }