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. /*
  17. * Created on 2007-1-24
  18. *
  19. */
  20. package peaklau.eaglefund;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.DataInputStream;
  24. import java.io.DataOutputStream;
  25. import java.util.Calendar;
  26. import java.util.Date;
  27. import javax.microedition.rms.RecordEnumeration;
  28. import javax.microedition.rms.RecordStore;
  29. /**
  30. * DataCenter class wraps all the Record operations.<br>
  31. *
  32. * <br>
  33. * @author peaklau <br>
  34. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  35. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  36. * @version $Revision: 1.1 $ $Date: 2007/05/09 16:07:03 $
  37. */
  38. public class DataCenter{
  39. public static final String RECORD_NAME="EagleFund";
  40. public static void delFund(String code){
  41. RecordEnumeration re=null;
  42. RecordStore rs=null;
  43. try{
  44. rs=RecordStore.openRecordStore(RECORD_NAME,true);
  45. re=rs.enumerateRecords(null,null,false);
  46. int index=-1;
  47. while(re.hasNextElement()){
  48. int recID=re.nextRecordId();
  49. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  50. DataInputStream dis=new DataInputStream(bais);
  51. if(code.equals(dis.readUTF())){
  52. index=recID;
  53. }
  54. dis.close();
  55. bais.close();
  56. }
  57. if(index!=-1){
  58. rs.deleteRecord(index);
  59. }
  60. }catch(Exception e){
  61. e.printStackTrace();
  62. }finally{
  63. if(re!=null){
  64. re.destroy();
  65. }
  66. try{
  67. if(rs!=null){
  68. rs.closeRecordStore();
  69. }
  70. }catch(Exception e){
  71. }
  72. }
  73. }
  74. /**
  75. * addFund
  76. * @param code
  77. * @param name
  78. * @param quantity
  79. * @return
  80. */
  81. public static boolean addFund(String code,String name,int quantity){
  82. //System.out.println("addFund code="+code+" name="+name+" quantity="+quantity);
  83. RecordEnumeration re=null;
  84. RecordStore rs=null;
  85. try{
  86. rs=RecordStore.openRecordStore(RECORD_NAME,true);
  87. re=rs.enumerateRecords(null,null,false);
  88. boolean existed=false;
  89. while(re.hasNextElement()){
  90. int recID=re.nextRecordId();
  91. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  92. DataInputStream dis=new DataInputStream(bais);
  93. if(code.equals(dis.readUTF())){
  94. existed=true;
  95. }
  96. dis.close();
  97. bais.close();
  98. }
  99. if(existed){
  100. return false;
  101. }
  102. ByteArrayOutputStream baos=new ByteArrayOutputStream();
  103. DataOutputStream dos=new DataOutputStream(baos);
  104. dos.writeUTF(code);//code
  105. dos.writeInt(0);//value
  106. dos.writeInt(quantity);//quantity
  107. dos.writeUTF("未知");//date
  108. dos.writeInt(0);//percent
  109. if(name==null){
  110. dos.writeUTF("未知");
  111. }else{
  112. dos.writeUTF(name);
  113. }
  114. dos.close();
  115. baos.close();
  116. rs.addRecord(baos.toByteArray(),0,baos.toByteArray().length);
  117. return true;
  118. }catch(Exception e){
  119. e.printStackTrace();
  120. return false;
  121. }finally{
  122. if(re!=null){
  123. re.destroy();
  124. }
  125. try{
  126. if(rs!=null){
  127. rs.closeRecordStore();
  128. }
  129. }catch(Exception e){
  130. }
  131. }
  132. }
  133. public static void updateRecordQantity(String code,int quantity){
  134. //System.out.println("updateRecordQantity code="+code+" quantity="+quantity);
  135. RecordEnumeration re=null;
  136. RecordStore rs=null;
  137. try{
  138. rs=RecordStore.openRecordStore(RECORD_NAME,true);
  139. re=rs.enumerateRecords(null,null,false);
  140. while(re.hasNextElement()){
  141. int recID=re.nextRecordId();
  142. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  143. DataInputStream dis=new DataInputStream(bais);
  144. String ccode=dis.readUTF();
  145. //System.out.println("ccode="+ccode);
  146. int vvalue=dis.readInt();
  147. int qquantity=dis.readInt();
  148. //System.out.println("qquantity="+qquantity);
  149. String ddate=dis.readUTF();
  150. int ppercent=dis.readInt();
  151. String nname=dis.readUTF();
  152. if(ccode.equals(code)){
  153. //System.out.println("updateRecordQantity");
  154. ByteArrayOutputStream baos=new ByteArrayOutputStream();
  155. DataOutputStream dos=new DataOutputStream(baos);
  156. dos.writeUTF(code);//code
  157. dos.writeInt(vvalue);//value
  158. dos.writeInt(quantity);//qantity
  159. dos.writeUTF(ddate);//date
  160. dos.writeInt(ppercent);//percent
  161. dos.writeUTF(nname);
  162. dos.close();
  163. baos.close();
  164. rs.setRecord(recID,baos.toByteArray(),0,baos.toByteArray().length);
  165. }
  166. dis.close();
  167. bais.close();
  168. }
  169. }catch(Exception e){
  170. e.printStackTrace();
  171. }finally{
  172. if(re!=null){
  173. re.destroy();
  174. }
  175. try{
  176. if(rs!=null){
  177. rs.closeRecordStore();
  178. }
  179. }catch(Exception e){
  180. }
  181. }
  182. }
  183. public static void updateRecord(String code,String name,String date,int value,int percent){
  184. //System.out.println("updateRecord code="+code+" name="+name+" date="+date+" value="+value+" percent="+percent);
  185. RecordEnumeration re=null;
  186. RecordStore rs=null;
  187. try{
  188. rs=RecordStore.openRecordStore(RECORD_NAME,true);
  189. re=rs.enumerateRecords(null,null,false);
  190. while(re.hasNextElement()){
  191. int recID=re.nextRecordId();
  192. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  193. DataInputStream dis=new DataInputStream(bais);
  194. String ccode=dis.readUTF();
  195. //System.out.println("ccode="+ccode);
  196. int vvalue=dis.readInt();
  197. int qquantity=dis.readInt();
  198. //System.out.println("qquantity="+qquantity);
  199. String ddate=dis.readUTF();
  200. int ppercent=dis.readInt();
  201. String nname=dis.readUTF();
  202. if(ccode.equals(code)){
  203. ByteArrayOutputStream baos=new ByteArrayOutputStream();
  204. DataOutputStream dos=new DataOutputStream(baos);
  205. dos.writeUTF(code);//code
  206. dos.writeInt(value);//value
  207. dos.writeInt(qquantity);//qantity
  208. dos.writeUTF(date);//date
  209. dos.writeInt(percent);//percent
  210. dos.writeUTF(name);
  211. dos.close();
  212. baos.close();
  213. rs.setRecord(recID,baos.toByteArray(),0,baos.toByteArray().length);
  214. }
  215. dis.close();
  216. bais.close();
  217. }
  218. }catch(Exception e){
  219. e.printStackTrace();
  220. }finally{
  221. if(re!=null){
  222. re.destroy();
  223. }
  224. try{
  225. if(rs!=null){
  226. rs.closeRecordStore();
  227. }
  228. }catch(Exception e){
  229. }
  230. }
  231. }
  232. public static String getFundRequestString(){
  233. Calendar calendar=Calendar.getInstance();
  234. int year=calendar.get(Calendar.YEAR);
  235. int month=calendar.get(Calendar.MONTH);
  236. int day=calendar.get(Calendar.DAY_OF_MONTH);
  237. int dateOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
  238. if(dateOfWeek==7){//SAT
  239. calendar.setTime(new Date(System.currentTimeMillis()-DAY));
  240. }else if(dateOfWeek==1){//SUN
  241. calendar.setTime(new Date(System.currentTimeMillis()-2L*DAY));
  242. }
  243. year=calendar.get(Calendar.YEAR);
  244. month=calendar.get(Calendar.MONTH);
  245. day=calendar.get(Calendar.DAY_OF_MONTH);
  246. month++;
  247. String ddate=year+"-"+(month<10?("0"+month):(""+month))+"-"+(day<10?("0"+day):(""+day));
  248. //System.out.println("ddate="+ddate);
  249. RecordEnumeration re=null;
  250. StringBuffer sb=new StringBuffer("");
  251. RecordStore rs=null;
  252. try{
  253. rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
  254. re=rs.enumerateRecords(null,null,false);
  255. int index=0;
  256. while(re.hasNextElement()){
  257. int recID=re.nextRecordId();
  258. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  259. DataInputStream dis=new DataInputStream(bais);
  260. String code=dis.readUTF();
  261. int value=dis.readInt();
  262. int quantity=dis.readInt();
  263. String date=dis.readUTF();
  264. if((date==null)||(!ddate.equals(date))){
  265. if(index==0){
  266. sb.append("C"+index+"="+code);
  267. }else{
  268. sb.append("&C"+index+"="+code);
  269. }
  270. index++;
  271. }
  272. dis.close();
  273. bais.close();
  274. }
  275. return sb.toString();
  276. }catch(Exception e){
  277. e.printStackTrace();
  278. }finally{
  279. if(re!=null){
  280. re.destroy();
  281. }
  282. try{
  283. if(rs!=null){
  284. rs.closeRecordStore();
  285. }
  286. }catch(Exception e){
  287. }
  288. }
  289. return null;
  290. }
  291. private static long DAY=24L*3600L*1000L;
  292. private static String[]ZEROS={"","0","00","000","0000","00000"};
  293. public static String[] getFunds(){
  294. String[] ret=new String[]{" "};
  295. RecordEnumeration re=null;
  296. RecordStore rs=null;
  297. try{
  298. rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
  299. re=rs.enumerateRecords(null,null,false);
  300. int n=re.numRecords();
  301. ret=new String[n];
  302. int index=0;
  303. while(re.hasNextElement()){
  304. int recID=re.nextRecordId();
  305. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  306. DataInputStream dis=new DataInputStream(bais);
  307. String code=dis.readUTF();
  308. int value=dis.readInt();
  309. int quantity=dis.readInt();
  310. String date=dis.readUTF();
  311. int percent=dis.readInt();
  312. String name=dis.readUTF();
  313. ret[index++]=name+"("+code+")";
  314. dis.close();
  315. bais.close();
  316. }
  317. }catch(Exception e){
  318. e.printStackTrace();
  319. }finally{
  320. if(re!=null){
  321. re.destroy();
  322. }
  323. try{
  324. if(rs!=null){
  325. rs.closeRecordStore();
  326. }
  327. }catch(Exception e){
  328. }
  329. }
  330. //System.out.println("getFunds "+ret);
  331. return ret;
  332. }
  333. /**
  334. * code/value(0.1234)/quantity/date(061103)/percent(-312)
  335. * @return
  336. */
  337. public static String getValueDesc(){
  338. RecordEnumeration re=null;
  339. StringBuffer sb=new StringBuffer();
  340. RecordStore rs=null;
  341. try{
  342. rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
  343. re=rs.enumerateRecords(null,null,false);
  344. BigInteger allTotal=new BigInteger(0);
  345. BigInteger total1=new BigInteger(0);
  346. BigInteger total2=new BigInteger(0);
  347. BigInteger addPerDayPerFund=new BigInteger(0);
  348. BigInteger addPerDay=new BigInteger(0);
  349. while(re.hasNextElement()){
  350. int recID=re.nextRecordId();
  351. ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
  352. DataInputStream dis=new DataInputStream(bais);
  353. String code=dis.readUTF();
  354. int value=dis.readInt();
  355. int quantity=dis.readInt();
  356. String date=dis.readUTF();
  357. int percent=dis.readInt();
  358. String name=dis.readUTF();
  359. sb.append(" \r\n "+name+"\r\n");
  360. sb.append(" ( "+code+" )"+"\r\n");
  361. sb.append(" 日期 = "+date+"\r\n");
  362. sb.append(" 价格 = "+(value10000L)+"."+getFormatInteger(value%10000L,4)+" 元\r\n");
  363. sb.append(" 数量 = "+(quantity100L)+"."+getFormatInteger(quantity%100L,2)+"\r\n");
  364. BigInteger total=BigInteger.mul(new BigInteger(value),new BigInteger(quantity));
  365. allTotal=BigInteger.add(allTotal,total);
  366. BigInteger tmp=BigInteger.div(total,new BigInteger(10000));
  367. long n=tmp.getValue();
  368. sb.append(" 总计 = "+(n100L)+"."+getFormatInteger(n%100L,2)+"\r\n");
  369. BigInteger oldValue=BigInteger.div(BigInteger.mul(new BigInteger(value),new BigInteger(1000000)),new BigInteger(percent+1000000));
  370. BigInteger tmp1=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(percent));
  371. total1=BigInteger.add(total1,tmp1);
  372. BigInteger tmp2=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(10000));
  373. total2=BigInteger.add(total2,tmp2);
  374. //System.out.println("code="+code+" tmp1="+tmp1.getValue()+" tmp2="+tmp2.getValue());
  375. addPerDayPerFund=BigInteger.div(tmp1,BigInteger.mul(new BigInteger(10000),new BigInteger(1000000)));
  376. addPerDay=BigInteger.add(addPerDay,addPerDayPerFund);
  377. long addPerDayPerFundValue=addPerDayPerFund.getValue();
  378. if(percent>0){
  379. sb.append("<00ff0000> 盈亏 = "+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"("+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
  380. }else if(percent<0){
  381. percent=0-percent;
  382. addPerDayPerFundValue=0-addPerDayPerFundValue;
  383. sb.append("<0000ff00> 盈亏 = -"+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"(-"+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
  384. }else{
  385. percent=0-percent;
  386. sb.append(" 盈亏 = "+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"("+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
  387. }
  388. dis.close();
  389. bais.close();
  390. }
  391. long addPerDayValue=addPerDay.getValue();
  392. sb.append(" \r\n --------\r\n");
  393. if(total2.getValue()==0){
  394. sb.append(" 平均盈亏 = 0.00%\r\n");
  395. sb.append(" 累计盈亏 = "+addPerDayValue100L+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
  396. }else{
  397. BigInteger tmp2=BigInteger.mul(total1,new BigInteger(100L));
  398. tmp2=BigInteger.div(tmp2,total2);
  399. long result=tmp2.getValue();
  400. if(result>=0){
  401. sb.append("<00ff0000> 平均盈亏 = "+(result100)+"."+getFormatInteger(result%100,2)+"%\r\n");
  402. sb.append("<00ff0000> 累计盈亏 = "+addPerDayValue100+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
  403. }else{
  404. result=0-result;
  405. addPerDayValue=0-addPerDayValue;
  406. sb.append("<0000ff00> 平均盈亏 = -"+(result100L)+"."+getFormatInteger(result%100L,2)+"%\r\n");
  407. sb.append("<0000ff00> 累计盈亏 = -"+addPerDayValue100L+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
  408. }
  409. }
  410. BigInteger tmp=BigInteger.div(allTotal,new BigInteger(10000L));
  411. long n=tmp.getValue();
  412. sb.append(" 累计= "+(n100L)+"."+getFormatInteger(n%100L,2)+" 元\r\n");
  413. return sb.toString();
  414. }catch(Exception e){
  415. e.printStackTrace();
  416. }finally{
  417. if(re!=null){
  418. re.destroy();
  419. }
  420. try{
  421. if(rs!=null){
  422. rs.closeRecordStore();
  423. }
  424. }catch(Exception e){
  425. }
  426. }
  427. return "";
  428. }
  429. public static String getFormatInteger(long num,int len){
  430. String tmp=num+"";
  431. return (tmp.length()<len?(ZEROS[len-tmp.length()]+tmp):tmp.substring(0,len));
  432. }
  433. }