Selasa, 10 Desember 2013

Tugas Pemrograman Mobil 5

package newpackage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class Praymom_RMS 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 proses entri data
private Form entri;
private TextField tfNama, tfTTL,tfJ_Kelamin,tfAlmt,tfOld_Id, input1, input2,input3, input4, input5;

//construktor
private final Command cmdKeluar = new Command("Exit", Command.EXIT, 1);
private final Command cmdPilih = new Command("Pilih", Command.OK, 1);
private final Command cmdSimpan = new Command("Save", Command.SCREEN, 1);
private final Command cmdUpdate = new Command("Update", Command.SCREEN, 1);
private final Command cmdHapus = new Command("Delete", Command.SCREEN, 1);
private final Command cmdEdit= new Command("Edit", Command.SCREEN, 1);
private final Command cmdKembali = new Command("Back", Command.BACK, 1);
private final Command combanding = new Command("bandingkan", Command.OK, 1);


//instance class
public Praymom_RMS() {
display = Display.getDisplay(this);
alert = new Alert(null);
alert.setTimeout(Alert.FOREVER);
list = new List(null, Choice.IMPLICIT);
RS = null;
try
{
RS = RecordStore.openRecordStore("db_KodeBar", true);
}
catch (RecordStoreException rse)
{
alert.setString("Record store tidak dapat dibuka. " +"Aplikasi akan dihetikan");
alert.setType(AlertType.ERROR);
display.setCurrent(alert, null);
System.exit(1);
}
}
public void startApp()
{
tambahRecord("Khoirul Anwar","Kendal, 17 April 1992","Laki-Laki","bandengan kendal");
tambahRecord("Danis Wara","Ungaran, 23 Juli 1993","Laki Laki","Jl Leyangan");
tambahRecord("Dadan Nugraha","Semarang, 19 November 1993 ","Laki-Laki","Perum Pedurungan");
tambahRecord("Deni Nur Setiawan","Jepara, 10 Mei 1992 ","Laki-Laki","Jl Kalibanteng");
tambahRecord("Ruth Mutiara Sari","Semarang, 26 April 1993","Perempuan","Jalan Pasadena");
tambahRecord("Mifthakul Huda","Sayung, 3 November 1992 ","Laki-Laki","Sayung");

form = new Form("Praymom Anggota Band");
choicegroup = new ChoiceGroup("Menu Pilihan:", Choice.EXCLUSIVE);
choicegroup.append("Tambah Anggota ", null);
choicegroup.append("Daftar Anggota ", 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(" ") && !tfTTL.equals(" ") && !tfJ_Kelamin.equals(" ") && !tfAlmt.equals(" ") ) //cek jika var ada isi nya
{
tambahRecord(tfNama.getString(),tfTTL.getString(), tfJ_Kelamin.getString(), tfAlmt.getString());
alert.setString("Data terbaru berhasil disimpan");
display.setCurrent(alert, form);
}
else //jika data kosong
{
alert.setString("Daftar Nama Anggota, Tempat Tanggal Lahir, Jenis Kelamin, Alamat " + "tidak boleh kosong");
display.setCurrent(alert, entri);
}
}
else if (c == cmdUpdate)
{
alert.setType(AlertType.INFO);
if (!tfNama.equals(" ") && !tfTTL.equals(" ") && !tfJ_Kelamin.equals(" ") && !tfAlmt.equals(" ")) //cek jika var ada isi nya
{
updateRecord(tfJ_Kelamin.getString(),tfNama.getString(), tfTTL.getString(), tfAlmt.getString(), tfOld_Id.getString());
alert.setString("Data terbaru berhasil disimpan");
display.setCurrent(alert, form);
}
else
{
alert.setString("Data Nama Anggota, Tempat Tanggal Lahir, Jenis Kelamin, Alamat " + "tidak boleh kosong");
display.setCurrent(alert, entri);
}
}
else if (c == cmdHapus || c == cmdEdit)
{
int pos = list.getString(list.getSelectedIndex()).indexOf("->");
String id_bc = list.getString(list.getSelectedIndex()).substring(0, pos);
if(c == cmdHapus){
hapusRecord(id_bc);
}
else{
editData(id_bc);
}
}
}



//fungsi tambah record
public void tambahRecord(String Nama, String TTL, String Jenis_Kelamin, String Alamat)
{
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(Nama);// tulis var pada dos
dos.writeUTF(TTL);// tulis var pada dos
dos.writeUTF(Jenis_Kelamin);
dos.writeUTF(Alamat);
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 Nama, String TTL, String Jenis_Kelamin, String Alamat, 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 internet
dos.writeUTF(Nama);// tulis var pada dos
dos.writeUTF(TTL);// tulis var pada dos
dos.writeUTF(Jenis_Kelamin);
dos.writeUTF(Alamat);
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 Anggota Praymom");
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"+harga+"\n"+keterangan+"\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("Masukan Daftar Anggota");
tfNama = new TextField("Nama :", null, 25, TextField.ANY);
tfTTL = new TextField("TTL :", null, 50, TextField.ANY);
tfJ_Kelamin = new TextField("Jenis Kelamin:", null, 15, TextField.ANY);
tfAlmt = new TextField("Alamat :", null, 100, TextField.ANY);
entri.append(tfNama);
entri.append(tfTTL);
entri.append(tfJ_Kelamin);
entri.append(tfAlmt);
entri.addCommand(cmdSimpan);
entri.addCommand(cmdKembali);
entri.setCommandListener(this);
display.setCurrent(entri);
return entri;
}
public Form editData(String Id_Old)
{
entri = new Form("Edit Daftar Anggota");
tfOld_Id = new TextField("Nama (Lama):", Id_Old, 25, TextField.UNEDITABLE);
tfNama = new TextField("Nama (Baru):", Id_Old, 25, TextField.ANY);
tfTTL = new TextField("TTL :", null, 25, TextField.ANY);
tfJ_Kelamin = new TextField("Jenis Kelamin:", null, 15, TextField.ANY);
tfAlmt = new TextField("Alamat :", null, 100, TextField.ANY);

entri.append(tfOld_Id);
entri.append(tfNama);
entri.append(tfTTL);
entri.append(tfJ_Kelamin);
entri.append(tfAlmt);
entri.addCommand(cmdUpdate);
entri.addCommand(cmdKembali);
entri.setCommandListener(this);
display.setCurrent(entri);
return entri;
}
}

Tidak ada komentar:

Posting Komentar

berkomentarlah untuk membagi ilmumu

Cara Seo Blogger