⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addressbookmidlet.java

📁 手机上的addressbook
💻 JAVA
字号:
package chapter4.addressbook;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;

public class AddressBookMIDlet extends MIDlet implements CommandListener {

	private Display display=null;   
	 //命令变量
	 Command quit=new Command("关闭",Command.EXIT,3);
	 Command search=new Command("查询",Command.OK,2);
	 Command delete=new Command("删除",Command.OK,2);
	 Command addnow=new Command("增加",Command.OK,2);
	 Command edit=new Command("修改",Command.OK,2);
	 Command mainmenu=new Command("主菜单",Command.SCREEN,2);
	  
	 Command paixu=new Command("排序",Command.OK,2);
	 Command filt=new Command("过滤",Command.OK,2);

	 List menuList=null;                  //界面组件变量
	 Form ui_form=null;
	 TextField name=null;
	 TextField phone=null;
	 TextField nameForEdit=null;
	 
	 TextField filterphone=null;       //过滤电话号码
	 
	 RecordStore recordStore=null;   //记录集声明
	 
	 
	 public AddressBookMIDlet() {
	  //记录集初始化
	  try{
	   recordStore=RecordStore.openRecordStore("addresses", true);
	  }catch(RecordStoreException rse){
	   rse.printStackTrace();
	  }
	 }
	 
