📄 phonebook.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.util.Stack;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;import javax.microedition.pim.*;import java.io.*;/** * @author i06003 */public class PhoneBook extends MIDlet implements CommandListener,RecordComparator { private final int jmlDataContact = 6; Stack S = new Stack(); String cari; Display display; List ListContact; List DetailContact; Form AddContact; Form SearchContact; Form EditContact; Alert alert = new Alert("Delete Confirmation","Are you sure?",null, AlertType.CONFIRMATION); Alert addConfirmation = new Alert("New Contact Confirmation", "Add to already existing contact?",null,AlertType.CONFIRMATION); RecordStore recStore; Command exitCommand = new Command("Exit",Command.EXIT,1); Command backCommand = new Command("Back",Command.BACK,1); Command newContactCommand = new Command("New Contact",Command.OK, 1); Command OkCommand = new Command("Done", Command.OK, 1); Command yesCommand = new Command("Yes", Command.OK, 3); Command deleteContactCommand = new Command("Delete",Command.OK,4); Command noCommand = new Command("No", Command.BACK,2); Command detailCommand = new Command("Detail",Command.OK,1); Command editCommand = new Command("Edit",Command.OK,2); Command searchCommand = new Command("Search",Command.OK,3); Command viewAllCommand = new Command("View All", Command.OK,0); Command addDetailCommand = new Command("Add Detail", Command.OK,0); TextField[] addTextField = new TextField[10]; TextField[] editTextField = new TextField[10]; TextField searchTextField; String temp; byte[] data = null; Ticker ticker = new Ticker("Phone Book"); public PhoneBook() { try {RecordStore.deleteRecordStore("Phone Book");} catch(Exception e){} ListContact = new List("Record Store", List.IMPLICIT); DetailContact = new List("Detail Contact",List.IMPLICIT); AddContact = new Form("Add Contact"); SearchContact = new Form("Search"); EditContact = new Form("Edit Contact"); addTextField[0] = new TextField("Name", "", 20, TextField.ANY); addTextField[1] = new TextField("Nomer", "", 20, TextField.PHONENUMBER); addTextField[2] = new TextField("Email","",30,TextField.EMAILADDR); addTextField[3] = new TextField("Address", "", 50, TextField.ANY); addTextField[4] = new TextField("Faximile", "", 20, TextField.PHONENUMBER); addTextField[5] = new TextField("Pager","",20,TextField.PHONENUMBER); editTextField[0] = new TextField("Name", "", 20, TextField.ANY); editTextField[1] = new TextField("Nomer", "", 20, TextField.PHONENUMBER); editTextField[2] = new TextField("Email","",30,TextField.EMAILADDR); editTextField[3] = new TextField("Address", "", 50, TextField.ANY); editTextField[4] = new TextField("Faximile", "", 20, TextField.PHONENUMBER); editTextField[5] = new TextField("Pager","",20,TextField.PHONENUMBER); searchTextField = new TextField("Search", "", 100, TextField.ANY); ListContact.setTicker(ticker); for(int i=0;i<jmlDataContact;i++) AddContact.append(addTextField[i]); SearchContact.append(searchTextField); //try {recStore.deleteRecordStore("Phone Book");}catch(Exception e){} initList(); initAdd(); initAlert(); } private void initAdd() { AddContact.addCommand(backCommand); AddContact.addCommand(OkCommand); for(int i=0;i<jmlDataContact;i++) addTextField[i].setString(""); AddContact.setCommandListener(this); } private void initDetail() { DetailContact.addCommand(backCommand); DetailContact.addCommand(newContactCommand); DetailContact.addCommand(editCommand); DetailContact.addCommand(deleteContactCommand); DetailContact.addCommand(searchCommand); DetailContact.setCommandListener(this); DetailContact.deleteAll(); try { recStore = RecordStore.openRecordStore("Phone Book", true); byte[] recBytes; recBytes = recStore.getRecord(Search(ListContact.getString(ListContact.getSelectedIndex()))); ByteArrayInputStream in = new ByteArrayInputStream(recBytes); DataInputStream dIn = new DataInputStream(in); String item = dIn.readUTF(); DetailContact.append(item, null); int panjang = dIn.readInt(),i=0; while(i<panjang) { item = dIn.readUTF(); if(!item.equals("")) DetailContact.append(item, null); i++; } dIn.close(); in.close(); recStore.closeRecordStore(); } catch(Exception e) { System.out.println("Detail gagal"); } } private void initList() { dispRec(); ListContact.addCommand(viewAllCommand); ListContact.addCommand(exitCommand); ListContact.addCommand(newContactCommand); ListContact.addCommand(detailCommand); ListContact.addCommand(editCommand); ListContact.addCommand(searchCommand); ListContact.addCommand(deleteContactCommand); ListContact.setCommandListener(this); } private void initAlert() { alert.addCommand(noCommand); alert.addCommand(yesCommand); alert.setCommandListener(this); } private void initAddAlert() { addConfirmation.addCommand(noCommand); addConfirmation.addCommand(yesCommand); addConfirmation.setCommandListener(this); } private void initEdit() { EditContact.deleteAll(); EditContact.addCommand(backCommand); EditContact.addCommand(addDetailCommand); EditContact.addCommand(OkCommand); EditContact.setCommandListener(this); try { recStore = RecordStore.openRecordStore("Phone Book", true); byte[] recBytes; if(display.equals(ListContact)) recBytes = recStore.getRecord(Search(ListContact.getString(ListContact.getSelectedIndex()))); else recBytes = recStore.getRecord(Search(DetailContact.getString(0))); ByteArrayInputStream in = new ByteArrayInputStream(recBytes); DataInputStream dIn = new DataInputStream(in); String item = dIn.readUTF(); EditContact.append(editTextField[0]); editTextField[0].setString(item); temp = item; int panjang = dIn.readInt(),i=0; while(i<panjang) { item = dIn.readUTF(); if(!item.equals("")) EditContact.append(editTextField[i+1]); editTextField[i+1].setString(item); i++; } dIn.close(); in.close(); recStore.closeRecordStore(); }catch(Exception e) { System.out.println("Buka edit gagal"); } } private void initSearch() { SearchContact.addCommand(yesCommand); SearchContact.addCommand(backCommand); searchTextField.setString(""); SearchContact.setCommandListener(this); } private int Search(String nama) { try { recStore = RecordStore.openRecordStore("Phone Book", false); RecordEnumeration enumerator = recStore.enumerateRecords(null, null, false); while (enumerator.hasNextElement()){ int recId = enumerator.nextRecordId(); byte[] recBytes = recStore.getRecord(recId); ByteArrayInputStream in = new ByteArrayInputStream(recBytes); DataInputStream dIn = new DataInputStream(in); String item = dIn.readUTF(); if(item.equals(nama)) { recStore.closeRecordStore(); return recId; } dIn.close(); in.close(); } recStore.closeRecordStore(); return 0; }catch(Exception e){ System.out.println("search gagal"); return 0; } } public void startApp() { if(display==null) { display = Display.getDisplay(this); display.setCurrent(ListContact); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d){ if (c == exitCommand){ destroyApp(true); notifyDestroyed(); // Exit } if (c == backCommand || c == noCommand) { display.setCurrent((Displayable)S.pop()); if(d == ListContact) dispRec(); } if (c == newContactCommand) { S.push(d); initAdd(); display.setCurrent(AddContact); } if (c == OkCommand || c == yesCommand) { if(d == AddContact) { if(!addTextField[0].getString().equals("") || !addTextField[1].getString().equals("")) { try { String[] strlist = new String[10]; recStore = RecordStore.openRecordStore("Phone Book", true); ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dOut = new DataOutputStream(out); int jmlDetail = 0,i; for(i=0;i<jmlDataContact;i++) { strlist[i] = addTextField[i].getString(); if(i>0) jmlDetail = !strlist[i].equals("")? jmlDetail+1:jmlDetail; } strlist[0] = strlist[0].equals("")? "Unknown" : strlist[0]; if(Search(strlist[0])<=0) { dOut.writeUTF(strlist[0]); dOut.writeInt(jmlDetail); i = 1; while(i<jmlDataContact) { // ini diganti if(!strlist[i].equals("")) dOut.writeUTF(strlist[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -