📄 filterfieldsmidlet.java
字号:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class FilterFieldsMIDlet extends MIDlet
implements CommandListener {
private Command exitCommand;
private Display display;
private String[] names = {"黄聪明","陈小筱","黄镇樟","郑雅文"};
private int[] chineseScore = {74,98,89,76};
private int[] englishScore = {67,89,89,78};
private int[] mathScore = {80,76,80,78};
public FilterFieldsMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("离开", Command.EXIT, 1);
}
public void startApp() {
TextBox aTextBox = new TextBox("主画面",null,256,TextField.ANY);
RecordStore rs = null;
byte[] nameEmail=null;
boolean existingOrNot = false;
boolean OK = true;
existingOrNot = existing("aRS3");
if(existingOrNot){
try{
rs = RecordStore.openRecordStore("aRS3",false);
}
catch(Exception e){
OK = false;
}
finally{
if(OK){
aTextBox.setString("aRS3已存在并且已打开");
}
else{
aTextBox.setString("aRS3已存在但无法打开");
}
}
}
else{
try{
rs = RecordStore.openRecordStore("aRS3",true);
}
catch(Exception e){
OK = false;
}
finally{
if(OK){
aTextBox.setString("aRS3虽未存在,但已创建并完成打开的操作!");
}
else{
aTextBox.setString("aRS3虽未存在,但无法创建,打开文件失败!");
}
}
}
Student aStudent = null;
if(OK)
try{
for(int i=0;i<names.length;i++){
aStudent = new Student();
aStudent.write(names[i],
chineseScore[i],
englishScore[i],
mathScore[i]
);
byte[] data = aStudent.changeToByteArray();
int recordID = aStudent.getRecordID();
if(recordID != -1){
rs.setRecord(recordID,data,0,data.length);
}
else{
recordID = rs.addRecord(data,0,data.length);
aStudent.setRecordID(recordID);
}
aStudent = null;
}
aTextBox.setString("已完成新增记录的操作!");
}
catch(Exception e){
aTextBox.setString("新增记录的操作失败!");
}
String result="";
aStudent = new Student();
if(OK)
try{
byte[] data;
int number = 0;
RecordFilter rf = new averageFilter();
RecordEnumeration re = rs.enumerateRecords(rf,null,false);
while(re.hasNextElement()){
int recordID = re.nextRecordId();
data = rs.getRecord(recordID);
aStudent.changeFromByteArray(data);
result += "第" + recordID + "笔\n" +
"姓名:" + aStudent.getName() + "\n" +
"语文:" + aStudent.getChineseScore() + "\n" +
"英语:" + aStudent.getEnglishScore() + "\n" +
"数学:" + aStudent.getMathScore() + "\n";
number++;
}
result += "共有" + number + "笔记录:\n";
aTextBox.setString(result);
}
catch(Exception e){
aTextBox.setString("获取记录的操作失败!" );
try{
rs.closeRecordStore();
System.out.println("1.Closed.");
RecordStore.deleteRecordStore("aRS3");
System.out.println("delete OK");
}
catch(Exception x){
}
}
finally{
try{
if(rs != null) rs.closeRecordStore();
rs.deleteRecordStore("aRS3");
}
catch(Exception e){
}
}
aTextBox.setString(result);
aTextBox.addCommand(exitCommand);
aTextBox.setCommandListener(this);
display.setCurrent(aTextBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public boolean existing(String recordStoreName) {
boolean existingOrNot = false;
RecordStore rs = null;
if(recordStoreName.length() > 32) return false;
try{
rs = RecordStore.openRecordStore(recordStoreName,false);
}
catch(RecordStoreNotFoundException e){
existingOrNot = false;
}
catch(Exception e){
}
finally{
try{
rs.closeRecordStore();
}
catch(Exception e){
}
}
return existingOrNot;
}
public void commandAction(Command c, Displayable s) {
destroyApp(false);
notifyDestroyed();
}
}
class averageFilter implements RecordFilter{
public boolean matches(byte[] candidate){
DataInputStream student = new DataInputStream(new ByteArrayInputStream(candidate));
int average = 0;
try{
String dummy = student.readUTF();
average = (student.readInt() +
student.readInt() +
student.readInt())/3;
}
catch(Exception e){
}
if(average >= 80 ) return true;
else return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -