Setelah menguasai penggunaan if else dan switch case maka berlanjut pada bab perulangan tunggal. Disini saya melengkapi source code warung sederhana dari bab percabangan ditambah dengan perintang perulangan tuggal do while.
import java.util.Scanner;
public class looping {
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
//deklarasi variabel
short menu,varian;
int porsi;
double harga = 0,diskon = 0,cash,kembali;
//looping
System.out.println("Warung Makan Sederhan");
do {
System.out.println("\nMenu :");
System.out.println("1. Sate");
System.out.println("2. Pecel");
System.out.println("3. Penyetan");
System.out.println("4. Selesai");
System.out.print("Pilih menu : ");
menu = sc.nextShort();
//sate
switch (menu) {
case 1 :
System.out.println("Varian sate :");
System.out.println("1. Ayam : Rp. 1.500/tusuk");
System.out.println("2. Kambing : Rp. 3.000/tusuk ");
System.out.print("Pilih varian : ");
varian = sc.nextShort();
if (varian==1){
System.out.print("Jumlah tusuk : ");
porsi = sc.nextInt();
harga = harga+porsi*1500;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
} else if (varian==2) {
System.out.print("Jumlah tusuk : ");
porsi = sc.nextInt();
harga = harga+porsi*3000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
} else {
System.out.println("Pemilihan varian salah");
}
break;
//pecel
case 2 :
System.out.println("Lauk yang tersedia : ");
System.out.println("1. Lauk ayam : Rp. 13.000/porsi");
System.out.println("2. Lauk empal : Rp. 15.000/porsi");
System.out.print("Pilih varian : ");
varian = sc.nextShort();
if (varian==1){
System.out.print("Jumlah porsi : ");
porsi = sc.nextInt();
harga = harga+porsi*13000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
} else if (varian==2) {
System.out.print("Jumlah porsi : ");
porsi = sc.nextInt();
harga = harga+porsi*15000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);;
} else {
System.out.println("Pemilihan varian salah");
}
break;
//penyetan
case 3 :
System.out.println("Lauk yang tersedia :");
System.out.println("1. Lauk tahu/tempe : Rp. 5.000/porsi");
System.out.println("2. Lauk telur : Rp. 7.000/porsi");
System.out.println("3. Lauk ayam : Rp. 10.000/porsi");
System.out.print("Pilih varian : ");
varian = sc.nextShort();
switch (varian){
case 1 :
System.out.print("Jumlah porsi : ");
porsi = sc.nextInt();
harga = harga+porsi*5000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
break;
case 2 :
System.out.print("Jumlah porsi : ");
porsi = sc.nextInt();
harga = harga+porsi*7000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
break;
case 3 :
System.out.print("Jumlah porsi : ");
porsi = sc.nextInt();
harga = harga+porsi*10000;
System.out.print("\nSubtotal = ");
System.out.printf("Rp %,.2f\n", harga);
break;
default :
System.out.println("Pemilihan varian salah");
} break;
case 4 :
System.out.println("Pesanan diterima");
break;
}
} while (menu!=4);
System.out.print("\nMemiliki kartu member Warung Sederhana [1. ya/2. Tidak] : ");
byte kartu = sc.nextByte();
System.out.println("\n----------------------------");
switch (kartu){
case 1 :
System.out.print("Subtotal : ");
System.out.printf("Rp %,.2f\n", harga);
System.out.println("Total diskon : 5%");
diskon = harga-harga*0.05;
System.out.print("Harga total : ");
System.out.printf("Rp %,.2f\n", diskon);
break;
case 2 :
System.out.print("Subtotal : ");
System.out.printf("Rp %,.2f\n", harga);
System.out.println("Total diskon : 0%");
diskon = harga;
System.out.print("Harga total : ");
System.out.printf("Rp %,.2f\n", harga);
}
System.out.print("Cash : Rp. ");
cash = sc.nextDouble();
kembali = cash-diskon; kembali = cash-harga;
if (cash==harga){
System.out.println(" uang pas ");
System.out.println("----------------------------");
}else if (cash==diskon){
System.out.println(" uang pas ");
System.out.println("----------------------------");
}
else { kembali = cash-diskon;
System.out.println("kembali : Rp. "+kembali);
System.out.println("----------------------------");
} System.out.println(" Terimakasih :)");
}
}