method 22082010014 Risma Paramesti
package latihanmethod;
import java.util.Scanner;
public class Latihanmethod {
//variabel global
static int harga[]=new int[10];
static int harga2[]=new int[5];
static int harga3[]=new int[5];
static int hitungharga(int hargasatuan, int jumlah) //header
{
//body
int harga=hargasatuan*jumlah;
return harga;
}
static String[] sort(String[] menu, int[]harga){
//String hasil[]= new String [20];
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][2];
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]);
idx++;
}
}
return hasil;
}
static String[][] search(String[]minuman, String cari, int harga2[]){
String[][]hasil=new String[5][2];
int idx=0;
for (int i = 0; i < minuman.length; i++) {
if (minuman[idx].indexOf(cari)>=0) {
hasil[idx][0]=minuman[i];
hasil[idx][1]=String.valueOf(harga2[i]);
idx++;
}
}
return hasil;
}
static String[][] search(int harga3[], String cari, String[] snack){
String[][]hasil=new String[5][2];
int idx=0;
for (int i = 0; i < snack.length; i++) {
if (snack[idx].indexOf(cari)>=0) {
hasil[idx][0]=snack[i];
hasil[idx][1]=String.valueOf(harga3[i]);
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[5];
//int harga[] = new int [20];
//inilisasi array
snack[0]="Oreo"; harga3[0]=8000;
snack[1]="Biskuat"; harga3[1]=5000;
snack[2]="Pizza Mini"; harga3[2]=18000;
snack[3]="Cookies"; harga3[3]=10000;
snack[4]="Donat"; harga3[4]=8000;
minuman[0]="Es Teh"; harga2[0]=5000;
minuman[1]="Kopi"; harga2[1]=10000;
minuman[2]="Es Jeruk"; harga2[2]=5000;
minuman[3]="Es Campur"; harga2[3]=10000;
minuman[4]="Es Oyen"; harga2[4]=12000;
makanan[0]="Ayam Geprek";harga[0]=12000;
makanan[1]="Ayam Krispi";harga[1]=10000;
makanan[2]="Nasi Kuning";harga[2]=8000;
makanan[3]="Nasi Goreng";harga[3]=12000;
makanan[4]="Bebek Goreng";harga[4]=18000;
makanan[5]="Nasi Padang";harga[5]=16000;
makanan[6]="Mie Goreng";harga[6]=12000;
makanan[7]="Gado-Gado";harga[7]=10000;
makanan[8]="Sate Ayam";harga[8]=15000;
makanan[9]="Mie Ayam Bakso";harga[9]=12000;
int pilih=0;
do{
System.out.println("MENU");
System.out.println("1. Menu Makanan");
System.out.println("2. Menu Minuman");
System.out.println("3. Menu Snack");
System.out.println("4. Exit");
System.out.print("Pilih : ");
pilih = sc.nextInt();
switch(pilih){
case 1:
System.out.println("1. Daftar Makanan");
System.out.println("2. Cari Makanan");
System.out.print("Pilih : ");
int pilih2=sc.nextInt();
switch(pilih2){
case 1:
String[] urut=sort(makanan, harga);
System.out.println("Daftar Menu");
for (int i = 0; i < 10; i++) {
System.out.println((i+1)+". "+urut[i]+" "+harga[i]);
}
System.out.println("");
break;
case 2:
System.out.print("Cari : ");
String c = sc.next();
String hasilcari[][] = search(makanan, harga, c);
System.out.println("");
System.out.println("Hasil Pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0]!=null){
System.out.print((i+1)+". "+hasilcari[i][0]+" ");
System.out.println(Integer.parseInt(hasilcari[i][1]));
}
}
System.out.println("");
break;
}
break;
case 2:
System.out.println("1. Daftar Minuman");
System.out.println("2. Cari Minuman");
System.out.print("Pilih : ");
pilih2=sc.nextInt();
switch(pilih2){
case 1:
String[] urut=sort(minuman, harga2);
System.out.println("Daftar Menu");
for (int i = 0; i < urut.length; i++) {
System.out.println((i+1)+". "+urut[i]+" "+harga2[i]);
}
System.out.println("");
break;
case 2:
System.out.print("Cari : ");
String c = sc.next();
String hasilcari[][] = search(minuman, harga2, c);
System.out.println("");
System.out.println("Hasil Pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0]!=null){
System.out.print((i+1)+". "+hasilcari[i][0]+" ");
System.out.println(Integer.parseInt(hasilcari[i][1]));
}
}
System.out.println("");
break;
}
break;
case 3:
System.out.println("1. Daftar Snack");
System.out.println("2. Cari Snack");
System.out.print("Pilih : ");
pilih2=sc.nextInt();
switch(pilih2){
case 1:
String[] urut=sort(snack, harga3);
System.out.println("Daftar Menu");
for (int i = 0; i < urut.length; i++) {
System.out.println((i+1)+". "+urut[i]+" "+harga3[i]);
}
System.out.println("");
break;
case 2:
System.out.print("Cari : ");
String c = sc.next();
String hasilcari[][] = search(snack, harga3, c);
System.out.println("");
System.out.println("Hasil Pencarian");
for (int i = 0; i < hasilcari.length; i++) {
if (hasilcari[i][0]!=null){
System.out.print((i+1)+". "+hasilcari[i][0]+" ");
System.out.println(Integer.parseInt(hasilcari[i][1]));
}
}
System.out.println("");
break;
}
break;
case 4:
System.out.println("");
System.out.println("Terima kasih");
break;
}
}while(pilih!=4);
}
}
Komentar
Posting Komentar