Contoh Program Java Inheritance(Pewarisan) dengan perulangan
package tugas_tambahan;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TUGAS_TAMBAHAN {
public static void main(String[] args) {
String [] menumakan = {"Ayam Goreng","Ayam Bakar","Ikan Goreng","Ikan Bakar","Bebek Goreng","Bebek Bakar"};
String [] menuminum = {"Jus Melon","Jus Semangka","Jus Alpukat","Es Teh","Es Jeruk","Air Mineral"};
double [] hargamakan = {15000,16000,12000,14000,20000,22000};
double [] hargaminum = {10000,12000,15000,4000,6000,3000};
String [] pesenmakan = new String[20];
String [] pesenminum = new String[20];
double [] hrgmkn = new double[20];
double [] hrgmnm = new double[20];
double [] jml = new double[20];
double [] jml1 = new double[20];
double disk=0,pemb,totalmakan = 0,totalminum = 0,total= 0,bayar=0,pot1=0,pot2=0,pot=0,pot3=0;
String inp;
int pilihmenu,pilih,ya,i,j=0,n=0;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
do{
System.out.println("\t\t\tMENU MAKAN DAN MINUM DI 1RESTORAN CEPAT SAJI");
System.out.println("==============================");
System.out.println("1. MAKANAN");
System.out.println("2. MINUMAN");
System.out.println("0. KELUAR");
System.out.println("==============================");
System.out.print("Masukkan pilihan anda : ");
pilihmenu = Integer.parseInt(inp=input.readLine());
switch (pilihmenu){
case 1:
do {
System.out.println("MAKANAN");
System.out.println("================================");
for (i=0;i<menumakan.length;i++){
System.out.println(i+". "+menumakan[i]+"\tRp. "+hargamakan[i]);
}
System.out.println("================================");
System.out.print("Masukkan Pilihan Menu : ");
pilih=Integer.parseInt(inp=input.readLine());
System.out.print("Masukkan Jumlah Pembelian : ");
jml[n]=pemb=Double.parseDouble(inp=input.readLine());
totalmakan = totalmakan+(hargamakan[pilih]*jml[n]);
pot1= pot1+jml[n];
pesenmakan[n]=menumakan[pilih];
hrgmkn[n]=hargamakan[pilih];
System.out.print("Apakah Anda Mau Pesan Lagi 1.Ya/0.Tidak :");
ya=Integer.parseInt(inp=input.readLine());
n++;
} while (ya !=0);
break;
case 2:
do {
System.out.println("MINUMAN");
System.out.println("========");
for (i=0;i<menuminum.length;i++){
System.out.println(i+". "+menuminum[i]+"\tRp. "+hargaminum[i]);
}
System.out.println("================================");
System.out.print("Masukkan Pilihan Menu : ");
pilih=Integer.parseInt(inp=input.readLine());
System.out.print("Masukkan Jumlah Pembelian : ");
jml1[j]=pemb=Double.parseDouble(inp=input.readLine());
totalminum = totalminum+(hargaminum[pilih]*jml1[j]);
pot2 = pot2+jml1[j];
pesenminum[j]=menuminum[pilih];
hrgmnm[j]=hargaminum[pilih];
System.out.print("Apakah Mau Pesan Lagi 1.Ya/0.Tidak : ");
ya=Integer.parseInt(inp=input.readLine());
j++;
} while (ya !=0);
break;
}
}while (pilihmenu !=0);
} catch (Exception e) {
}
total = totalmakan+totalminum;
pot = pot1+pot2;
if(total >=200000){
disk = (total*25)/100;
bayar = total-disk;
}else if ((total>=150000)&&(total<200000)){
disk = (total*15)/100;
bayar = total -disk;
}else if ((total>=50000)&&(total<150000)){
disk = (total*5)/100;
bayar = total - disk;
}
if (pot >=7){
pot3=20000;
bayar = bayar -pot3;
}else if ((pot>=4)&&(pot<7)){
pot3=10000;
bayar = bayar -pot3;
}
System.out.println("PEMESANAN ANDA");
if (n>0){
System.out.println("===========");
for (i=0;i<n;i++){
System.out.println((i+1)+". "+pesenmakan[i]+"\tJmlah = "+jml[i]+"\tRp. "+hrgmkn[i]);
}
System.out.println("===============================");
System.out.println("TOTAL = "+totalmakan);
}
System.out.println("\n");
if (j>0){
System.out.println("===============================");
for (i=0;i<j;i++){
System.out.println((i+1)+". "+pesenminum[i]+"\tJumlah = "+jml1[i]+"\tRp. "+hrgmnm[i]);
}
System.out.println("===============================");
System.out.println("TOTAL = "+totalminum);
}
System.out.println("====================================");
System.out.println("JUMLAH PEMBELIAN = "+total);
System.out.println("DISKON = "+disk);
System.out.println("POTONGAN = "+pot3);
System.out.println("====================================");
System.out.println("TOTAL YANG HARUS DI BAYAR = "+bayar);
}
}