Latihan Pengurutan Data App Warung
Nama : Rayhan Qalby Ramadhan
NPM : 21082010008
Kelas : A082
Source Code :
package sorting;
import java.util.Scanner;
public class appCafe {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String menu[] = new String[5];
int harga[] = new int[5];
int pilih=0;
//Inisialisasi Data
menu[0]="Caramel Coffee"; harga[0]=12000;
menu[1]="Banana Milk"; harga[1]=10000;
menu[2]="Vanilla Coffee"; harga[2]=14000;
menu[3]="Taro Milk"; harga[3]=15000;
menu[4]="Chocolate"; harga[4]=11000;
//
System.out.println(" SELAMAT DATANG DI GETA KOFFIE ");
System.out.println(" JL. DHARMAWANGSA NO. 52 ");
System.out.println(" SURABAYA ");
System.out.println("====================================");
do{
System.out.println("\nMENU");
System.out.println("1. Daftar Minuman");
System.out.println("2. Urutkan Termurah");
System.out.println("3. Urutkan Termahal");
System.out.println("4. Selesai");
System.out.print("Pilih = ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.println("MENU \tHARGA");
for (int i = 0; i < 5; i++){
System.out.println(menu[i]+"\t"+harga[i]);
}
break;
case 2:
//memakai algoritma bubble sort
String menu2[] = new String[5];
menu2 = menu;
int harga2[] = new int[5];
harga2 = harga;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 4; j++){
if(harga2[j]>harga2[j+1]){
int temp=harga2[j];
harga2[j]=harga2[j+1];
harga2[j+1]=temp;
//mengurutkan nama
String s = menu2[j];
menu2[j] = menu2[j+1];
menu2[j+1] = s;
}
}
}
System.out.println("Harga Menu Termurah");
for (int i = 0; i < 5; i++){
System.out.println(menu2[i]+"\t"+harga2[i]);
}
break;
case 3:
//memakai algoritma selection sort
menu2 = menu;
harga2 = harga;
for (int i = 0; i < 4; i++){
for(int j = i+1; j < 5; j++){
if(harga2[i]<harga2[j]){
int temp=harga2[j];
harga2[j]=harga2[i];
harga2[i]=temp;
//mengurutkan nama menu makanan
String s = menu2[j];
menu2[j] = menu2[i];
menu2[i] = s;
}
}
}
System.out.println("Harga Menu Termahal");
for (int i = 0; i < 5; i++){
System.out.println(menu2[i]+"\t"+harga2[i]);
}
break;
case 4:
System.out.println("Terima Kasih...");
break;
default :
System.out.println("Ups..");
}
}while(pilih!=4);
}
}
Comments
Post a Comment