- /*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /*
- * Created on 2007-1-24
- *
- */
- package peaklau.eaglefund;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.util.Calendar;
- import java.util.Date;
-
-
- import javax.microedition.rms.RecordEnumeration;
- import javax.microedition.rms.RecordStore;
- /**
- * DataCenter class wraps all the Record operations.<br>
- *
- * <br>
- * @author peaklau <br>
- * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
- * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
- * @version $Revision: 1.1 $ $Date: 2007/05/09 16:07:03 $
- */
-
- public class DataCenter{
- public static final String RECORD_NAME="EagleFund";
- public static void delFund(String code){
- RecordEnumeration re=null;
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- int index=-1;
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- if(code.equals(dis.readUTF())){
- index=recID;
- }
- dis.close();
- bais.close();
- }
- if(index!=-1){
- rs.deleteRecord(index);
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- }
- /**
- * addFund
- * @param code
- * @param name
- * @param quantity
- * @return
- */
- public static boolean addFund(String code,String name,int quantity){
- //System.out.println("addFund code="+code+" name="+name+" quantity="+quantity);
- RecordEnumeration re=null;
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- boolean existed=false;
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- if(code.equals(dis.readUTF())){
- existed=true;
- }
- dis.close();
- bais.close();
- }
- if(existed){
- return false;
- }
-
- ByteArrayOutputStream baos=new ByteArrayOutputStream();
- DataOutputStream dos=new DataOutputStream(baos);
- dos.writeUTF(code);//code
- dos.writeInt(0);//value
- dos.writeInt(quantity);//quantity
- dos.writeUTF("未知");//date
- dos.writeInt(0);//percent
- if(name==null){
- dos.writeUTF("未知");
- }else{
- dos.writeUTF(name);
- }
- dos.close();
- baos.close();
- rs.addRecord(baos.toByteArray(),0,baos.toByteArray().length);
- return true;
- }catch(Exception e){
- e.printStackTrace();
- return false;
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- }
- public static void updateRecordQantity(String code,int quantity){
- //System.out.println("updateRecordQantity code="+code+" quantity="+quantity);
- RecordEnumeration re=null;
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- String ccode=dis.readUTF();
- //System.out.println("ccode="+ccode);
- int vvalue=dis.readInt();
- int qquantity=dis.readInt();
- //System.out.println("qquantity="+qquantity);
- String ddate=dis.readUTF();
- int ppercent=dis.readInt();
- String nname=dis.readUTF();
- if(ccode.equals(code)){
- //System.out.println("updateRecordQantity");
- ByteArrayOutputStream baos=new ByteArrayOutputStream();
- DataOutputStream dos=new DataOutputStream(baos);
- dos.writeUTF(code);//code
- dos.writeInt(vvalue);//value
- dos.writeInt(quantity);//qantity
- dos.writeUTF(ddate);//date
- dos.writeInt(ppercent);//percent
- dos.writeUTF(nname);
- dos.close();
- baos.close();
- rs.setRecord(recID,baos.toByteArray(),0,baos.toByteArray().length);
- }
- dis.close();
- bais.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- }
-
- public static void updateRecord(String code,String name,String date,int value,int percent){
- //System.out.println("updateRecord code="+code+" name="+name+" date="+date+" value="+value+" percent="+percent);
- RecordEnumeration re=null;
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- String ccode=dis.readUTF();
- //System.out.println("ccode="+ccode);
- int vvalue=dis.readInt();
- int qquantity=dis.readInt();
- //System.out.println("qquantity="+qquantity);
- String ddate=dis.readUTF();
- int ppercent=dis.readInt();
- String nname=dis.readUTF();
- if(ccode.equals(code)){
- ByteArrayOutputStream baos=new ByteArrayOutputStream();
- DataOutputStream dos=new DataOutputStream(baos);
- dos.writeUTF(code);//code
- dos.writeInt(value);//value
- dos.writeInt(qquantity);//qantity
- dos.writeUTF(date);//date
- dos.writeInt(percent);//percent
- dos.writeUTF(name);
- dos.close();
- baos.close();
- rs.setRecord(recID,baos.toByteArray(),0,baos.toByteArray().length);
- }
- dis.close();
- bais.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- }
-
- public static String getFundRequestString(){
- Calendar calendar=Calendar.getInstance();
- int year=calendar.get(Calendar.YEAR);
- int month=calendar.get(Calendar.MONTH);
- int day=calendar.get(Calendar.DAY_OF_MONTH);
- int dateOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
- if(dateOfWeek==7){//SAT
- calendar.setTime(new Date(System.currentTimeMillis()-DAY));
- }else if(dateOfWeek==1){//SUN
- calendar.setTime(new Date(System.currentTimeMillis()-2L*DAY));
- }
-
- year=calendar.get(Calendar.YEAR);
- month=calendar.get(Calendar.MONTH);
- day=calendar.get(Calendar.DAY_OF_MONTH);
- month++;
- String ddate=year+"-"+(month<10?("0"+month):(""+month))+"-"+(day<10?("0"+day):(""+day));
- //System.out.println("ddate="+ddate);
-
- RecordEnumeration re=null;
- StringBuffer sb=new StringBuffer("");
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- int index=0;
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- String code=dis.readUTF();
- int value=dis.readInt();
- int quantity=dis.readInt();
- String date=dis.readUTF();
- if((date==null)||(!ddate.equals(date))){
- if(index==0){
- sb.append("C"+index+"="+code);
- }else{
- sb.append("&C"+index+"="+code);
- }
- index++;
- }
- dis.close();
- bais.close();
- }
- return sb.toString();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- return null;
- }
- private static long DAY=24L*3600L*1000L;
-
-
-
-
-
-
- private static String[]ZEROS={"","0","00","000","0000","00000"};
-
-
-
- public static String[] getFunds(){
- String[] ret=new String[]{" "};
- RecordEnumeration re=null;
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- int n=re.numRecords();
- ret=new String[n];
- int index=0;
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- String code=dis.readUTF();
- int value=dis.readInt();
- int quantity=dis.readInt();
- String date=dis.readUTF();
- int percent=dis.readInt();
- String name=dis.readUTF();
-
- ret[index++]=name+"("+code+")";
- dis.close();
- bais.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- //System.out.println("getFunds "+ret);
- return ret;
- }
-
-
-
- /**
- * code/value(0.1234)/quantity/date(061103)/percent(-312)
- * @return
- */
- public static String getValueDesc(){
- RecordEnumeration re=null;
- StringBuffer sb=new StringBuffer();
- RecordStore rs=null;
- try{
- rs=RecordStore.openRecordStore(DataCenter.RECORD_NAME,true);
- re=rs.enumerateRecords(null,null,false);
- BigInteger allTotal=new BigInteger(0);
- BigInteger total1=new BigInteger(0);
- BigInteger total2=new BigInteger(0);
-
- BigInteger addPerDayPerFund=new BigInteger(0);
- BigInteger addPerDay=new BigInteger(0);
-
- while(re.hasNextElement()){
- int recID=re.nextRecordId();
- ByteArrayInputStream bais=new ByteArrayInputStream(rs.getRecord(recID));
- DataInputStream dis=new DataInputStream(bais);
- String code=dis.readUTF();
- int value=dis.readInt();
- int quantity=dis.readInt();
- String date=dis.readUTF();
- int percent=dis.readInt();
- String name=dis.readUTF();
- sb.append(" \r\n "+name+"\r\n");
- sb.append(" ( "+code+" )"+"\r\n");
- sb.append(" 日期 = "+date+"\r\n");
- sb.append(" 价格 = "+(value10000L)+"."+getFormatInteger(value%10000L,4)+" 元\r\n");
- sb.append(" 数量 = "+(quantity100L)+"."+getFormatInteger(quantity%100L,2)+"\r\n");
-
-
- BigInteger total=BigInteger.mul(new BigInteger(value),new BigInteger(quantity));
- allTotal=BigInteger.add(allTotal,total);
-
- BigInteger tmp=BigInteger.div(total,new BigInteger(10000));
- long n=tmp.getValue();
- sb.append(" 总计 = "+(n100L)+"."+getFormatInteger(n%100L,2)+"\r\n");
-
- BigInteger oldValue=BigInteger.div(BigInteger.mul(new BigInteger(value),new BigInteger(1000000)),new BigInteger(percent+1000000));
-
-
- BigInteger tmp1=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(percent));
- total1=BigInteger.add(total1,tmp1);
- BigInteger tmp2=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(10000));
- total2=BigInteger.add(total2,tmp2);
-
- //System.out.println("code="+code+" tmp1="+tmp1.getValue()+" tmp2="+tmp2.getValue());
-
- addPerDayPerFund=BigInteger.div(tmp1,BigInteger.mul(new BigInteger(10000),new BigInteger(1000000)));
- addPerDay=BigInteger.add(addPerDay,addPerDayPerFund);
- long addPerDayPerFundValue=addPerDayPerFund.getValue();
- if(percent>0){
- sb.append("<00ff0000> 盈亏 = "+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"("+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
- }else if(percent<0){
- percent=0-percent;
- addPerDayPerFundValue=0-addPerDayPerFundValue;
- sb.append("<0000ff00> 盈亏 = -"+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"(-"+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
- }else{
- percent=0-percent;
- sb.append(" 盈亏 = "+addPerDayPerFundValue100L+"."+getFormatInteger(addPerDayPerFundValue%100L,2)+"("+percent10000L+"."+getFormatInteger(percent%10000L,4)+"%)\r\n");
- }
- dis.close();
- bais.close();
- }
-
- long addPerDayValue=addPerDay.getValue();
- sb.append(" \r\n --------\r\n");
- if(total2.getValue()==0){
- sb.append(" 平均盈亏 = 0.00%\r\n");
- sb.append(" 累计盈亏 = "+addPerDayValue100L+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
- }else{
- BigInteger tmp2=BigInteger.mul(total1,new BigInteger(100L));
- tmp2=BigInteger.div(tmp2,total2);
- long result=tmp2.getValue();
- if(result>=0){
- sb.append("<00ff0000> 平均盈亏 = "+(result100)+"."+getFormatInteger(result%100,2)+"%\r\n");
- sb.append("<00ff0000> 累计盈亏 = "+addPerDayValue100+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
- }else{
- result=0-result;
- addPerDayValue=0-addPerDayValue;
- sb.append("<0000ff00> 平均盈亏 = -"+(result100L)+"."+getFormatInteger(result%100L,2)+"%\r\n");
- sb.append("<0000ff00> 累计盈亏 = -"+addPerDayValue100L+"."+getFormatInteger(addPerDayValue%100L,2)+" 元\r\n");
- }
- }
-
- BigInteger tmp=BigInteger.div(allTotal,new BigInteger(10000L));
- long n=tmp.getValue();
- sb.append(" 累计= "+(n100L)+"."+getFormatInteger(n%100L,2)+" 元\r\n");
-
- return sb.toString();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(re!=null){
- re.destroy();
- }
- try{
- if(rs!=null){
- rs.closeRecordStore();
- }
- }catch(Exception e){
- }
- }
- return "";
- }
-
-
- public static String getFormatInteger(long num,int len){
- String tmp=num+"";
- return (tmp.length()<len?(ZEROS[len-tmp.length()]+tmp):tmp.substring(0,len));
- }
-
- }