	 // 设置菜单选项
	  protected void startApp() throws MIDletStateChangeException {
	  	
	  display=Display.getDisplay(this);

	  menuList=new List("电话薄...",List.IMPLICIT);
	  menuList.append("1.查询", null);
	  menuList.append("2.增加", null);
	  menuList.append("3.删除", null);
	  menuList.append("4.修改", null);
	  menuList.append("5.关闭", null);
	  
	  menuList.append("6.排序", null);
	  menuList.append("7.过滤", null);

	  menuList.setCommandListener(this);
	  display.setCurrent(menuList);
	 }
	 //命令处理 
	  public void commandAction(Command c, Displayable d) {
	  	if(d==menuList){
	  	 switch(menuList.getSelectedIndex()){
	       case 0: searchScreen();break;
	       case 1: addScreen();break;
	       case 2: deleteScreen();break;
	       case 3: editScreen();break;
	       case 4: try {
	              destroyApp(true);
	     } catch (MIDletStateChangeException e) {
	                   e.printStackTrace();
	     }break;
	       case 5: paixu_search();break;
	       case 6: filterScreen();break;
	   }
	  }
	  else if(c==quit){//按下关闭按钮,触发相应事件处理
	   try{
	    close();//关闭记录集
	   }catch(RecordStoreException rse){
	    rse.printStackTrace();//销毁应用程序,触发相应事件处理
	   }
	   try {
	    destroyApp(true);
	   } catch (MIDletStateChangeException e) {
	    e.printStackTrace();
	   }
	  }
	  else if(c==search){
	   String temp_search=name.getString();//获取要查询的姓名
	   address_search(temp_search);//调用查询方法
	  }
	  else if(c==filt){
	   String temp_search=filterphone.getString();//获取要查询的号码
	   filteraddress_search(temp_search);//调用查询方法
	  }
	  else if (c==edit){
	   String temp_nameForEdit=nameForEdit.getString();
	   String temp_name=name.getString();
	   String temp_phone=phone.getString();//调用修改方法
	   
	   address_edit(temp_nameForEdit,temp_name,temp_phone);
	  }
	  else if(c==mainmenu){
	   try {
	    startApp();
	   } catch (MIDletStateChangeException e) {
	    
	    e.printStackTrace();
	   }//重置
	  }
	  else if(c==delete){
	   String temp_delete=name.getString();//获得要删除的姓名
	   address_del(temp_delete);//调用删除方法
	  }
	  else if(c==addnow){
	   String temp_name=name.getString();//获得增加的姓名
	   String temp_phone=phone.getString();//获得增加的电话号码
	   
	   address_add(temp_name,temp_phone);//调用增加方法
	  }
	  
	 }
	 //重置menu
	  protected void pauseApp() {
	  menuList=null;
	 }
	 //关闭应用程序
	  protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	  menuList=null;
	  notifyDestroyed();
	 }
	//查询用户界面
	 void searchScreen(){
	  ui_form=new Form("电话薄查询");
	  name=new TextField("查询姓名","",50,0);
	  ui_form.append(name);
	  
	  ui_form.addCommand(search);
	  ui_form.addCommand(quit);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	 //增加记录的用户界面。。。
	 void addScreen(){
	  ui_form=new Form("增加");
	  
	  name=new TextField("姓名","",50,0);
	  ui_form.append(name);
	  
	  phone=new TextField("电话号码","",50,0);
	  ui_form.append(phone);
	  
	  ui_form.addCommand(addnow);
	  ui_form.addCommand(quit);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	  
	 }
	 //修改记录的用户界面
	 void editScreen(){
	  ui_form=new Form("修改");
	  
	  nameForEdit= new TextField("需要修改的姓名","",50,0);
	  ui_form.append(nameForEdit);
	  
	  name=new TextField("姓名","",50,0);
	  ui_form.append(name);
	  
	  phone=new TextField("电话号码","",50,0);
	  ui_form.append(phone);
	  
	  ui_form.addCommand(edit);
	  ui_form.addCommand(quit);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	 //删除一条记录的用户界面
	 void deleteScreen(){
	  ui_form=new Form("删除");
	  name=new TextField("姓名","",50,0);
	  ui_form.append(name);
	  
	  ui_form.addCommand(delete);
	  ui_form.addCommand(quit);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	 //过滤用户界面
	 void filterScreen(){
	  ui_form=new Form("电话薄过滤查询");
	  filterphone=new TextField("查找号码大于","",50,TextField.NUMERIC);
	  ui_form.append(filterphone);
	  
	  ui_form.addCommand(filt);
	  ui_form.addCommand(quit);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	//查询方法
	 void address_search(String address){
	  String temp="";
	  String phone_number;
	  String person_name;
	  String check_name;
	  int size=address.length();
	  ui_form=new Form("查询结果");
	  try{
	   RecordEnumeration re=
	   recordStore.enumerateRecords(null, null, false);
	   
	   while(re.hasNextElement()){
	    String name1=new String(re.nextRecord());
	    try{
	     person_name=name1.substring(0,name1.indexOf("?"));
	    }catch(Exception ef){
	     person_name="请检查姓名是否正确";
	     ef.printStackTrace();
	    }
	    if (person_name.length()>=size){
	     check_name=person_name.substring(0,size);
	     if(check_name.equals(address)){
	      try{
	       phone_number=name1.substring(name1.indexOf("?")+1);
	      }catch(Exception e){
	       phone_number="";
	      }
	      temp=temp+"\n姓名.."+person_name+"\n电话.."+phone_number;
	     }
	    }
	   }
	   if(temp.equals("")){
	    temp="没有找到。。。。。";
	   }
	   ui_form.append(temp);
	   
	   ui_form.addCommand(quit);
	   ui_form.addCommand(mainmenu);
	   
	   ui_form.setCommandListener(this);
	   display.setCurrent(ui_form);
	  }catch(RecordStoreNotOpenException rsnoe){
	   rsnoe.printStackTrace();
	  }catch(InvalidRecordIDException irid){
	   irid.printStackTrace();
	  }catch(RecordStoreException rse){
	   rse.printStackTrace();
	  }
	 }
	 //删除方法
	 void address_del(String address){
	  String temp="";
	  String phone_number;
	  String person_name;
	  int id;
	  int del_id=0;
	  try{
	   RecordEnumeration re=recordStore.enumerateRecords(null, null, false);
	   ui_form=new Form("删除结果");
	   while(re.hasNextElement()){
	    id=re.nextRecordId();
	    String name1=new String(recordStore.getRecord(id));
	    try{
	     person_name=name1.substring(0,name1.indexOf("?"));
	    }catch(Exception ef){
	     person_name="请检查姓名是否正确";
	    }
	    if(person_name.equals(address)){
	     del_id=id;
	    }
	   }
	   if(del_id !=0){
	    recordStore.deleteRecord(del_id);
	    temp="成功删除一条记录";
	   }else{
	    temp="所指定的记录不在电话薄内";
	   }
	   
	  }catch(Exception e){
	   
	  }
	  ui_form.append(temp);
	  
	  ui_form.addCommand(quit);
	  ui_form.addCommand(mainmenu);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	 //增加方法
	 void address_add(String address,String phone){
	  String data=address+"?"+phone;
	  System.out.println(data);
	  try{
	   byte[] b=data.getBytes();
	   recordStore.addRecord(b, 0, b.length);
	   
	   ui_form =new Form("添加成功");
	   ui_form.append("成功添加一条记录");
	   
	   ui_form.addCommand(quit);
	   ui_form.addCommand(mainmenu);
	   
	   ui_form.setCommandListener(this);
	   
	   display.setCurrent(ui_form);
	  }catch(RecordStoreException rse){
	   rse.printStackTrace();
	  }
	 }
	 //修改方法
	 void address_edit(String addressForEdit,String address,String phone){
	 
	  String person_name;
	  String temp="要修改的记录不在电话薄内";
	  try{
	      ui_form=new Form("修改");

	   RecordEnumeration re=recordStore.enumerateRecords(null, null, false);
	   while(re.hasNextElement()){
	    int id=re.nextRecordId();//获得记录ID 
	    String record=new String(recordStore.getRecord(id));//获得记录
	    //String name1=new String(re.nextRecord());

	   person_name=record.substring(0,record.indexOf("?"));//取得姓名
	   
	   if(person_name.equals(addressForEdit)){//如果是要修改的姓名
	     String data=address+"?"+phone;     //准备好新的数据
	     byte[] b_data=data.getBytes();
	     recordStore.setRecord(id, b_data, 0, b_data.length);//修改记录
	     temp="成功修改记录";
	}
	}
	}
	  catch(Exception e){
	   e.printStackTrace();
	  }
	  ui_form.append(temp);
	  
	  ui_form.addCommand(quit);
	  ui_form.addCommand(mainmenu);
	  
	  ui_form.setCommandListener(this);
	  
	  display.setCurrent(ui_form);
	 }
	 
	 //排序方法
	 void paixu_search(){
	  String temp="";
	  String person_phone;
	  String person_name;

	  ui_form=new Form("排序查询结果");
	  try{
	   Comparator cmp=new Comparator();
	   RecordEnumeration re=
	   recordStore.enumerateRecords(null, cmp, false);
	   
	   while(re.hasNextElement()){
	      String name1=new String(re.nextRecord());
	    
	      person_name=name1.substring(0,name1.indexOf("?"));

	      person_phone=name1.substring(name1.indexOf("?")+1);

	      temp=temp+"\n姓名.."+person_name+"\n电话.."+person_phone;
	   }
	   if(temp.equals("")){
	    temp="没有找到。。。。。";
	   }
	   ui_form.append(temp);
	   
	   ui_form.addCommand(quit);
	   ui_form.addCommand(mainmenu);
	   
	   ui_form.setCommandListener(this);
	   display.setCurrent(ui_form);
	  }catch(RecordStoreNotOpenException rsnoe){
	   rsnoe.printStackTrace();
	  }catch(InvalidRecordIDException irid){
	   irid.printStackTrace();
	  }catch(RecordStoreException rse){
	   rse.printStackTrace();
	  }
	 }
	 //过滤查询 
	  void filteraddress_search(String filphone){
	  String temp="";
	  String person_phone;
	  String person_name;

	  
	  ui_form=new Form("查询结果");
	  try{
	  // Comparator cmp=new Comparator();

	   MyFilter rf=new MyFilter(filphone);

	   RecordEnumeration re=recordStore.enumerateRecords(rf, null, false);
	   
	   while(re.hasNextElement()){
	      String name1=new String(re.nextRecord());
	    
	      person_name=name1.substring(0,name1.indexOf("?"));

	      person_phone=name1.substring(name1.indexOf("?")+1);

	      temp=temp+"\n姓名.."+person_name+"\n电话.."+person_phone;
	   }
	   if(temp.equals("")){
	    temp="没有找到。。。。。";
	   }
	   ui_form.append(temp);
	   
	   ui_form.addCommand(quit);
	   ui_form.addCommand(mainmenu);
	   
	   ui_form.setCommandListener(this);
	   display.setCurrent(ui_form);
	  }catch(RecordStoreNotOpenException rsnoe){
	   rsnoe.printStackTrace();
	  }catch(InvalidRecordIDException irid){
	   irid.printStackTrace();
	  }catch(RecordStoreException rse){
	   rse.printStackTrace();
	  }
	 }
	 //关闭存储记录
	 public void close()throws RecordStoreNotOpenException,RecordStoreException{
	  recordStore.closeRecordStore();
	 }
	}

	class Comparator implements RecordComparator
	{
	  public int compare(byte[] record1, byte[] record2)
	  {
	    String str1 = new String(record1);
	    String str2= new String(record2);
	    
	   String phone1=str1.substring(str1.indexOf("?")+1);
	   String phone2=str2.substring(str2.indexOf("?")+1);

	    int comparison= phone1.compareTo(phone2);
	    if (comparison == 0)
	      return RecordComparator.EQUIVALENT;
	    else if (comparison < 0)
	      return RecordComparator.PRECEDES;
	    else
	      return RecordComparator.FOLLOWS;
	  }
	} 

	 
	class MyFilter implements RecordFilter 
	    { 
	      private  String filtPhone=null;
	      public MyFilter(String filtPhone)
	     {
	       // 过滤比较的号码
	         this.filtPhone = filtPhone;
	     }

	        public boolean matches(byte[] rec) 
	        {  
	            String record=new String(rec);
	            String  person_phone=record.substring(record.indexOf("?")+1);
	            
	            double d =Double.parseDouble(person_phone); 
	            double s=Double.parseDouble(filtPhone);
	            if(d>s)
	            { 
	                return true;
	            }
	            else{ 
	                return false;
	            }
	        }
	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -