📄 filtertest.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class FilterTest extends MIDlet implements CommandListener{
Display display;
RecordStore scoreStore;
Command exitCmd;
Command searchCmd;
Alert results;
List condition;
String[] names = {"小明", "阿花", "大牛"};
int[] math = {72, 98, 80};
int[] chinese = {15, 80, 100};
int[] english = {40, 62, 55};
int numOfRecords;
public FilterTest(){
display = Display.getDisplay(this);
condition = new List("选择筛选科目",Choice.MULTIPLE);
condition.append("数学", null);
condition.append("语文", null);
condition.append("英语", null);
results = new Alert("筛选结果");
results.setTimeout(Alert.FOREVER);
exitCmd = new Command("退出",Command.EXIT,1);
searchCmd = new Command("筛选",Command.SCREEN,1);
condition.addCommand(exitCmd);
condition.addCommand(searchCmd);
condition.setCommandListener(this);
try{
scoreStore = RecordStore.openRecordStore("score", true);
numOfRecords = scoreStore.getNumRecords();
}
catch(Exception ex){}
if(numOfRecords == 0){
for(int i=0;i<names.length;i++){
Score score = new Score(names[i], math[i], chinese[i], english[i]);
score.writeToRecord(scoreStore);
}
}
}
public void startApp(){
display.setCurrent(condition);
}
public void search(int choice){
String searchingResult = "";
boolean[] selected = new boolean[3];
int num = condition.getSelectedFlags(selected);
if(num == 0){
searchingResult = "未设定筛选条件";
results.setString(searchingResult);
return;
}
for(int i=0;i<selected.length;i++){
if(selected[i]){
searchingResult += condition.getString(i)+" ";
}
}
searchingResult += "及格的是\n";
MyFilter mf = new MyFilter();
mf.setFilteredItem(choice);
try{
RecordEnumeration renum = scoreStore.enumerateRecords(mf, null, false);
while(renum.hasNextElement()){
Score score = new Score();
score.readFromRecord(scoreStore, renum.nextRecordId());
searchingResult += ""+score.name+"\n";
}
renum.destroy();
}
catch(Exception ex){}
results.setString(searchingResult);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
try{
scoreStore.closeRecordStore();
}
catch(Exception ex){}
}
public void commandAction(Command c, Displayable d){
if(c == exitCmd){
destroyApp(true);
notifyDestroyed();
}
else if(c == searchCmd){
int choice = 1;
boolean[] selected = new boolean[3];
condition.getSelectedFlags(selected);
for(int i=0;i<selected.length;i++){
if(selected[i]){
choice += 2*i;
}
}
search(choice);
display.setCurrent(results);
}
}
}
class Score{
String name;
int math;
int chinese;
int english;
public Score(){
}
public Score(String name, int math, int chinese, int english){
this.name = name;
this.math = math;
this.chinese = chinese;
this.english = english;
}
public void writeToRecord(RecordStore rs){
byte[] data = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeInt(math);
dos.writeInt(chinese);
dos.writeInt(english);
data = baos.toByteArray();
rs.addRecord(data, 0, data.length);
baos.close();
dos.close();
}
catch(Exception ex){
}
}
public void readFromRecord(RecordStore rs, int RecordID){
try{
byte[] data = rs.getRecord(RecordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
name = new String(dis.readUTF());
math = dis.readInt();
chinese = dis.readInt();
english = dis.readInt();
bais.close();
dis.close();
}
catch(Exception ex){}
}
}
class MyFilter implements RecordFilter{
static final int MATH = 1;
static final int CHINESE = 2;
static final int ENGLISH = 4;
int filterItem;
public void setFilteredItem(int item){
filterItem = item;
}
public boolean matches(byte[] candidate){
int math = 0, chinese = 0, english = 0;
boolean matched = false;
try{
ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
DataInputStream dis = new DataInputStream(bais);
String name = dis.readUTF();
math = dis.readInt();
chinese = dis.readInt();
english = dis.readInt();
dis.readInt();
bais.close();
dis.close();
}
catch(Exception ex){}
if((filterItem & 0x01) == MATH)
matched = (math>=60)?true:false;
if((filterItem & 0x02) == CHINESE)
matched = (chinese>=60)?true:false;
if((filterItem & 0x04) == ENGLISH)
matched = (english>=60)?true:false;
return matched;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -