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

📄 phonebook.java

📁 Phone Book with RecordStore
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                i++;                            }                            byte[] bytes = out.toByteArray();                            recStore.addRecord(bytes, 0, bytes.length);                            out.close();                            dOut.close();                            S.pop();                            display.setCurrent(ListContact);                        }                        else {                            S.push(d);                            initAddAlert();                            display.setCurrent(addConfirmation);                        }                        recStore.closeRecordStore();                    } catch (Exception e) {                        System.out.println("add contact gagal");                    }                    dispRec();                }            }            else if(d == EditContact) {                S.pop();                display.setCurrent(ListContact);                if(!editTextField[0].getString().equals("") || !editTextField[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] = editTextField[i].getString();                            if(i>0) jmlDetail = !strlist[i].equals("")? jmlDetail+1:jmlDetail;                        }                        strlist[0] = strlist[0].equals("")? "Unknown" : strlist[0];                                                dOut.writeUTF(strlist[0]);                        dOut.writeInt(jmlDetail);                        i = 1;                        while(i<jmlDataContact) {                            if(!strlist[i].equals("")) dOut.writeUTF(strlist[i]);                            i++;                        }                        byte[] bytes = out.toByteArray();                        recStore.setRecord(Search(temp),bytes, 0, bytes.length);                        out.close();                        dOut.close();                                                recStore.closeRecordStore();                    } catch (Exception e) {                        System.out.println("edit contact gagal"+e);                    }                    dispRec();                }            }            else if(d == addConfirmation) {                S.pop();                S.pop();                display.setCurrent(ListContact);                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];                        dOut.writeUTF(strlist[0]);                        dOut.writeInt(jmlDetail);                        i = 1;                        while(i<=jmlDetail) {                            dOut.writeUTF(strlist[i]);                            i++;                        }                                                byte[] bytes = out.toByteArray();                        recStore.setRecord(Search(strlist[0]),bytes, 0, bytes.length);                        out.close();                        dOut.close();                        recStore.closeRecordStore();                    } catch (Exception e) {                        System.out.println("ganti contact gagal"+e);                    }                    dispRec();                }            }            else if(d == alert) {                try {                    recStore = RecordStore.openRecordStore("Phone Book", true);                    recStore.deleteRecord(Search(cari));                    recStore.closeRecordStore();                }catch(Exception e){                    System.out.println("delete gagal");                }                S.pop();                if(!S.empty()) S.pop();                display.setCurrent(ListContact);                dispRec();            }            else if(d == SearchContact) {                ListContact.deleteAll();                try{                    recStore = RecordStore.openRecordStore("Phone Book", true);                    byte[] recBytes;                    RecordEnumeration enumerator = recStore.enumerateRecords(null, this, false);                    while (enumerator.hasNextElement()){                        recBytes = enumerator.nextRecord();                        ByteArrayInputStream in = new ByteArrayInputStream(recBytes);                        DataInputStream dIn = new DataInputStream(in);                        String item = dIn.readUTF();                        if(item.startsWith(searchTextField.getString())) ListContact.append(item, null);                        dIn.close();                        in.close();                    }                                        recStore.closeRecordStore();                }catch (Exception e){                    System.out.println("Menampilkan search gagal");                }                S.pop();                display.setCurrent(ListContact);            }        }        if (c == viewAllCommand) {            dispRec();        }        if (c == deleteContactCommand) {            try {                if(d == ListContact) cari = ListContact.getString(ListContact.getSelectedIndex());                else if(d == DetailContact) cari = DetailContact.getString(0);                S.push(d);                initAlert();                display.setCurrent(alert);            }catch(Exception e) {                System.out.println("Delete gagal");            }        }        if (c == detailCommand) {            if(ListContact.getSelectedIndex()>=0) {                S.push(d);                initDetail();                display.setCurrent(DetailContact);            }            else System.out.println("detail gagal di sini");        }        if (c == searchCommand) {            S.push(d);            initSearch();            display.setCurrent(SearchContact);        }        if (c == editCommand) {            S.push(d);            initEdit();            display.setCurrent(EditContact);        }        if (c == addDetailCommand) {            EditContact.deleteAll();            EditContact.addCommand(backCommand);            EditContact.addCommand(addDetailCommand);            EditContact.addCommand(OkCommand);            EditContact.setCommandListener(this);            for(int i=0;i<jmlDataContact;i++) editTextField[i].setString("");            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();                EditContact.append(editTextField[0]);                editTextField[0].setString(item);                temp = item;                int panjang = dIn.readInt(),i=0;                while(i<panjang) {                    item = dIn.readUTF();                    EditContact.append(editTextField[i+1]);                    editTextField[i+1].setString(item);                    i++;                }                while(i<jmlDataContact-1) {                    EditContact.append(editTextField[i+1]);                    i++;                }                                dIn.close();                in.close();                recStore.closeRecordStore();            }catch(Exception e) {                System.out.println("Add detail gagal"+e);            }        }    }           public void dispRec() {        ListContact.deleteAll();        try{            recStore = RecordStore.openRecordStore("Phone Book", true);            byte[] recBytes;            RecordEnumeration enumerator = recStore.enumerateRecords(null, this, false);            while (enumerator.hasNextElement()){                recBytes = enumerator.nextRecord();                ByteArrayInputStream in = new ByteArrayInputStream(recBytes);                DataInputStream dIn = new DataInputStream(in);                String item = dIn.readUTF();                ListContact.append(item, null);                dIn.close();                in.close();            }            recStore.closeRecordStore();        }catch (Exception e){            System.out.println("menampilkan contact gagal");        }    }        public int compare(byte[] rec1, byte[] rec2){        try {            String record1;            String record2;            //Sorting Ascending            ByteArrayInputStream in1 = new ByteArrayInputStream(rec1);            DataInputStream dIn1 = new DataInputStream(in1);            ByteArrayInputStream in2 = new ByteArrayInputStream(rec2);            DataInputStream dIn2 = new DataInputStream(in2);            record1 = dIn1.readUTF().toUpperCase();            record2 = dIn2.readUTF().toUpperCase();            dIn1.close(); in1.close();            dIn2.close(); in2.close();            if (record1.compareTo(record2) < 0){                return(PRECEDES);            } else {                if (record1.compareTo(record2) > 0){                    return(FOLLOWS);                } else {                    return(EQUIVALENT);                }            }        }catch(Exception e) {            System.out.println("sortin gagal");            return -2;        }    }}

⌨️ 快捷键说明

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