Pengurutan data 22082010014 RISMA PARAMESTI
package pengurutandata;
import java.util.Scanner;
public class Pengurutandata {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int harga[] = new int[6];
String menu[] = new String [6];
int pilih=0;
menu [0]= "Roti tawar";
harga[0]= 15000;
menu [1]= "Roti pandan";
harga[1]= 17000;
menu [2]= "Roti coklat";
harga[2]= 16000;
menu [3]= "Meises";
harga[3]= 9000;
menu [4]= "Telur ayam kampung";
harga[4]= 21000;
do{
System.out.println("------------------------------------------------");
System.out.println(" WARUNG KUE NIKMAT ");
System.out.println("------------------------------------------------");
System.out.println("1. Daftar Menu ");
System.out.println("2. Urut harga dari yang termurah");
System.out.println("3. Urut harga dari yang termahal");
System.out.println("4. Selesai");
System.out.print("Pilih : ");
pilih = sc.nextInt();
System.out.println(" ");
switch(pilih){
case 1:
System.out.println("----------------------------------------");
System.out.println(" DAFTAR MENU ");
System.out.println("----------------------------------------");
for (int i = 0; i < 5; i++) {
System.out.println(menu[i]+" Rp"+harga[i]);
}
System.out.println(" ");
break;
case 2:
System.out.println("----------------------------------------");
System.out.println(" URUT HARGA DARI YANG TERMURAH ");
System.out.println("----------------------------------------");
String menu2[]= new String [5];
menu2=menu;
int harga2[]= new int[5];
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;
String termurah = menu2[j];
menu2[j] = menu2[i];
menu2[i] = termurah;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(menu2[i]+" Rp"+harga[i]);
}
break;
case 3:
System.out.println("URUT HARGA DARI YANG TERMAHAL");
String menu3[]= new String [5];
menu3=menu;
int harga3[]= new int[5];
harga3=harga;
for (int i = 0; i < 4; i++) {
for (int j = i+1 ; j < 5; j++) {
if(harga3[i]<harga3[j]){
int temp = harga3[j];
harga3[j] = harga3[i];
harga3[i] = temp;
String termahal = menu3[j];
menu3[j] = menu3[i];
menu3[i] = termahal;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(menu3[i]+" Rp"+harga3[i]);
}
break;
case 4:
System.out.println("SELESAI");
System.out.println("TERIMA KASIH");
break;
}
}while(pilih!=4);
}
}
Komentar
Posting Komentar