Sie sind auf Seite 1von 7

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ import import import import java.io.*; javax.microedition.lcdui.*; javax.microedition.midlet.*; javax.microedition.rms.*;

/** * @author user */ public class ReMS extends MIDlet implements CommandListener { private Display display; private Form form,banding; private RecordStore rs; private RecordEnumeration re; private ChoiceGroup choicegroup; private Alert alert; private List list; //untuk private private 2,input3, proses entri data Form entri; TextField tfNama, tfHarga,tfID_BC,tfKeterangan,tfOld_Id, input1, input input4, input5; Command Command Command Command Command Command Command Command cmdKeluar = new Command("Keluar", Command.EXIT, 1); cmdPilih = new Command("Pilih", Command.OK, 1); cmdSimpan = new Command("Simpan", Command.SCREEN, 1); cmdUpdate = new Command("Ubah", Command.SCREEN, 1); cmdHapus = new Command("Hapus", Command.SCREEN, 1); cmdEdit= new Command("Edit", Command.SCREEN, 1); cmdKembali = new Command("Kembali", Command.BACK, 1); combanding = new Command("bandingkan", Command.OK, 1)

//construktor private final private final private final private final private final private final private final private final ;

//instance class public ReMS() { display = Display.getDisplay(this); alert = new Alert(null); alert.setTimeout(Alert.FOREVER); list = new List(null, Choice.IMPLICIT); rs = null; // membuat atau membuka record store try { rs = RecordStore.openRecordStore("db_KodeBar", true); } catch (RecordStoreException rse) //klo error = klo exception { alert.setString("Record store tidak dapat dibuka. " +"Aplikasi akan dihe tikan"); alert.setType(AlertType.ERROR); display.setCurrent(alert, null); System.exit(1);

} }

