Rabu, 18 Juli 2012

PROGRAM APLIKASI PEMINJAM BUKU (J2ME dengan RMS)

 

Program aplikasi yang penulis buat ini merupakan program aplikasi yang menggunakan J2ME (Java2 Micro Edition) dengan RMS (Record Management System). Pada dasarnya fungsi dari RMS ini sama dengan database, RMS itu sendiri merupakan salah satu fasilitas yang berguna untuk menyimpan semua informasi kedalam memori non-volatile (memori tetap). Jadi, semua data yang telah kita simpan tidak akan hilang sampai kita sendiri yang menghapusnya.
Baik, langsung saja ke topik inti kita. Pertama-tama bukalah projek baru pada NetBeans. Apabila anda belum memilikinya, bisa  download disini
Cara membuat projek baru :
1. Klik "new project" atau Ctrl+Shift+N
2. Pada "Choosen project" pilih "Mobility" lalu klik "Next"
3. Pada "Projek Name" tulislah "JavaMobilePerbaungan"
4. Hilangkan centang pada "Create Hello MIDlet" lalu klik finish

Cara membuat file baru :
1. Klik "new file" atau Ctrl + N
2. Pada "Categories" pilih MIDP lalu klik "Next"
3. Pada MIDlet name tulislah dengan"PeminjamBuku" lalu klik "Finish"

Selanjutnya copykan semua listing dibawah ini kedalam file PeminjamBuku.java tersebut. Untuk menampilkan gambar pada program aplikasi ini maka copykan gambar diatas lalu  tempatkan pada  C:\Users\"Sesuaikan dengan Nama Komputer Anda"\Documents\NetBeansProjects\JavaMobilePerbaungan\src. Setelah selesai maka gambar secara otomatis akan tampil pada saat anda akan menjalankan program ini.

