Forum Latihan Pencarian Data
Source Code :
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
//inisialisasi array
String[] makanan = new String[10];
int[] harga = new int[10];
makanan[0] = "Bakso "; harga[0] = 10000;
makanan[1] = "Bakso Goreng "; harga[1] = 5000;
makanan[2] = "Gado - Gado "; harga[2] = 12000;
makanan[3] = "Rujak Cingur "; harga[3] = 13000;
makanan[4] = "Mie Ayam "; harga[4] = 10000;
makanan[5] = "Mie Pangsit "; harga[5] = 11000;
makanan[6] = "Mie Goreng "; harga[6] = 12000;
makanan[7] = "Ayam Bakar "; harga[7] = 30000;
makanan[8] = "Nasi Goreng "; harga[8] = 12000;
makanan[9] = "Kwetiau "; harga[9] = 13000;
int pilih = 0;
String beli[] = new String[5];
int hargaBeli[] = new int[5];
int jumlahBeli = 0;
do{
System.out.println("");
System.out.println("======= SELAMAT DATANG DI WARUNG MAKAN ======");
System.out.println("|| ||");
System.out.println("|| 1. Daftar Makanan ||");
System.out.println("|| 2. Cari Makanan Berdasarkan Nama ||");
System.out.println("|| 3. Cari Makanan Berdasarkan Harga ||");
System.out.println("|| 4. Selesai ||");
System.out.println("|| ||");
System.out.println("=============================================");
System.out.print(" Pilih = ");
pilih = input.nextInt();
switch(pilih){
case 1:
//urutkan berdasarkan nama
//menggunakan bubble sort
for(int i=0; i<10; i++){
for(int j=0; j<9; j++){
if(makanan[j].compareToIgnoreCase(makanan[j+1]) > 0){
String temp_makanan = makanan[j];
makanan[j] = makanan[j+1];
makanan[j+1] = temp_makanan;
int temp_harga = harga[j];
harga[j] = harga[j+1];
harga[j+1] = temp_harga;
}
}
}
System.out.println("");
System.out.println("================= MENU KAMI =================");
for(int i=0; i<10; i++){
System.out.println(makanan[i]+""+harga[i]);
}
break;
case 2:
System.out.println("");
System.out.println("============= CARI NAMA MAKANAN =============");
System.out.print("Cari Nama Makanan = ");
String cari = input.next();
String cariMakan[] = new String[10];
int cariHarga[] = new int[10];
int index = 0;
for(int i=0; i<makanan.length; i++){
if(makanan[i].indexOf(cari) >= 0){
cariMakan[index] = makanan[i];
cariHarga[index] = harga[i];
index++;
System.out.println(index+". "+makanan[i]+""+harga[i]);
}
}
//transaksi
System.out.print("Pilih Menu = ");
int pilihMenu = input.nextInt();
beli[jumlahBeli] = cariMakan[pilihMenu-1];
hargaBeli[jumlahBeli] = cariHarga[pilihMenu-1];
jumlahBeli++;
break;
case 3:
System.out.println("");
System.out.println("========== CARI RANGE HARGA MAKANAN =========");
System.out.print("Input Harga Minimal = ");
int hargaAwal = input.nextInt();
System.out.print("Input Harga Maksimal = ");
int hargaAkhir = input.nextInt();
String carimakan2[] = new String[10];
int cariharga2[] = new int[10];
int index2 = 0;
for(int i=0; i<10; i++){
if(harga[i] >= hargaAwal && harga[i] <= hargaAkhir){
carimakan2[index2] = makanan[i];
cariharga2[index2] = harga[i];
index2++;
System.out.println(index2+". "+makanan[i]+""+harga[i]);
}
}
//transaksi
System.out.print("Pilih Menu = ");
int pilihMenu2 = input.nextInt();
beli[jumlahBeli] = carimakan2[pilihMenu2-1];
hargaBeli[jumlahBeli] = cariharga2[pilihMenu2-1];
jumlahBeli++;
break;
case 4:
System.out.println("");
System.out.println("Terimakasih Telah Memesan Di Resto Kami...\n");
}
}while(pilih != 4);
//cetak daftar beli
System.out.println("");
System.out.println("================ DAFTAR BELI ================");
for(int i=0; i<jumlahBeli; i++){
System.out.println(beli[i]+" ["+hargaBeli[i]+"]");
}
}
Komentar
Posting Komentar