public void startApp() { tambahRecord("01","Lemari","100000","The overall goal of this book is to p rovide introductory coverage of Symbian OS"); tambahRecord("02","Labtop","30000000","The overall goal of this book is to provide introductory coverage of Symbian OS"); tambahRecord("03","PC","3500000","The overall goal of this book is to prov ide introductory coverage of Symbian OS"); tambahRecord("04","Kursi","230000","The overall goal of this book is to pr ovide introductory coverage of Symbian OS"); form = new Form("Inventarisasi Barang/ Produk"); choicegroup = new ChoiceGroup("Menu Inventarisasi :", Choice.EXCLUSIVE); choicegroup.append("Masukkan Data ", null); choicegroup.append("Lihat Data ", null); form.append(choicegroup); form.addCommand(cmdKeluar); form.addCommand(cmdPilih); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == cmdKeluar) { destroyApp(false); notifyDestroyed(); } else if (c == cmdPilih) { switch (choicegroup.getSelectedIndex()) { case 0: { entriData(); //fungsi masukin data break; } case 1: { lihatRecord(); //fungsi liat record break; } } } else if (c == cmdKembali) {

display.setCurrent(form); } else if (c == cmdSimpan) { alert.setType(AlertType.INFO); if (!tfNama.equals("") && !tfHarga.equals("") && !tfID_BC.equals("") & & !tfKeterangan.equals("")) //cek jika var ada isi nya { tambahRecord(tfID_BC.getString(),tfNama.getString(), tfHarga.getSt ring(),tfKeterangan.getString()); alert.setString("Data baru telah berhasil disimpan"); display.setCurrent(alert, form); } else //jika data kosong { alert.setString("Data Nama Produk, Harga, ID Brcode/ ISBN, Keteran gan " + "tidak boleh kosong"); display.setCurrent(alert, entri); } } else if (c == cmdUpdate) { alert.setType(AlertType.INFO); if (!tfNama.equals("") && !tfHarga.equals("") && !tfID_BC.equals("") & & !tfKeterangan.equals("")) //cek jika var ada isi nya { updateRecord(tfID_BC.getString(),tfNama.getString(), tfHarga.getSt ring(), tfKeterangan.getString(), tfOld_Id.getString()); alert.setString("Data baru telah berhasil disimpan"); display.setCurrent(alert, form); } else //jika data kosong { alert.setString("Data Nama Produk, Harga, ID Brcode/ ISBN, Keteran gan " + "tidak boleh kosong"); display.setCurrent(alert, entri); } } else if (c == cmdHapus || c == cmdEdit) { int pos = list.getString(list.getSelectedIndex()).indexOf("->"); //amb il index dari list String id_bc = list.getString(list.getSelectedIndex()).substring(0, po s); if(c == cmdHapus){ hapusRecord(id_bc); } else{ editData(id_bc); } } } //fungsi tambah record public void tambahRecord(String id_bc, String nama, String harga, String keter angan) { byte[] temp = null; try

{ //kosntruktor baos dan dos ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); //cari fungsi ini ByteArrayOutputStream dan DataOutputStream di internet dos.writeUTF(id_bc);// tulis var pada dos dos.writeUTF(nama);// tulis var pada dos dos.writeUTF(harga); dos.writeUTF(keterangan); temp = baos.toByteArray(); } catch (IOException ioe) { ioe.printStackTrace(); } try { rs.addRecord(temp, 0, temp.length); //masukan data pada RecordStore } catch (RecordStoreNotOpenException rsnoe) { rsnoe.printStackTrace(); } catch (RecordStoreException rse) { rse.printStackTrace(); } } void updateRecord(String id_bc, String nama, String harga, String keterangan, String Old_Id) { byte[] temp = null; try { re = rs.enumerateRecords(null, null, false); while (re.hasNextElement()) { int i = re.nextRecordId(); temp = rs.getRecord(i); ByteArrayInputStream bais = new ByteArrayInputStream(temp); DataInputStream dis = new DataInputStream(bais); try { String vID_BC = dis.readUTF(); if (vID_BC.equals(Old_Id)) { rs.deleteRecord(i); break; } } catch (IOException ioe) { ioe.printStackTrace();} } re.rebuild(); } catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();} catch (RecordStoreException rse) {rse.printStackTrace();} try { //kosntruktor baos dan dos ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos);

//cari fungsi ini ByteArrayOutputStream dan DataOutputStream di inte rnet dos.writeUTF(id_bc);// tulis var pada dos dos.writeUTF(nama);// tulis var pada dos dos.writeUTF(harga); dos.writeUTF(keterangan); temp = baos.toByteArray(); } catch (IOException ioe) { ioe.printStackTrace(); } try { rs.addRecord(temp, 0, temp.length); //masukan data pada RecordStore } catch (RecordStoreNotOpenException rsnoe) { rsnoe.printStackTrace(); } catch (RecordStoreException rse) { rse.printStackTrace(); } } public void lihatRecord() { byte[] temp = null; list.setTitle("Daftar Barang/ Produk"); list.deleteAll(); try { re = rs.enumerateRecords(null, null, false); //enumerasi record while (re.hasNextElement()) //jk elemen rs lbh dr satu { int i = re.nextRecordId(); temp = rs.getRecord(i); ByteArrayInputStream bais = new ByteArrayInputStream(temp); DataInputStream dis = new DataInputStream(bais); try { String id_bc = dis.readUTF(); String nama = dis.readUTF(); String harga = dis.readUTF(); String keterangan = dis.readUTF(); list.append(id_bc + "-> \n -"+nama + "\n -Rp "+harga+"\n -"+ke terangan+" \n", null);// tampilkan } catch (IOException ioe) {ioe.printStackTrace();} } list.addCommand(cmdKembali); list.addCommand(cmdHapus); list.addCommand(cmdEdit); list.setCommandListener(this); display.setCurrent(list); } catch (InvalidRecordIDException invID) {invID.printStackTrace();} catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();} catch (RecordStoreException rse) {rse.printStackTrace();} }

public void hapusRecord(String id_bc) { byte[] temp = null; try { re = rs.enumerateRecords(null, null, false); while (re.hasNextElement()) { int i = re.nextRecordId(); temp = rs.getRecord(i); ByteArrayInputStream bais = new ByteArrayInputStream(temp); DataInputStream dis = new DataInputStream(bais); try { String vID_BC = dis.readUTF(); if (vID_BC.equals(id_bc)) { rs.deleteRecord(i); break; } } catch (IOException ioe) { ioe.printStackTrace();} } re.rebuild(); //memulai enumerasi baru, karena data telah berubah lihatRecord(); } catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();} catch (RecordStoreException rse) {rse.printStackTrace();} } public Form entriData() { entri = new Form("Entri Data"); tfID_BC = new TextField("Kode Barang :", null, 25, TextField.ANY); tfNama = new TextField("Nama Barang :", null, 25, TextField.ANY); tfHarga = new TextField("Harga:", null, 15, TextField.NUMERIC); tfKeterangan = new TextField("Keterangan :", null, 100, TextField.ANY); entri.append(tfID_BC); entri.append(tfNama); entri.append(tfHarga); entri.append(tfKeterangan); entri.addCommand(cmdSimpan); entri.addCommand(cmdKembali); entri.setCommandListener(this); display.setCurrent(entri); return entri; } public Form editData(String Id_Old) { entri = new Form("Edit Data"); tfOld_Id = new TextField("ID Barcode / ISBN (Lama):", Id_Old, 25, TextField. UNEDITABLE); tfID_BC = new TextField("ID Barcode / ISBN (Baru):", Id_Old, 25, TextField.A NY); tfNama = new TextField("Nama Barang/ Produk :", null, 25, TextField.ANY); tfHarga = new TextField("Harg a:", null, 15, TextField.NUMERIC); tfKeterangan = new TextField("Keterangan :", null, 100, TextField.ANY);

entri.append(tfOld_Id); entri.append(tfID_BC); entri.append(tfNama); entri.append(tfHarga); entri.append(tfKeterangan); entri.addCommand(cmdUpdate); entri.addCommand(cmdKembali); entri.setCommandListener(this); display.setCurrent(entri); return entri; }

Das könnte Ihnen auch gefallen