public class cPengunjung {
private String nama;
private String alamat;
cPengunjung(String n, String a){
nama=n; alamat=a;
System.out.println("Object pengunjung dibuat...");
}
public String getNama(){
return nama;
}
public String getAlamat(){
return alamat;
}
}
public class cMakanan {
private String nama;
private double harga;
cMakanan(String n, double h){
nama=n; harga=h;
System.out.println("Object makanan dibuat...");
}
public String getNama(){
return nama;
}
public double getHarga(){
return harga;
}
}
public class cPembelian {
private cPengunjung pgj;
private cMakanan[] mkn;
private int jmkn, maksmkn;
cPembelian(cPengunjung p, int j){
pgj=p; maksmkn=j; jmkn=0;
mkn=new cMakanan[j];
System.out.println("Object pembelian dibuat...");
}
public void tambahMakanan(cMakanan m){
if(jmkn<maksmkn){
mkn[jmkn]=m;
jmkn++;
System.out.println("Tambah sukses...");
}
else System.out.println("Kapasitas penuh!");
}
public void hapusMakanan(String nama){
if(jmkn>0){
//cari nama di array makanan
boolean ada=false;
for (int i = 0; i < jmkn; i++) {
if(mkn[i].getNama().equalsIgnoreCase(nama)){
ada=true;
mkn[i]=null;
//geser maju elemen belakang
for (int j = i; j < jmkn; j++) {
if(j==jmkn-1)
mkn[j]=null;
else
mkn[j]=mkn[j+1];
}
jmkn--;
System.out.println("Hapus sukses...");
break;
}
}
if(ada==false){
System.out.println("Makanan tidak ada!");
}
}
else System.out.println("Makanan masih kosong!");
}
public String ToString(){
String t="";
t=t+"Daftar Pembelian ["+pgj.getNama()+"]\n";
for (int i = 0; i < jmkn; i++) {
t=t+mkn[i].getNama()+"\t";
t=t+mkn[i].getHarga()+"\n";
}
return t;
}
}
import java.util.Scanner;
public class appWarung {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
cPembelian pb=null;
cPengunjung p = new cPengunjung("Budi","Surabaya");
cMakanan m[] = new cMakanan[5];
m[0] = new cMakanan("Bakso",20000);
m[1] = new cMakanan("Sate",25000);
m[2] = new cMakanan("Soto",15000);
m[3] = new cMakanan("Nasgor",30000);
m[4] = new cMakanan("Siomay",15000);
int jmk=5;
int pilih=0;
do{
System.out.println("\nMENU WARUNG");
System.out.println("1. Makanan");
System.out.println("2. Pengunjung");
System.out.println("3. Pembelian");
System.out.println("4. Exit");
System.out.print("Pilih = ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.println("Daftar Makanan");
for (int i = 0; i < jmk; i++) {
System.out.println(m[i].getNama()+"\t"+m[i].getHarga());
}
break;
case 2:
System.out.println("Data Pengunjung");
System.out.println(p.getNama()+"\t"+p.getAlamat());
break;
case 3:
System.out.println("Sub Menu Pembelian");
System.out.println("1. Buat Baru");
System.out.println("2. Tambah Makanan");
System.out.println("3. Hapus Makanan");
System.out.println("4. Lihat Pembelian");
System.out.print("Pilih = ");
int pilih2 = sc.nextInt();
switch(pilih2){
case 1:
System.out.print("Jumlah Makanan = ");
int j=sc.nextInt();
pb = new cPembelian(p,j);
System.out.println("Pembuatan sukses...");
break;
case 2:
System.out.print("Masukkan makanan = ");
String mk=sc.next();
//validasi nama makanan ke array makanan di MAIN
boolean ada=false;
for (int i = 0; i < jmk; i++) {
if(m[i].getNama().equalsIgnoreCase(mk)){
ada=true;
if(pb!=null)
pb.tambahMakanan(m[i]);
else System.out.println("Pembelian belum ada!");
break;
}
}
if(ada==false){
System.out.println("Makanan tidak ada!");
}
break;
case 3:
if(pb!=null){
System.out.print("Masukkan nama = ");
mk=sc.next();
pb.hapusMakanan(mk);
}
else System.out.println("Pembelian masih kosong!");
break;
case 4:
if(pb!=null){
System.out.println("Data Pembelian");
System.out.println(pb.ToString());
}
else System.out.println("Pembelian masih kosong!");
break;
}
break;
}
}while(pilih!=4);
}
}
No comments:
Post a Comment