Kamis, 29 November 2012
PERSISTANCE
package ListMenu;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import java.io.*;
public class ListMenu extends MIDlet implements CommandListener{
private Display display;
private List list;
private Command cmdKeluar;
private Command cmdKembali, cmdHasil, cmdbalik, cmdSimpan;
private TextField soal1, soal2, soal3, soal4, soal5, id;
private Form form;
int nilai1, nilai2, nilai3, nilai4, nilai5;
int Nakhir;
private RecordStore rs;
private RecordEnumeration re;
private Alert alert;
public ListMenu() {
form = new Form("MENU UTAMA");
cmdKeluar = new Command("Keluar", Command.EXIT, 0);
cmdKembali = new Command("Kembali", Command.BACK, 1);
cmdbalik = new Command("Kembali", Command.BACK, 1);
cmdHasil = new Command("Hasil", Command.OK, 2);
cmdSimpan = new Command("Simpan", Command.OK, 2);
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) //check kalau error = exception
{
alert.setString("Record store tidak dapat dibuka. " +"Aplikasi akan dihetikan");
alert.setType(AlertType.ERROR);
display.setCurrent(alert, null);
System.exit(1);
}
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
menuList();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void menuList(){
list = new List("MENU UTAMA", List.IMPLICIT);
list.append("UJIAN", null);
list.append("Nilai Tertinggi", null);
list.append("Keluar", null);
list.setCommandListener(this);
display.setCurrent(list);
}
public void UJIAN(){
form = new Form("UJIAN");
soal1 = new TextField("1. 2+3 = ?", "", 100, TextField.DECIMAL);
soal2 = new TextField("2. 8/2 = ?", "", 100, TextField.DECIMAL);
soal3 = new TextField("3. 3*4 = ?", "", 100, TextField.DECIMAL);
soal4 = new TextField("4. 7-3 = ?", "", 100, TextField.DECIMAL);
soal5 = new TextField("5. (7-3)*2 = ?", "", 100, TextField.DECIMAL);
form.append(soal1);
form.append(soal2);
form.append(soal3);
form.append(soal4);
form.append(soal5);
form.setCommandListener(this);
form.addCommand(cmdKembali);
form.addCommand(cmdHasil);
display.setCurrent(form);
}
public void nama(){
form = new Form("SIMPAN HASIL");
id = new TextField("Masukkan nama anda : ","", 100, TextField.ANY);
form.append(id);
form.setCommandListener(this);
form.addCommand(cmdSimpan);
form.addCommand(cmdbalik);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdKeluar){
destroyApp(true);
}
if(c.getLabel()=="Simpan"){
alert.setType(AlertType.INFO);
if (!id.equals(null)) //cek jika var ada isi nya
{
tambahRecord(id.getString());
alert.setString("Data lengkap !, Data sudah disimpan di RMS, untuk melihat gunakan pilihan menu'nilai tertinggi' pada menu utama");
display.setCurrent(alert);
display.setCurrent(alert, list);
}
else //jika data kosong
{
alert.setString("Data Kurang !, isikan nama anda terlebih dahulu.");
display.setCurrent(alert, form);
}
}
if(c.getLabel()=="Nilai Tertinggi"){
lihatRecord();
}
else if (c == cmdHasil){
nama();
}
else if (c == cmdbalik){
UJIAN();
}else if (c == cmdKembali){
menuList();
}else{
List menu = (List) display.getCurrent();
switch (menu.getSelectedIndex()){
case 0:
UJIAN();
break;
case 1:
lihatRecord();
break;
case 2:
destroyApp(true);
}
}
}
public void tambahRecord(String id)
{
int a = Integer.parseInt(soal1.getString());
int b = Integer.parseInt(soal2.getString());
int c = Integer.parseInt(soal3.getString());
int d = Integer.parseInt(soal4.getString());
int e = Integer.parseInt(soal5.getString());
if(a == 5){
a=2;
}
else if(a != 5){
a=0;
}
if(b==4){
b=2;
}
else if(b != 4){
b=0;
}
if(c==12){
c=2;
}
else if(c != 12){
c=0;
}
if(d==5){
d=2;
}
else if(d !=2 ){
d=0;
}
if(e==10){
e=2;
}
else if(e != 10){
e=0;
}
Nakhir = a + b + c + d + e;
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);// tulis var pada dos
dos.writeInt(Nakhir);
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("Nilai Tertinggi");
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 = dis.readUTF();
//Integer Nakhir = dis.readUTF();
list.append(id + " = "+ Nakhir +" \n",null);// tampilkan
}
catch (IOException ioe) {ioe.printStackTrace();}
}
list.addCommand(cmdKembali);
list.setCommandListener(this);
display.setCurrent(list);
}
catch (InvalidRecordIDException invID) {invID.printStackTrace();}
catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();}
catch (RecordStoreException rse) {rse.printStackTrace();}
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar