latihan method
method
nama: Syuraini Noor Chamsyah
npm: 22082010033
paralel kelas: A
mata kuliah: bahasa pemrograman
dosen pengampu: Nur Cahyo Wibowo S.kom,M.kom.
source code:
package methodbp;
import java.util.Scanner;
public class Methodbp {
//variabel global
static int harga[] = new int[18];
static String[] sort(String[] menu) {
for (int i = 0; i < menu.length; i++) {
for (int j = 0; j < menu.length - 1; j++) {
if (menu[j].compareToIgnoreCase(menu[j + 1]) > 0) {
String t = menu[j];
menu[j] = menu[j + 1];
menu[j + 1] = t;
int h = harga[j];
harga[j] = harga[j + 1];
harga[j + 1] = h;
}
}
}
return menu;
}
static String[][] search(String[] menu, int harga[], String cari) {
String hasil[][] = new String[10][4];
int idx = 0;
for (int i = 0; i < menu.length; i++) {
if (menu[i].indexOf(cari) >= 0) {
hasil[idx][0] = menu[i];
hasil[idx][1] = String.valueOf(harga[i]);
hasil[idx][2] = String.valueOf(harga[i + 3]);
hasil[idx][3] = String.valueOf(harga[i + 9]);
idx++;
}
}
return hasil;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String makanan[] = new String[10];
String minuman[] = new String[5];
String Snack[] = new String[3];
// int harga[]= new Int[3];
//inisialisasi array
Snack[0] = "oreo";
harga[0] = 3000;
Snack[1] = "biskuat";
harga[1] = 2000;
Snack[2] = "nabati";
harga[2] = 1000;
minuman[0] = "Es teh";
harga[3] = 3000;
minuman[1] = "Es jeruk";
harga[4] = 6000;
minuman[2] = "air putih ";
harga[5] = 2000;
minuman[3] = "teh hangat";
harga[6] = 3000;
minuman[4] = "jeruk hangat";
harga[7] = 5000;
makanan[0] = "sate ayam";
harga[8] = 12000;
makanan[1] = "sate sapi";
harga[9] = 15000;
makanan[2] = "sate kambing";
harga[10] = 20000;
makanan[3] = "soto madura";
harga[11] = 12000;
makanan[4] = "soto lamongan";
harga[12] = 14000;
makanan[5] = "gado gado";
harga[13] = 10000;
makanan[6] = "rawon";
harga[14] = 13000;
makanan[7] = "bakso";
harga[15] = 8000;
makanan[8] = "pecel";
harga[16] = 7000;
makanan[9] = "penyetan";
harga[17] = 9000;
int pilih = 0;
do {
System.out.println("MENU");
System.out.println("1.makanan");
System.out.println("2.minuman");
System.out.println("3. snack");
System.out.println("4.exit");
System.out.println("pilih= ");
pilih = sc.nextInt();
switch (pilih) {
case 1:
System.out.println("1.Daftar makanan");
System.out.println("2. cari makanan");
System.out.println("3. Pilih= ");
int pilih2 = sc.nextInt();
switch (pilih2) {
case 1:
String[] urut = sort(makanan);
System.out.println("Daftar menu");
for (int i = 0; i < 10; i++) {
System.out.println(i + 1 + "." + urut[i]);
}
break;
case 2:
System.out.print("Cari= ");
String m = sc.next();
String hasilcari[][] = search(makanan, harga, m);
System.out.println("Hasil pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0] != null) {
System.out.println((i + 1) + "." + hasilcari[i][0] + " ");
System.out.println(Integer.parseInt(hasilcari[i][3]));
}
}
break;
}
break;
case 2:
System.out.println("1.Daftar minuman");
System.out.println("2.Cari minuman");
System.out.println("3. Pilih= ");
pilih2 = sc.nextInt();
switch (pilih2) {
case 1:
String[] urut = sort(minuman);
System.out.println("Daftar menu");
for (int i = 0; i < urut.length; i++) {
System.out.println(i + 1 + "." + urut[i]);
}
System.out.println("");
break;
case 2:
System.out.print("Cari= ");
String m = sc.next();
String hasilcari[][] = search(minuman, harga, m);
System.out.println("Hasil pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0] != null) {
System.out.println((i + 1) + "." + hasilcari[i][0] + " ");
System.out.println(Integer.parseInt(hasilcari[i][2]));
}
}
break;
}
break;
case 3:
System.out.println("1.Daftar snack");
System.out.println("2.Cari snack");
System.out.println("3. Pilih= ");
pilih2 = sc.nextInt();
switch (pilih2) {
case 1:
String[] urut = sort(Snack);
System.out.println("Daftar menu");
for (int i = 0; i < urut.length; i++) {
System.out.println(i + 1 + "." + urut[i]);
}
System.out.println("");
break;
case 2:
System.out.print("Cari= ");
String s = sc.next();
String hasilcari[][] = search(Snack, harga, s);
System.out.println("Hasil pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0] != null) {
System.out.println((i + 1) + "." + hasilcari[i][0] + " ");
System.out.println(Integer.parseInt(hasilcari[i][1]));
}
}
break;
}
break;
case 4:
System.out.println("Terimakasih");
break;
}
} while (pilih != 4);
}
}
Komentar
Posting Komentar