?? account.java
字號:
/*
* Account.java
* 一筆賬目的定義
* Created on 2007年3月11日, 下午5:27
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package net.bccn.account.model;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.rms.RecordComparator;
import net.bccn.account.util.*;
/**
*
* @author hadeslee
*/
public class Account implements Record{
private String type,detail;
private float money;
private Date date=new Date();//標(biāo)準(zhǔn)日期,結(jié)束日期
private Date from=new Date();//起始日期
private String remark;
private int id;
private int mask;
public static final int YEAR=0x00000001;
public static final int MONTH=0x00000001<<1;
public static final int DAY=0x00000001<<2;
private boolean isPhase=false;//是否按階段查
public static final String NAME="Account";//名字
/** Creates a new instance of Account */
public Account() {
remark="";
}
//復(fù)制構(gòu)造函數(shù)
public Account(Account ac){
this.type=ac.type;
this.detail=ac.detail;
this.money=ac.money;
this.date.setTime(ac.date.getTime());
this.remark=ac.remark;
this.id=ac.id;
}
public void fromBytes(byte[] data)throws IOException {
ByteArrayInputStream bin=new ByteArrayInputStream(data);
DataInputStream din=new DataInputStream(bin);
String name=din.readUTF();
if(!name.equals(Account.NAME)){
return;
}
type=din.readUTF();
detail=din.readUTF();
money=din.readFloat();
date.setTime(din.readLong());
remark=din.readUTF();
}
public static Account getAccount(byte[] data)throws IOException{
Account ac=new Account();
ByteArrayInputStream bin=new ByteArrayInputStream(data);
DataInputStream din=new DataInputStream(bin);
String name=din.readUTF();
if(!name.equals(Account.NAME)){
ac.setType("不可能匹配的");
return ac;
}
ac.type=din.readUTF();
ac.detail=din.readUTF();
ac.money=din.readFloat();
ac.date.setTime(din.readLong());
ac.remark=din.readUTF();
din.close();
return ac;
}
public Record getEmptyRecord(){
return new Account();
}
//設(shè)置掩碼
public void setMask(int mask){
//如果沒有設(shè)置日期,那么比較日期就沒有意義了,所以直接返回
if(date==null)
return;
this.mask=mask;
isPhase=false;//如果按掩碼查詢,則區(qū)間為false
}
//設(shè)置區(qū)間查詢
public void setPhase(){
isPhase=true;
mask=0;//如果按區(qū)間來查詢,則掩碼沒有意義,所以設(shè)其為0
}
//設(shè)置起始日期
public void setFrom(Date date){
from.setTime(date.getTime());
}
public byte[] toBytes()throws IOException {
ByteArrayOutputStream bout=new ByteArrayOutputStream();
DataOutputStream dout=new DataOutputStream(bout);
dout.writeUTF(Account.NAME);
dout.writeUTF(type);
dout.writeUTF(detail);
dout.writeFloat(money);
dout.writeLong(date.getTime());
dout.writeUTF(remark);
dout.flush();
return bout.toByteArray();
}
public int getID() {
return id;
}
public void setID(int id){
this.id=id;
}
public int getMask(){
return mask;
}
public boolean matches(byte[] b) {
try{
Account demo=Account.getAccount(b);
if(demo.getType().equals("不可能匹配的")){
return false;
}
Calendar demoCal=Calendar.getInstance();
Calendar me=Calendar.getInstance();
me.setTime(date);
demoCal.setTime(demo.getDate());
//如果掩碼不等于0,則說明要比較時間了
if(mask!=0){
Calendar temp=Calendar.getInstance();
temp.setTime(demo.date);
if((mask&YEAR)!=0){
if(me.get(Calendar.YEAR)!=demoCal.get(Calendar.YEAR)){
return false;
}
}
if((mask&MONTH)!=0){
if(me.get(Calendar.MONTH)!=demoCal.get(Calendar.MONTH)){
return false;
}
}
if((mask&DAY)!=0){
if(me.get(Calendar.DATE)!=demoCal.get(Calendar.DATE)){
return false;
}
}
}
//如果要查詢階段,則另一種處理了
else if(isPhase){
long demoLong=(demo.getDate().getTime()+Util.OFF_SET)/86400000;
long fromLong=(from.getTime()+Util.OFF_SET)/86400000;
long toLong=(date.getTime()+Util.OFF_SET)/86400000;
if(demoLong>=fromLong&&demoLong<=toLong){
} else
return false;
}
return ((this.type!=null?this.type.equals(demo.type):true)&&
(this.detail!=null?this.detail.equals(demo.detail):true)&&
(this.money!=0?this.money>=demo.money:true)&&
(this.remark.equals("")?true:demo.remark.startsWith(this.remark)));
} catch(Exception exe){
return false;
}
}
public int compare(byte[] b, byte[] b0) {
try{
Account temp1=(Account)Account.getAccount(b);
Account temp2=(Account)Account.getAccount(b0);
if((temp1.date.getTime()+Util.OFF_SET)/86400000==
(temp2.date.getTime()+Util.OFF_SET)/86400000){
return RecordComparator.EQUIVALENT;
}
return ((temp1.date.getTime()+Util.OFF_SET)/86400000<
(temp2.date.getTime()+Util.OFF_SET)/86400000?
RecordComparator.PRECEDES:
RecordComparator.FOLLOWS);
} catch(Exception exe){
return RecordComparator.EQUIVALENT;
}
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
private String getDateString(){
Calendar cal=Calendar.getInstance();
cal.setTime(date);
return ""+cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+
"-"+cal.get(Calendar.DATE);
}
public String toString(){
return "類型:"+type+",金額:"+money+",日期:"+getDateString();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -