Latihan Pencarian Data (21082010126)
package javaapplication9;
import java.util.Scanner;
public class Searching {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String makanan []= new String[10];
int harga[]= new int [10];
int hargatotal=0;
makanan[0]="Nasi Goreng"; harga[0]=16000;
makanan[1]="Nasi kebuli "; harga[1]=15000;
makanan[2]="Nasi Pecel "; harga[2]=8000;
makanan[3]="Soto Ayam "; harga[3]=12000;
makanan[4]="coto makasar"; harga[4]=14000;
makanan[5]="Soto betawi"; harga[5]=16000;
makanan[6]="Mie Ayam "; harga[6]=12000;
makanan[7]="Ketoprak "; harga[7]=11000;
makanan[8]="Sop Iga "; harga[8]=40000;
makanan[9]="Soto Ayam "; harga[9]=15000;
int pilih=0;
String belimakan[]= new String[5];
int beliharga[]= new int[5];
int jumlahbeli=0;
do{
System.out.println("\nDAFTAR MENU");
System.out.println("1. Daftar Makanan");
System.out.println("2. Cari Makanan");
System.out.println("3. Cari Berdasarkan Harga");
System.out.println("4. Selesai");
System.out.print("Pilih = ");
pilih = sc.nextInt();
switch(pilih){
case 1:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10-1; j++) {
if (makanan[j].compareToIgnoreCase(makanan[j+1])>0){
String t = makanan[j];
makanan[j] = makanan[j+1];
makanan[j+1] = t;
int h = harga[j];
harga[j]= harga[j+1];
harga[j+1] = h;
}
}
}
System.out.println("Pilihan Menu\tHarga");
for (int i = 0; i < 10; i++) {
System.out.println(makanan[i]+"\t"+harga[i]);
}
break;
case 2:
System.out.print("Cari = ");
String cari = sc.next();
String carimakan[] = new String[10];
int cariharga[] = new int[10];
int idx = 0;
System.out.println("Pilihan Menu\tHarga");
for (int i = 0; i < makanan.length; i++) {
if (makanan[i].indexOf(cari)>=0){
carimakan[idx] = makanan[i];
cariharga[idx] = harga[i];
idx++;
System.out.println(idx+". "+makanan[i]+"\t"+harga[i]);
}
}
System.out.print("Pilih Menu = ");
int pilih2 = sc.nextInt();
belimakan[jumlahbeli]=carimakan[pilih2-1];
beliharga[jumlahbeli]=cariharga[pilih2-1];
jumlahbeli++;
break;
case 3:
System.out.println("1.Harga < Rp. 15.000");
System.out.println("2.Harga > Rp. 15.000");
System.out.print("Pilih = ");
int pilih3 = sc.nextInt();
int cariharga2[] = new int[10];
String carimakan2[] = new String[10];
int idx2 = 0;
switch(pilih3){
case 1:
for (int i = 0; i < makanan.length; i++) {
if (harga[i]<=15000){
carimakan2[idx2] = makanan[i];
cariharga2[idx2] = harga[i];
idx2++;
System.out.println(idx2+". "+makanan[i]+" "+harga[i]);
}
}
break;
case 2:
for (int i = 0; i < makanan.length; i++) {
if (harga[i]>15000){
carimakan2[idx2] = makanan[i];
cariharga2[idx2] = harga[i];
idx2++;
System.out.println(idx2+". "+makanan[i]+" "+harga[i]);
}
}
break;
}
//transaksi
System.out.print("Pilih Menu = ");
int pilih4 = sc.nextInt();
belimakan[jumlahbeli]=carimakan2[pilih4-1];
beliharga[jumlahbeli]=cariharga2[pilih4-1];
jumlahbeli++;
break;
case 4:
System.out.println("");
break;
}
}while(pilih!=3);
System.out.println("Total Beli");
for (int i = 0; i < jumlahbeli; i++) {
System.out.println(belimakan[i]+" ["+beliharga[i]+"]");
hargatotal = hargatotal+beliharga[i];
}
System.out.println("Harga Total = Rp "+hargatotal);
}
}
Comments
Post a Comment