Kamis, 19 Juli 2012
PROGRAM DAFTAR GAJI KARYAWAN BANK BRI (J2ME dengan RMS, RecordComparator, dan RecordFilter)
Untuk program yang penulis postingkan kali ini merupakan program yang lebih lengkap dari yang sebelumnya. Hal ini dikarenakan adanya penambahan seperti RecordFilter yang berfungsi untuk menyaring Record/Data yang telah diinputkan. Hasilnya adalah kita bisa mencari data yang kita inginkan dengan hanya menulis sebagian atau keseluruhan dari data tersebut. Apabila Record/Data terdiri dari jumlah yang cukup banyak, maka RecordFilter ini akan sangat bermanfaat karena bisa menghemat waktu dan mempermudah pencarian.
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
"JavaMobilePerbaungan3"
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"DaftarGajiKaryawan" lalu klik "Finish"
Listing Program "DaftarGajiKaryawan.java"
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class DaftarGajiKaryawan extends MIDlet implements CommandListener {
Display display;
Alert alert;
Form formMotto,formUtama, formTambahData, formLihat, formEdit, formCari,formBank;
List list;
RecordStore recordstore = null;
RecordEnumeration recordEnumeration = null;
Penyaring filter = null;
Ticker tik;
Command cmdBank=new Command ("Tentang Bank",Command.OK,3);
Command cmdMotto=new Command ("Motto Bank",Command.OK,2);
Command cmdKeluar = new Command("Keluar", Command.EXIT, 1);
Command cmdLihatData = new Command("Lihat Data", Command.SCREEN, 1);
Command cmdDataBaru = new Command("Tambahkan Data", Command.SCREEN, 1);
Command cmdEdit = new Command("Edit Data", Command.SCREEN, 1);
Command cmdHapus = new Command("Hapus Data", Command.SCREEN, 1);
Command cmdFind = new Command("Cari Karyawan", Command.SCREEN, 2);
Command cmdCari = new Command("Cari", Command.SCREEN, 1);
Command cmdSimpan = new Command("Simpan", Command.SCREEN, 1);
Command cmdUbah = new Command("Edit", Command.SCREEN, 1);
Command cmdCancel = new Command("Kembali", Command.BACK, 1);
Command cmdBack = new Command("Kembali", Command.BACK, 1);
Command cmdKembali=new Command("Kembali",Command.BACK,1);
TextField txtNama = new TextField("Nama:", "", 20, TextField.ANY);
TextField txtNIK = new TextField("NIK:", "", 30, TextField.PHONENUMBER);
TextField txtJabatan = new TextField("Jabatan:", "", 20, TextField.ANY);
TextField txtGaji = new TextField("Gaji:", "", 30, TextField.PHONENUMBER);
TextField txtRecNama, txtRecNIK, txtRecJabatan, txtRecGaji;
TextField txtFind = new TextField("Masukkan Nama Karyawan:", "", 20, TextField.ANY);
int recID, currentTask;
String keyword;
public DaftarGajiKaryawan() {
tik = new Ticker("Aplikasi Pendataan karyawan Bank BRI");
formMotto=new Form("Motto Bank BRI");
formMotto.append("=================================");
formMotto.append("\" Melayani Dengan Setulus Hati\"");
formMotto.append("=================================");
formMotto.setTicker(tik);
formMotto.addCommand(cmdKembali);
formMotto.setCommandListener(this);
formUtama = new Form("");
try {
Image img = Image.createImage("/java mobile perbaungan.jpg");
ImageItem image = new ImageItem("", img, Item.LAYOUT_CENTER, "Data Karyawan");
formUtama.append(image);
} catch (Exception e) {
e.printStackTrace();
}
formUtama.addCommand(cmdLihatData);
formUtama.addCommand(cmdKeluar);
formUtama.addCommand(cmdBank);
formUtama.addCommand(cmdMotto);
formUtama.setCommandListener(this);
formTambahData = new Form("Tambah Data Karyawan");
formTambahData.addCommand(cmdBack);
formTambahData.addCommand(cmdSimpan);
formTambahData.append(txtNama);
formTambahData.append(txtNIK);
formTambahData.append(txtJabatan);
formTambahData.append(txtGaji);
formTambahData.setTicker(tik);
formTambahData.setCommandListener(this);
formCari = new Form("Cari Data Karyawan");
formCari.addCommand(cmdBack);
formCari.addCommand(cmdCari);
formCari.append(txtFind);
formCari.setTicker(tik);
formCari.setCommandListener(this);
formBank=new Form("Tentang Bank");
formBank.append("Bank Rakyat Indonesia (BRI) adalah salah satu bank milik pemerintah" +
" yang terbesar di Indonesia. Pada awalnya Bank Rakyat Indonesia (BRI)" +
" didirikan di Purwokerto, Jawa Tengah oleh Raden Bei Aria Wirjaatmadja dengan" +
" nama De Poerwokertosche Hulp en Spaarbank der Inlandsche Hoofden atau \"Bank" +
" Bantuan dan Simpanan Milik Kaum Priyayi Purwokerto\", suatu lembaga keuangan" +
" yang melayani orang-orang berkebangsaan Indonesia (pribumi). Lembaga tersebut" +
" berdiri tanggal 16 Desember 1895, yang kemudian dijadikan sebagai hari kelahiran BRI.");
formBank.addCommand(cmdKembali);
formBank.setTicker(tik);
formBank.setCommandListener(this);
}
public void startApp() {
if (display == null) {
display = Display.getDisplay(this);
display.setCurrent(formUtama);
}
}
public void pauseApp() {
}
public class Pembanding 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 class Penyaring implements RecordFilter {
private String search = null;
private ByteArrayInputStream inputstream = null;
private DataInputStream datainputstream = null;
public Penyaring(String search) {
this.search = search.toLowerCase();
}
public boolean matches(byte[] suspect) {
String string = new String(suspect).toLowerCase();
if (string != null && string.indexOf(search) != -1) {
return true;
} else {
return false;
}
}
public void filterClose() {
try {
if (inputstream != null) {
inputstream.close();
}
if (datainputstream != null) {
datainputstream.close();
}
} catch (Exception error) {
}
}
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
try {
recordstore = RecordStore.openRecordStore("DataKaryawanRS", true);
} catch (Exception error) {
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
if (c == cmdKeluar) {
destroyApp(true);
notifyDestroyed();
}
if (c == cmdBack) {
display.setCurrent(list);
}
if (c == cmdCancel) {
display.setCurrent(formUtama);
}
if (c == cmdBank) {
display.setCurrent(formBank);
}
if (c == cmdMotto) {
display.setCurrent(formMotto);
}
if (c == cmdKembali) {
display.setCurrent(formUtama);
}
if (c == cmdDataBaru) {
txtNama.setString("");
txtNIK.setString("");
txtJabatan.setString("");
txtGaji.setString("");
display.setCurrent(formTambahData);
}
if (c == cmdFind) {
txtFind.setString("");
display.setCurrent(formCari);
}
if (c == cmdCari) {
keyword = txtFind.getString();
try {
list = new List("Nama-Nama Karyawan", List.IMPLICIT);
list.addCommand(cmdDataBaru);
list.addCommand(cmdFind);
list.addCommand(cmdCancel);
list.setTicker(tik);
list.setCommandListener(this);
currentTask = 1;
String inputName = null;
byte[] byteInputData = new byte[300];
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (recordstore.getNumRecords() > 0) {
filter = new Penyaring(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
while (recordEnumeration.hasNextElement()) {
recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
inputName = inputDataStream.readUTF();
list.append(inputName + "", 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 == cmdSimpan) {
try {
String outputNama = txtNama.getString();
String outputNIK = txtNIK.getString();
String outputJabatan = txtJabatan.getString();
String outputGaji = txtGaji.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputNama);
outputDataStream.writeUTF(outputNIK);
outputDataStream.writeUTF(outputJabatan);
outputDataStream.writeUTF(outputGaji);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(formUtama);
} catch (Exception error) {
display.setCurrent(alert);
}
}
if (c == cmdUbah) {
try {
String outputNama = txtRecNama.getString();
String outputNIK = txtRecNIK.getString();
String outputJabatan = txtRecJabatan.getString();
String outputGaji = txtRecGaji.getString();
byte[] outputRecord;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
outputDataStream.writeUTF(outputNama);
outputDataStream.writeUTF(outputNIK);
outputDataStream.writeUTF(outputJabatan);
outputDataStream.writeUTF(outputGaji);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.setRecord(recID, outputRecord, 0, outputRecord.length);
outputStream.reset();
outputStream.close();
outputDataStream.close();
display.setCurrent(formUtama);
} 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("Edit Data Karyawan");
formEdit.addCommand(cmdBack);
formEdit.addCommand(cmdUbah);
formEdit.setTicker(tik);
formEdit.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputNama, inputNIK, inputJabatan, inputGaji = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 1) {
filter = new Penyaring(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if (currentTask == 2) {
Pembanding comparator = new Pembanding();
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();
inputNIK = inputDataStream.readUTF();
inputJabatan = inputDataStream.readUTF();
inputGaji = inputDataStream.readUTF();
txtRecNama = new TextField("Nama:", inputNama, 20, TextField.ANY);
txtRecNIK = new TextField("NIK:", inputNIK, 30, TextField.PHONENUMBER);
txtRecJabatan = new TextField("Jabatan:", inputJabatan, 20, TextField.ANY);
txtRecGaji = new TextField("Gaji:", inputGaji, 30, TextField.PHONENUMBER);
formEdit.append(txtRecNama);
formEdit.append(txtRecNIK);
formEdit.append(txtRecJabatan);
formEdit.append(txtRecGaji);
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 == cmdHapus) {
int index = 1 + list.getSelectedIndex();
try {
if (currentTask == 1) {
filter = new Penyaring(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if (currentTask == 2) {
Pembanding comparator = new Pembanding();
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(formUtama);
} catch (Exception error) {
alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
if (c == cmdLihatData) {
try {
list = new List("Nama-Nama Karyawan", List.IMPLICIT);
list.addCommand(cmdDataBaru);
list.addCommand(cmdFind);
list.addCommand(cmdCancel);
list.setTicker(tik);
list.setCommandListener(this);
currentTask = 2;
byte[] byteInputData = new byte[300];
String inputNama, inputNIK, inputJabatan, inputGaji = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
Pembanding comparator = new Pembanding();
recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
while (recordEnumeration.hasNextElement()) {
recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
inputNama = inputDataStream.readUTF();
inputNIK = inputDataStream.readUTF();
inputJabatan = inputDataStream.readUTF();
inputGaji = 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("Rincian Data Karyawan ");
formLihat.addCommand(cmdBack);
formLihat.addCommand(cmdEdit);
formLihat.addCommand(cmdHapus);
formLihat.setTicker(tik);
formLihat.setCommandListener(this);
byte[] byteInputData = new byte[300];
String inputNama, inputNIK, inputJabatan, inputGaji = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
if (currentTask == 1) {
filter = new Penyaring(keyword);
recordEnumeration = recordstore.enumerateRecords(filter, null, false);
}
if (currentTask == 2) {
Pembanding comparator = new Pembanding();
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();
inputNIK = inputDataStream.readUTF();
inputJabatan = inputDataStream.readUTF();
inputGaji = inputDataStream.readUTF();
StringItem stringNama = new StringItem("Nama: ", inputNama, Item.PLAIN);
StringItem stringNIK = new StringItem("NIK: ", inputNIK, Item.PLAIN);
StringItem stringJabatan = new StringItem("Jabatan: ", inputJabatan, Item.PLAIN);
StringItem stringGaji = new StringItem("Gaji: ", inputGaji, Item.PLAIN);
formLihat.append(stringNama);
formLihat.append(stringNIK);
formLihat.append(stringJabatan);
formLihat.append(stringGaji);
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);
}
}
}
Demikianlah Program Daftar Gaji Karyawan ini. Semoga dapat bermanfaat dan membantu Anda semua. Salam "Java Mobile Perbaungan"!!
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar