Forum Latihan Method
Source Code :
public class ProgMethod {
static int[] harga = new int[10];
static int hitungharga(int hargasatuan, int jumlah) {
int hargatotal = hargasatuan*jumlah;
return hargatotal;
}
static String[] sort(String[] menu){
//String hasil[] = new String[10];
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[][] pencarian(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;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] makanan = new String[5];
String[] minuman = new String[5];
String[] snack = new String[5];
//INISIALISASI ARRAY
//MAKANAN
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;
//MINUMAN
minuman[0] = "Es Teh "; harga[0] = 3000;
minuman[1] = "Teh Hangat "; harga[1] = 2500;
minuman[2] = "Soda Gembira "; harga[2] = 7000;
minuman[3] = "Es Teler "; harga[3] = 6000;
minuman[4] = "Air Mineral "; harga[4] = 3000;
//SNACK
snack[0] = "Nugget "; harga[0] = 12000;
snack[1] = "Kentang Goreng "; harga[1] = 10000;
snack[2] = "Tahu Crispy "; harga[2] = 6000;
snack[3] = "Keripik "; harga[3] = 4000;
snack[4] = "Kue Pukis "; harga[4] = 6000;
int pilih = 0;
do{
System.out.println("======= SELAMAT DATANG DI WARUNG MAKAN ======");
System.out.println("|| ||");
System.out.println("|| Mau Pesan Apa? ||");
System.out.println("|| 1. Makanan ||");
System.out.println("|| 2. Minuman ||");
System.out.println("|| 3. Snack ||");
System.out.println("|| 4. Selesai ||");
System.out.println("|| ||");
System.out.println("=============================================");
System.out.println("");
System.out.print(" Pilih = ");
pilih = sc.nextInt();
switch(pilih) {
case 1:
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Pilih Salah Satu ||");
System.out.println("|| 1. Daftar Makanan ||");
System.out.println("|| 2. Cari Makanan ||");
System.out.println("|| ||");
System.out.println("=============================================");
System.out.println("");
System.out.print(" Pilih = ");
int pilih2 = sc.nextInt();
switch(pilih2) {
case 1 :
String[] urut = sort(makanan);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Daftar Menu Makanan ||");
for(int i = 0; i < urut.length; i++) {
System.out.println("|| "+(i+1)+". "+urut[i]+ "Rp. " + harga[i]+" ||");
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
case 2 :
System.out.println("");
System.out.print(" Cari Makanan = ");
String c = sc.next();
String[][] hasilpencarian = pencarian(makanan, harga, c);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Hasil Pencarian Makanan ||");
for(int i=0; i< hasilpencarian.length; i++){
if(hasilpencarian[i][0] != null){
System.out.println("|| "+(i+1)+". "+hasilpencarian[i][0]+" Rp "+hasilpencarian[i][1]+" ||");
}
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
}
break;
case 2:
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Pilih Salah Satu ||");
System.out.println("|| 1. Daftar Minuman ||");
System.out.println("|| 2. Cari Minuman ||");
System.out.println("|| ||");
System.out.println("=============================================");
System.out.println("");
System.out.print(" Pilih = ");
pilih2 = sc.nextInt();
switch(pilih2) {
case 1 :
String[] urut = sort(minuman);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Daftar Menu Minuman ||");
for(int i = 0; i < urut.length; i++) {
System.out.println("|| "+(i+1)+". "+urut[i]+ "Rp. " + harga[i]+" ||");
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
case 2 :
System.out.println("");
System.out.print(" Cari Minuman = ");
String c = sc.next();
String[][] hasilpencarian = pencarian(minuman, harga, c);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Hasil Pencarian Minuman ||");
for(int i=0; i< hasilpencarian.length; i++){
if(hasilpencarian[i][0] != null){
System.out.println("|| "+(i+1)+". "+hasilpencarian[i][0]+" Rp "+hasilpencarian[i][1]+" ||");
}
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
}
break;
case 3:
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Pilih Salah Satu ||");
System.out.println("|| 1. Daftar Snack ||");
System.out.println("|| 2. Cari Snack ||");
System.out.println("|| ||");
System.out.println("=============================================");
System.out.println("");
System.out.print(" Pilih = ");
pilih2 = sc.nextInt();
switch(pilih2) {
case 1 :
String[] urut = sort(snack);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Daftar Menu Snack ||");
for(int i = 0; i < urut.length; i++) {
System.out.println("|| "+(i+1)+". "+urut[i]+ "Rp. " + harga[i]+" ||");
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
case 2 :
System.out.println("");
System.out.print(" Cari Snack = ");
String c = sc.next();
String[][] hasilpencarian = pencarian(snack, harga, c);
System.out.println("");
System.out.println("=============================================");
System.out.println("|| ||");
System.out.println("|| Hasil Pencarian Snack ||");
for(int i=0; i< hasilpencarian.length; i++){
if(hasilpencarian[i][0] != null){
System.out.println("|| "+(i+1)+". "+hasilpencarian[i][0]+" Rp "+hasilpencarian[i][1]+" ||");
}
}
System.out.println("|| ||");
System.out.println("=============================================");
break;
}
break;
case 4:
System.out.println("");
System.out.println("=========== T E R I M A K A S I H ===========");
System.out.println("");
break;
}
}while(pilih !=4);
}
}
Screenshot :
1. Output Pada Makanan
2. Output Pada Minuman
Komentar
Posting Komentar