Listing Program "PeminjamBuku.java"

    import javax.microedition.rms.*; 
    import javax.microedition.midlet.*; 
    import javax.microedition.lcdui.*; 
    import java.io.*; 
     
    public class PeminjamBuku extends MIDlet implements CommandListener { 
     
        Form formTampil, formTambahData, formLihat, formEdit;
        Alert alert; 
        List list; 
        RecordStore recordstore = null; 
        RecordEnumeration recordEnumeration = null; 
        Display display;
        Ticker t;
     
        
        Command cmdLihatAlumni = new Command("Data", Command.SCREEN, 1);
        Command cmdTambahData = new Command("Tambah Data", Command.SCREEN, 1); 
        Command cmdEdit = new Command("Ubah Data", Command.SCREEN, 1); 
        Command cmdDelete = new Command("Hapus Data", Command.SCREEN, 1); 
        Command cmdExit = new Command("Keluar", Command.EXIT, 1);
        
        Command cmdSimpan = new Command("Simpan", Command.SCREEN, 1); 
        Command cmdUpdate = new Command("Ubah", Command.SCREEN, 1); 
        Command cmdCancel = new Command("Kembali", Command.BACK, 1); 
        Command cmdBack = new Command("Kembali", Command.BACK, 1); 
     
        TextField txtNama = new TextField("Nama Peminjam\t:", "", 30, TextField.ANY); 
        TextField txtNpm = new TextField("No Identitas\t:", "", 30, TextField.PHONENUMBER); 
        TextField txtJudul = new TextField("Judul Buku\t:", "", 100, TextField.ANY); 
        TextField txtTglPinjam = new TextField("Tanggal Peminjaman\t:", "", 30, TextField.ANY);
         TextField txtTglPulang = new TextField("Tanggal Pengembalian\t:", "", 30, TextField.ANY);
        TextField txtRecNama, txtRecNpm, txtRecJudul, txtRecPinjam,txtRecPulang; 
        
     
        int recID, currentTask; 
        String keyword; 
     
        public PeminjamBuku() {
            t=new Ticker("Program Daftar Peminjam Buku di Perpustakaan");
            formTampil = new Form("             Daftar Para Peminjam Buku"); 
            try { 
                Image img = Image.createImage("/java mobile perbaungan.jpg"); 
                ImageItem image = new ImageItem("               SELAMAT DATANG", img, Item.LAYOUT_CENTER, "");
                formTampil.append(image);
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
          
           
            formTampil.addCommand(cmdLihatAlumni);
            formTampil.addCommand(cmdExit); 
            formTampil.setCommandListener(this); 
           
           
            formTambahData = new Form("Tambah Data Peminjam"); 
            formTambahData.addCommand(cmdBack); 
            formTambahData.addCommand(cmdSimpan); 
            formTambahData.append(txtNama); 
            formTambahData.append(txtNpm); 
            formTambahData.append(txtJudul); 
            formTambahData.append(txtTglPinjam);
            formTambahData.append(txtTglPulang);
            formTambahData.setCommandListener(this); 
     
           
        } 
          public class RmsBanding implements RecordComparator { 
     
        private byte[] comparatorInputData = new byte[300]; 
        private ByteArrayInputStream comparatorInputStream = null; 
        private DataInputStream comparatorInputDataType = null; 
     
        public int compare(byte[] record1, byte[] record2) { 
            int record1int, record2int; 
            try { 
                comparatorInputStream = new ByteArrayInputStream(record1); 
                comparatorInputDataType = new DataInputStream(comparatorInputStream); 
                String data1 = comparatorInputDataType.readUTF(); 
                comparatorInputStream = new ByteArrayInputStream(record2); 
                comparatorInputDataType = new DataInputStream(comparatorInputStream); 
                String data2 = comparatorInputDataType.readUTF(); 
                int comparison = data1.compareTo(data2); 
                if (comparison == 0) { 
                    return RecordComparator.EQUIVALENT; 
                } else if (comparison < 0) { 
                    return RecordComparator.PRECEDES; 
                } else { 
                    return RecordComparator.FOLLOWS; 
                } 
            } catch (Exception error) { 
                return RecordComparator.EQUIVALENT; 
            } 
        } 
     
        public void compareClose() { 
            try { 
                if (comparatorInputStream != null) { 
                    comparatorInputStream.close(); 
                } 
                if (comparatorInputDataType != null) { 
                    comparatorInputDataType.close(); 
                } 
            } catch (Exception error) { 
            } 
        } 
    } 

     
        public void startApp() { 
            if (display == null) { 
                display = Display.getDisplay(this); 
                display.setCurrent(formTampil); 
            } 
        } 
     
        public void pauseApp() { 
        } 
     
        public void destroyApp(boolean unconditional) { 
        } 
     
        public void commandAction(Command c, Displayable d) { 
          
            try { 
                recordstore = RecordStore.openRecordStore("PeminjamRS", true); 
            } catch (Exception error) { 
                alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING); 
                alert.setTimeout(Alert.FOREVER); 
                display.setCurrent(alert); 
            } 
     
          
        if (c == cmdExit) { 
                destroyApp(true); 
                notifyDestroyed(); 
            } 
     
          
            if (c == cmdBack) { 
                display.setCurrent(list); 
            }
          
           
     
           
            if (c == cmdCancel) { 
                display.setCurrent(formTampil); 
            } 
     
         
            if (c == cmdTambahData) { 
                txtNama.setString(""); 
                txtNpm.setString(""); 
                txtJudul.setString(""); 
                txtTglPinjam.setString("");
                txtTglPulang.setString("");
                display.setCurrent(formTambahData); 
            } 
     
          
            if (c == cmdSimpan) { 
                try { 
                    String outputNama = txtNama.getString(); 
                    String outputNpm = txtNpm.getString(); 
                    String outputJudul = txtJudul.getString(); 
                    String outputPinjam = txtTglPinjam.getString();
                    String outputPulang = txtTglPulang.getString();
                    byte[] outputRecord; 
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
                    DataOutputStream outputDataStream = new DataOutputStream(outputStream); 
                    outputDataStream.writeUTF(outputNama); 
                    outputDataStream.writeUTF(outputNpm); 
                    outputDataStream.writeUTF(outputJudul); 
                    outputDataStream.writeUTF(outputPinjam);
                    outputDataStream.writeUTF(outputPulang);
                    outputDataStream.flush(); 
                    outputRecord = outputStream.toByteArray(); 
                    recordstore.addRecord(outputRecord, 0, outputRecord.length); 
                    outputStream.reset(); 
                    outputStream.close(); 
                    outputDataStream.close(); 
                    display.setCurrent(formTampil); 
                } catch (Exception error) { 
                    display.setCurrent(alert); 
                } 
            } 
     
          
            if (c == cmdUpdate) { 
                try { 
                    String outputNama = txtRecNama.getString(); 
                    String outputNpm = txtRecNpm.getString(); 
                    String outputJudul = txtRecJudul.getString(); 
                    String outputPinjam = txtRecPinjam.getString();
                    String outputPulang = txtRecPulang.getString();
                    byte[] outputRecord; 
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
                    DataOutputStream outputDataStream = new DataOutputStream(outputStream); 
                    outputDataStream.writeUTF(outputNama); 
                    outputDataStream.writeUTF(outputNpm); 
                    outputDataStream.writeUTF(outputJudul); 
                    outputDataStream.writeUTF(outputPinjam);
                     outputDataStream.writeUTF(outputPulang);
                    outputDataStream.flush(); 
                    outputRecord = outputStream.toByteArray(); 
                    recordstore.setRecord(recID, outputRecord, 0, outputRecord.length); 
                    outputStream.reset(); 
                    outputStream.close(); 
                    outputDataStream.close(); 
                    display.setCurrent(formTampil); 
                } catch (Exception error) { 
                    alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING); 
                    alert.setTimeout(Alert.FOREVER); 
                    display.setCurrent(alert); 
                } 
            } 
     
        
            if (c == cmdEdit) { 
                int index = 1 + list.getSelectedIndex(); 
                try { 
                    formEdit = new Form("Ubah data Peminjam"); 
                    formEdit.addCommand(cmdBack); 
                    formEdit.addCommand(cmdUpdate); 
                    formEdit.setCommandListener(this); 
                    byte[] byteInputData = new byte[300]; 
                    String inputNama, inputNpm, inputJudul, inputPinjam,inputPulang = null; 
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); 
                    DataInputStream inputDataStream = new DataInputStream(inputStream); 
                 
                    if (currentTask == 2) { 
                        RmsBanding comparator = new RmsBanding(); 
                        recordEnumeration = recordstore.enumerateRecords(null, comparator, false); 
                    } 
                    for (int i = 1; i <= recordEnumeration.numRecords(); i++) { 
                        int datapointer = recordEnumeration.nextRecordId(); 
                        if (i == index) { 
                            recordstore.getRecord(datapointer, byteInputData, 0); 
                            recID = datapointer; 
                            inputNama = inputDataStream.readUTF(); 
                            inputNpm = inputDataStream.readUTF(); 
                            inputJudul = inputDataStream.readUTF(); 
                            inputPinjam = inputDataStream.readUTF();
                            inputPulang = inputDataStream.readUTF();
                            txtRecNama = new TextField("Nama\t:", inputNama, 30, TextField.ANY); 
                            txtRecNpm = new TextField("No Identitas\t:", inputNpm, 30, TextField.PHONENUMBER); 
                            txtRecJudul = new TextField("Judul Buku\t:", inputJudul, 100, TextField.ANY); 
                            txtRecPinjam = new TextField("Tanggal Peminjaman\t:", inputPinjam, 30, TextField.ANY);
                             txtRecPulang = new TextField("Tanggal Pengembalian\t:", inputPulang, 30, TextField.ANY);
     
                            formEdit.append(txtRecNama); 
                            formEdit.append(txtRecNpm); 
                            formEdit.append(txtRecJudul); 
                            formEdit.append(txtRecPinjam);
                             formEdit.append(txtRecPulang);
                            inputStream.reset(); 
                        } 
                    } 
                    display.setCurrent(formEdit); 
                    inputStream.close(); 
                } catch (Exception error) { 
                    alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING); 
                    alert.setTimeout(Alert.FOREVER); 
                    display.setCurrent(alert); 
                } 
            } 
     
            if (c == cmdDelete) { 
                int index = 1 + list.getSelectedIndex(); 
                try { 
                   
                    if (currentTask == 2) { 
                        RmsBanding comparator = new RmsBanding(); 
                        recordEnumeration = recordstore.enumerateRecords(null, comparator, false); 
                    } 
                    for (int i = 1; i <= recordEnumeration.numRecords(); i++) { 
                        int datapointer = recordEnumeration.nextRecordId(); 
                        if (i == index) { 
                            recordstore.deleteRecord(datapointer); 
                        } 
                    } 
                    display.setCurrent(formTampil); 
                } catch (Exception error) { 
                    alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING); 
                    alert.setTimeout(Alert.FOREVER); 
                    display.setCurrent(alert); 
                } 
            } 
     
            if (c == cmdLihatAlumni) { 
                try { 
                    list = new List(" Nama-Nama Peminjam", List.IMPLICIT); 
                    list.addCommand(cmdTambahData);
                    list.addCommand(cmdCancel);
                    list.setTicker(t);
                    list.setCommandListener(this); 
                    currentTask = 2; 
                    byte[] byteInputData = new byte[300]; 
                    String inputNama, inputNpm, inputJudul, inputPinjam,inputPulang = null; 
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); 
                    DataInputStream inputDataStream = new DataInputStream(inputStream); 
                    RmsBanding comparator = new RmsBanding(); 
                    recordEnumeration = recordstore.enumerateRecords(null, comparator, false); 
                    while (recordEnumeration.hasNextElement()) { 
                        recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0); 
                        inputNama = inputDataStream.readUTF(); 
                        inputNpm = inputDataStream.readUTF(); 
                        inputJudul = inputDataStream.readUTF(); 
                        inputPinjam = inputDataStream.readUTF();
                        inputPulang = inputDataStream.readUTF();
                        list.append(inputNama + "", null); 
                        inputStream.reset(); 
                    } 
                    display.setCurrent(list); 
                    inputStream.close(); 
                } catch (Exception error) { 
                    alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING); 
                    alert.setTimeout(Alert.FOREVER); 
                    display.setCurrent(alert); 
                } 
            } 
     
          
            if (c == List.SELECT_COMMAND) { 
                int index = 1 + list.getSelectedIndex(); 
                try { 
                    formLihat = new Form("Keterangan : "); 
                    formLihat.addCommand(cmdBack); 
                    formLihat.addCommand(cmdEdit); 
                    formLihat.addCommand(cmdDelete); 
                    formLihat.setCommandListener(this); 
                    byte[] byteInputData = new byte[300]; 
                    String inputNama, inputNpm, inputJudul, inputPinjam,inputPulang = null; 
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData); 
                    DataInputStream inputDataStream = new DataInputStream(inputStream); 
                  
                    if (currentTask == 2) { 
                        RmsBanding comparator = new RmsBanding(); 
                        recordEnumeration = recordstore.enumerateRecords(null, comparator, false); 
                    } 
                    for (int i = 1; i <= recordEnumeration.numRecords(); i++) { 
                        int datapointer = recordEnumeration.nextRecordId(); 
                        if (i == index) { 
                            recordstore.getRecord(datapointer, byteInputData, 0); 
                            recID = datapointer; 
                            inputNama = inputDataStream.readUTF(); 
                            inputNpm = inputDataStream.readUTF(); 
                            inputJudul = inputDataStream.readUTF(); 
                            inputPinjam = inputDataStream.readUTF();
                            inputPulang = inputDataStream.readUTF();
                            StringItem stringNama = new StringItem("Nama Peminjam\t: ", inputNama, Item.PLAIN); 
                            StringItem stringNpm = new StringItem("No Identitas\t: ", inputNpm, Item.PLAIN); 
                            StringItem stringJudul = new StringItem("Judul Buku\t: ", inputJudul, Item.PLAIN); 
                            StringItem stringPinjam = new StringItem("Tanggal Peminjaman\t: ", inputPinjam, Item.PLAIN);
                            StringItem stringPulang = new StringItem("Tanggal Pengembalian\t: ", inputPulang, Item.PLAIN);
                            formLihat.append(stringNama); 
                            formLihat.append(stringNpm); 
                            formLihat.append(stringJudul); 
                            formLihat.append(stringPinjam);
                            formLihat.append(stringPulang);
                            inputStream.reset(); 
                        } 
                    } 
                    display.setCurrent(formLihat); 
                    inputStream.close(); 
                } catch (Exception error) { 
                    alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING); 
                    alert.setTimeout(Alert.FOREVER); 
                    display.setCurrent(alert); 
                } 
            } 
     
            try { 
                recordstore.closeRecordStore(); 
            } catch (Exception error) { 
                alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING); 
                alert.setTimeout(Alert.FOREVER); 
                display.setCurrent(alert); 
            } 
        } 
    }  





Baik!, sampai disini dulu artikel yang penulis berikan. Semoga dapat bermanfaat dan membantu Anda semua. Salam "Java Mobile Perbaungan"!!




2 komentar:

Unknown mengatakan...

Semoga bermanfaat!!

Unknown mengatakan...

boleh minta project-nya gak bang.?
untuk di pelajari karna lagi nyusun skripsi tentang j2me

Jumlah Pengunjung

About Me

Follower