Rabu, 26 Maret 2014

Membuat Aplikasi Phone Book Sederhana

Membuat Aplikasi Phone Book Sederhana


Berikut Codingnya

ProjectName : PhoneBook
ClassMIDLet : BukuTelepon


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

public class BukuTelepon extends MIDlet implements CommandListener
{
private Display tampil;
private Form form;
private TextField txtNama,txtEmail,txtNoTelp;
private Command comSimpan,comKembali,comTampil,comUpdate,comHapus;
private int i = 0;

public BukuTelepon()
{

}

public void startApp()
{
tampil = Display.getDisplay(this);
form = new Form("Aplikasi Buku Telepon Suka - Suka");
txtNama = new TextField("Nama : ","",20,TextField.ANY); //String label,String text,int maxSize,Input Type
txtEmail = new TextField("Email : ","",30,TextField.ANY);
txtNoTelp = new TextField("No.Telp : ","",15,TextField.ANY);
comSimpan = new Command("Simpan",Command.OK,1);
comTampil = new Command("Tampil",Command.OK,2);
comUpdate = new Command("Update",Command.OK,3);
comHapus = new Command("Hapus",Command.OK,4);
comKembali = new Command("Kembali",Command.BACK,1);
form.append(txtNama); //Menambahkan textfield nama ke dalam form utama
form.append(txtEmail); //Menambahkan textfield email ke dalam form utama
form.append(txtNoTelp); //Menambahkan textfield no telepon ke dalam form utama
form.addCommand(comSimpan);
form.addCommand(comTampil);
form.addCommand(comUpdate);
form.addCommand(comHapus);
form.addCommand(comKembali);
form.setCommandListener(this);
tampil.setCurrent(form);

}
//Membuat method simpan
public void Simpan(String input1,String input2,String input3)
{
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(input1);
dos.writeUTF(input2);
dos.writeUTF(input3);
dos.flush();
RecordStore rs = RecordStore.openRecordStore("bukutelepon",true); //bukutelepon sebagai nama database dalam konsep RMS
rs.addRecord(bos.toByteArray(),0,bos.toByteArray().length);
//Menutup koneksi
rs.closeRecordStore();
bos.close();
dos.close();
tampil.setCurrent(new Alert("Informasi","Data berhasil disimpan",null,AlertType.INFO));
}
catch(Exception ex)
{
ex.printStackTrace();
tampil.setCurrent(new Alert("Error","Data gagal disimpan",null,AlertType.ERROR));
}
}

//Membuat method update
public void Update(String input1,String input2,String input3,int i)
{
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(input1);
dos.writeUTF(input2);
dos.writeUTF(input3);
dos.flush();
RecordStore rs = RecordStore.openRecordStore("bukutelepon",true);
rs.setRecord(i,bos.toByteArray(),0,bos.toByteArray().length);
rs.closeRecordStore();
bos.close();
dos.close();
tampil.setCurrent(new Alert("Informasi","Data berhasil diupdate",null,AlertType.INFO));
}
catch(Exception ex)
{
ex.printStackTrace();
tampil.setCurrent(new Alert("Informasi","Data gagal disimpan",null,AlertType.ERROR));
}

}
//Membuat method Hapus
public void Hapus(int i)
{
try
{
RecordStore rs = RecordStore.openRecordStore("bukutelepon",true);
rs.deleteRecord(i);
rs.closeRecordStore();
tampil.setCurrent(new Alert("Informasi","Data berhasil dihapus",null,AlertType.INFO));
}
catch(Exception ex)
{
ex.printStackTrace();
tampil.setCurrent(new Alert("Error","Data gagal dihapus",null,AlertType.ERROR));
}
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}

public void commandAction(Command cmd,Displayable display)
{
if(cmd == comKembali)
{
destroyApp(true);
}
else if(cmd == comSimpan)
{
//Method simpan
Simpan(txtNama.getString(),txtEmail.getString(),txtNoTelp.getString());
txtNama.setString("");
txtEmail.setString("");
txtNoTelp.setString("");
}
//Menampilkan data
else if(cmd == comTampil)
{
try
{
RecordStore rs = RecordStore.openRecordStore("bukutelepon",true);
int a;
for(a=1;a<=rs.getNumRecords();a++)
{
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(a));
DataInputStream dis = new DataInputStream(bis);
String nama = dis.readUTF();
String email = dis.readUTF();
String telp = dis.readUTF();
bis.close();
dis.close();
System.out.println(nama + "\t" + email + "\t" + telp + "\t" + txtNama.getString().equals(nama));
if(txtNama.getString().equals(nama))
{
txtEmail.setString(email);
txtNoTelp.setString(telp);
i = a;
break;
}
}
rs.closeRecordStore();
}
catch(Exception ex)
{
ex.printStackTrace();
tampil.setCurrent(new Alert("Error",ex.getMessage(),null,AlertType.ERROR));
}
}
else if(cmd == comUpdate)
{
if(i>0)
{
//Method Update
Update(txtNama.getString(),txtEmail.getString(),txtNoTelp.getString(),i);
txtNama.setString("");
txtEmail.setString("");
txtNoTelp.setString("");
}
else
{
tampil.setCurrent(new Alert("Error","Data belum dipilih",null,AlertType.ERROR));
}
}
else if(cmd == comHapus)
{
if(i>0)
{
//Method Hapus
Hapus(i);
txtNama.setString("");
txtEmail.setString("");
txtNoTelp.setString("");
}
else
{
tampil.setCurrent(new Alert("Error","Data belum dipilih",null,AlertType.ERROR));
}
}
}
}