public class cPelanggan {
private String nama;
private String alamat;
cPelanggan(String n, String a){
nama=n; alamat=a;
System.out.println("Object pelanggan dibuat...");
}
public String getNama(){
return nama;
}
public String getAlamat(){
return alamat;
}
}
public class cBarang {
private String kode;
private String nama;
private double harga;
cBarang(){
kode=""; nama=""; harga=0;
System.out.println("Object barang dibuat...");
}
cBarang(String k, String n, double h){
kode=k; nama=n; harga=h;
System.out.println("Object barang dibuat...");
}
public String getNama(){
return nama;
}
public String getKode(){
return kode;
}
public double getHarga(){
return harga;
}
}
public class cPembelian {
private cPelanggan pelanggan;
private cBarang[] barang;
private int jbrg, maksbrg;
cPembelian(){
pelanggan=null;
barang = new cBarang[10];
jbrg=0; maksbrg=10;
System.out.println("Object pembelian dibuat...");
}
public String ToString(){
String t="";
System.out.println("jumlah "+jbrg);
for (int i = 0; i < jbrg; i++) {
t=t+barang[i].getKode()+"\t";
t=t+barang[i].getNama()+"\t";
t=t+barang[i].getHarga()+"\n";
}
return t;
}
public void tambahBarang(cBarang brg){
if(jbrg<maksbrg){
barang[jbrg]=brg;
jbrg++;
System.out.println("Penambahan sukses...");
}
else System.out.println("Kapasitas penuh!");
}
public void hapusBarang(String kd){
if(jbrg>0){
//cari kode KD di array barang
boolean ada=false;
for (int i = 0; i < jbrg; i++) {
if(barang[i].getKode().equalsIgnoreCase(kd)){
ada=true;
barang[i]=null;
for (int j = i; j < jbrg; j++) {
if(j==jbrg-1) //khuses elemen terakhir
barang[j]=null;
else barang[j]=barang[j+1];
}
jbrg--;
System.out.println("Penghapusan sukses...");
break;
}
}
if(ada==false){
System.out.println("Barang tidak ada!");
}
}
else System.out.println("Barang masih kosong!");
}
}
import java.util.Scanner;
public class appToko {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
cPelanggan plg;
cBarang[] brg = new cBarang[5];
cPembelian pb;
int jb=0, maksbrg=5;
int pilih=0;
//create object uji coba
pb = new cPembelian();
plg = new cPelanggan("Budi","Surabaya");
brg[0] = new cBarang("111","Sepatu",100000);
brg[1] = new cBarang("112","Tas",150000);
brg[2] = new cBarang("113","Topi",50000);
jb=3;
do{
System.out.println("\nMENU TOKO");
System.out.println("1. Pelanggan");
System.out.println("2. Barang");
System.out.println("3. Pembelian");
System.out.println("4. Exit");
System.out.print("Pilih = ");
pilih=sc.nextInt();
switch(pilih){
case 1:
System.out.println("Pelanggan");
System.out.println(plg.getNama()+" ["+plg.getAlamat()+"]");
break;
case 2:
System.out.println("Daftar Barang");
System.out.println("Kode\tNama Barang\tHarga");
for (int i = 0; i < jb; i++) {
System.out.print(brg[i].getKode());
System.out.print("\t"+brg[i].getNama());
System.out.println("\t"+brg[i].getHarga());
}
break;
case 3:
System.out.println("1. Tambah Barang");
System.out.println("2. Hapus Barang");
System.out.println("3. Lihat Pembelian");
System.out.print("Pilih = ");
int pilih2 = sc.nextInt();
switch(pilih2){
case 1:
System.out.print("Masukkan Nama = ");
String n = sc.next();
if(plg.getNama().equalsIgnoreCase(n)){
System.out.print("Kode Barang = ");
String k = sc.next();
//cek ke array barang di main
boolean ada=false;
for (int i = 0; i < jb; i++) {
if(brg[i].getKode().equalsIgnoreCase(k)){
ada=true;
pb.tambahBarang(brg[i]);
}
}
if(ada==false){
System.out.println("Barang tidak ada!");
}
}
else System.out.println("Pembeli belum ada!");
break;
case 2:
System.out.print("Masukkan Nama = ");
n = sc.next();
if(plg.getNama().equalsIgnoreCase(n)){
System.out.print("Kode Barang = ");
String k = sc.next();
if(pb!=null){
pb.hapusBarang(k);
}
else System.out.println("Pembelian masih kosong!");
}
else System.out.println("Pembeli belum ada!");
break;
case 3:
System.out.println("Daftar Pembelian");
System.out.println("Pelanggan : "+plg.getNama());
System.out.println("Kode\tNama Barang\tHarga");
System.out.println(pb.ToString());
break;
}
break;
}
}while(pilih!=4);
}
}
No comments:
Post a Comment