kali ini saya akan men-share cara membuat Aplikasi Restoran menggunakan Database SQLite. Berikut deskripsi Aplikasi beserta subsistem dan Source Code,
Nama App : Resto
Subsistem : 1. Data Makanan Dan Minuman
2. Data Pelayan
3. Data Meja
4. Mencatat Pesan
5. Bayar
1. Data Makanan Dan Minuman
- Buat Tampilan seperti dibawah ini : main.xml
SOURCE CODE main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/image" >
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="DATA MAKANAN and MINUMAN" android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20dp"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Kode"
android:textColor="#FFFF00"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" >
<EditText
android:id="@+id/edkode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" />
<Button
android:id="@+id/btCek" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Cek" />
</LinearLayout>
<TextView
android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Nama Makanan/Minuman" android:textColor="#000000"
android:textStyle="bold" />
<EditText
android:id="@+id/ednama"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_gravity="center">
<RadioButton
android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"
android:text="Makanan"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Minuman"
android:textColor="#000000" />
</RadioGroup>
<TextView
android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Harga"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="@+id/edharga" android:layout_width="match_parent" android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"
android:layout_margin="10dp" >
<Button
android:id="@+id/btSimpan" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Simpan" />
<Button
android:id="@+id/btEdit" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Edit" />
<Button
android:id="@+id/btHapus" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hapus" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="DATA MAKANAN and MINUMAN" android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20dp"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Kode"
android:textColor="#FFFF00"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" >
<EditText
android:id="@+id/edkode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" />
<Button
android:id="@+id/btCek" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Cek" />
</LinearLayout>
<TextView
android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Nama Makanan/Minuman" android:textColor="#000000"
android:textStyle="bold" />
<EditText
android:id="@+id/ednama"
android:layout_width="match_parent" android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_gravity="center">
<RadioButton
android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"
android:text="Makanan"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Minuman"
android:textColor="#000000" />
</RadioGroup>
<TextView
android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Harga"
android:textColor="#FF0000"
android:textStyle="bold" />
<EditText
android:id="@+id/edharga" android:layout_width="match_parent" android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"
android:layout_margin="10dp" >
<Button
android:id="@+id/btSimpan" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Simpan" />
<Button
android:id="@+id/btEdit" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Edit" />
<Button
android:id="@+id/btHapus" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hapus" />
</LinearLayout>
</LinearLayout>
Buat class java baru dengan nama "database.java", class ini digunakan untuk pembuatan database dan tabel. berikut SOURCE CODE database.java:
package resto.com;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class database extends SQLiteOpenHelper {
// code mendeklarasikan nama database
private static final String DATABASE_NAME = "resto";
// deklarasi Field
public static final String Kode = "Kode";
public static final String Nama = "NamaMakMin";
public static final String Jenis = "Jenis";
public static final String Harga = "Harga";
public database(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
// Tabel tb_MakMin
public void createTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE if not exists tb_MakMin (Kode text PRIMARY KEY, Nama TEXT, " +
"Jenis TEXT, Harga integer)");
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Di class main java "dataMakMinAct.java" SOURCE CODE-nya:
package resto.com;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class dataMakMinAct extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private SQLiteDatabase db = null;
private Cursor dicCursor = null;
private RadioGroup grup;
private EditText kode, nama, harga;
private RadioButton rMa, rMi;
private Button btnSearch, simpan, edit, hapus;
private database dataSQL = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Membuat objek dari class DatabaseSQLite
dataSQL = new database(this, null, null, 0);
db = dataSQL.getWritableDatabase();
// Menjalankan method yang ada di class DatabaseSQLite
dataSQL.createTable(db);
setContentView(R.layout.main);
rMa = (RadioButton)findViewById(R.id.radio0);
rMi = (RadioButton)findViewById(R.id.radio1);
kode = (EditText) findViewById(R.id.edkode);
nama = (EditText) findViewById(R.id.ednama);
harga = (EditText) findViewById(R.id.edharga);
grup = (RadioGroup) findViewById(R.id.radioGroup1);
grup.clearCheck();
simpan = (Button) findViewById(R.id.btSimpan);
simpan.setOnClickListener(this);
btnSearch = (Button) findViewById(R.id.btCek);
btnSearch.setOnClickListener(this);
edit = (Button) findViewById(R.id.btEdit);
edit.setOnClickListener(this);
hapus = (Button) findViewById(R.id.btHapus);
hapus.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String kode1 = kode.getText().toString();
String nama1 = nama.getText().toString();
String jenis ="";
if (rMa.isChecked()){
jenis="Makanan";
}else if (rMi.isChecked()){
jenis="Minuman";
}
String harga1 = harga.getText().toString();
switch (arg0.getId()) {
case R.id.btSimpan:
if (kode1.equals("") || nama1.equals("") || harga.equals("")) {
Toast.makeText(this, "Data masih kosong", Toast.LENGTH_SHORT)
.show();
} else {
db.beginTransaction();
try {
db.execSQL("insert into tb_MakMin (Kode,Nama,Jenis,Harga) values('"
+ kode1
+ "','"
+ nama1
+ "','"
+ jenis
+ "','"
+ harga1 + "')");
db.setTransactionSuccessful();
Toast.makeText(this, "Simpan Berhasil", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
db.endTransaction();
kode.setText("");
nama.setText("");
grup.clearCheck();
harga.setText("");
}
break;
case R.id.btEdit:
if (kode1.equals("")) {
Toast.makeText(this, "Kode masih kosong", Toast.LENGTH_SHORT)
.show();
} else {
db.beginTransaction();
try {
db.execSQL("update tb_MakMin set Nama='" + nama1
+ "',Jenis='" + jenis + "', Harga='" + harga1
+ "' where Kode='" + kode1 + "'");
db.setTransactionSuccessful();
Toast.makeText(this, "Edit Berhasil", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
db.endTransaction();
kode.setText("");
nama.setText("");
grup.clearCheck();
harga.setText("");
}
break;
case R.id.btHapus:
if (kode1.equals("")) {
Toast.makeText(this, "Kode masih kosong", Toast.LENGTH_SHORT)
.show();
} else {
db.beginTransaction();
try {
db.execSQL("delete from tb_MakMin where Kode='" + kode1
+ "'");
db.setTransactionSuccessful();
Toast.makeText(this, "Hapus Berhasil", Toast.LENGTH_SHORT)
.show();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
db.endTransaction();
kode.setText("");
nama.setText("");
grup.clearCheck();
harga.setText("");
}
break;
case R.id.btCek:
db.beginTransaction();
try {
dicCursor = db.rawQuery("select * from tb_MakMin where Kode= '"
+ kode1 + "'", null);
if (dicCursor.moveToNext()) {
nama.setText(dicCursor.getString(1));
harga.setText(dicCursor.getString(3));
if(dicCursor.getString(2)=="Makanan"){
rMa.setChecked(true);
}else if(dicCursor.getString(2)=="Minuman"){
rMi.setChecked(true);
}
Toast.makeText(this, "Data Sudah Ada", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(this, "Data Belum ADA", Toast.LENGTH_SHORT)
.show();
}
db.setTransactionSuccessful();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
db.endTransaction();
break;
default:
break;
}
}
}
SEMOGA BERMANFAAT
-Giat Berilmu-
jika ada pertanyaan silahkan tinggalkan komentar kawan ☺
arum ini tombol cek nya yg radiobutton kog gag tampil ???
BalasHapuscoba di cek kembli mbak variable nya :) mungkin ada yang kurang
HapusMbak arum. Kok tombol cek, simpan, ubah, hapus kok nggak berfungsi.
Hapusudah,, log yg lainx udh mncul smw. tinggal radiobuttonx jha...
BalasHapusArum yang data memesan kok ga ada source code nya
BalasHapusMemang masih blm lengkap maaf, blm sempet melanjutkan msh sibuk d dunia nyata :)
Hapuskapan dilengkapi itu menurut sya kurng lengkap
BalasHapusiya mohon maaf sebelumnya terima kasih atas kunjungannya, masih blm sempat melengkapi
Hapusmba arum kapan di lengkapi nya?
BalasHapus