Dalam membuat transaksi kantin,, kita membutuhkan 3 class yaitu penjualan, kantin dan makanan
Programnya dapat dilihat sbb:
1. ClassPenjualan
import java.util.Date;
public class penjualan
{
private Date tgl;
private makanan m;
private int jml;
private int total;
public penjualan(Date tgl, makanan m, int jml)
{
this.tgl = tgl;
this.m = m;
this.jml = jml;
hitungTotal();
}
public Date getTgl()
{
return tgl;
}
public int getJml()
{
return jml;
}
public int getTotal()
{
return total;
}
public void hitungTotal()
{
total = m.getHarga() * getJml();
}
public String cetak()
{
return getTgl() + "\t" + m.getNama() + "\t\t" + getJml() + "\t" + m.getHarga() + "\t" + getTotal();
}
}
2. ClassMakanan
public class makanan
{
private String nama;
private String jenis;
private int harga;
public makanan(String nama, String jenis, int harga)
{
this.nama = nama;
this.jenis = jenis;
this.harga = harga;
}
public String getNama()
{
return nama;
}
public String getJenis()
{
return jenis;
}
public int getHarga()
{
return harga;
}
public String cetak()
{
return getNama() + "\t" + getJenis() + "\t" + getHarga();
}
}
3. ClassKantin
import java.util.Date;
import java.io.*;
public class kantin
{
static makanan[] m = new makanan[20];
static int countMakanan = 0;
static void cetakSemuaMakanan() // Untuk mencetak semua makanan
{
System.out.println("-------------------------------");
System.out.println("Nama\t\tJenis\tHarga");
System.out.println("-------------------------------");
for(int i=0;i<countMakanan;i++)
{
System.out.println(m[i].cetak());
}
}
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String namaMakanan, jenisMakanan;
int hargaMakanan;
int jmlMakanan;
penjualan[] p = new penjualan[20];
Date tgl;
int countPenjualan = 0;
do
{
// menu utama
System.out.println("==================================");
System.out.println(" KANTIN ");
System.out.println("==================================");
System.out.println("1. Tambah Makanan");
System.out.println("2. Update Makanan");
System.out.println("3. Penjualan Makanan");
System.out.println("4. Lihat Semua Makanan");
System.out.println("5. Lihat Semua Penjualan");
System.out.println("6. Keluar");
System.out.println("==================================");
System.out.print("Pilihan : ");
int pil = Integer.parseInt(in.readLine());
System.out.println();
if(pil == 1)
{
// tambah makanan
System.out.println("-------------------------------");
System.out.println(" Tambah Makanan ");
System.out.println("-------------------------------");
System.out.print("Nama : ");
namaMakanan = in.readLine();
System.out.print("Jenis : ");
jenisMakanan = in.readLine();
System.out.print("Harga : ");
hargaMakanan = Integer.parseInt(in.readLine());
m[countMakanan] = new makanan(namaMakanan,jenisMakanan,hargaMakanan);
countMakanan ++;
}
else
{
if(pil == 2)
{
// update makanan
System.out.println("-------------------------------");
System.out.println(" Update Makanan ");
System.out.println("-------------------------------");
System.out.println();
cetakSemuaMakanan();
System.out.println();
System.out.print("Nama : ");
namaMakanan = in.readLine();
for(int i=0;i<countMakanan;i++)
{
if(m[i].getNama().equals(namaMakanan))
{
System.out.print("Jenis : ");
jenisMakanan = in.readLine();
System.out.print("Harga : ");
hargaMakanan = Integer.parseInt(in.readLine());
m[i] = new makanan(namaMakanan,jenisMakanan,hargaMakanan);
break;
}
}
}
else
{
if(pil == 3)
{
// penjualan makanan
System.out.println("-------------------------------");
System.out.println(" Penjualan Makanan ");
System.out.println("-------------------------------");
System.out.println();
cetakSemuaMakanan();
System.out.println();
System.out.print("Nama : ");
namaMakanan = in.readLine();
System.out.print("Jumlah : ");
jmlMakanan = Integer.parseInt(in.readLine());
for(int i=0;i<countMakanan;i++)
{
if(m[i].getNama().equals(namaMakanan))
{
tgl = new Date();
p[countPenjualan] = new penjualan(tgl,m[i],jmlMakanan);
countPenjualan ++;
break;
}
}
}
else
{
if(pil == 4)
{
// lihat semua makanan
System.out.println("-------------------------------");
System.out.println(" Lihat Semua Makanan ");
System.out.println("-------------------------------");
System.out.println();
cetakSemuaMakanan();
}
else
{
if(pil == 5)
{
// lihat semua penjualan
System.out.println("-------------------------------");
System.out.println(" Lihat Semua Penjualan ");
System.out.println("-------------------------------");
System.out.println();
System.out.println("------------------------------------------");
System.out.println("Tgl\tNama\t\tJumlah\tHarga\tTotal");
System.out.println("------------------------------------------");
int totalPenjualan = 0;
for(int i=0;i<countPenjualan;i++)
{
System.out.println(p[i].cetak());
totalPenjualan += p[i].getTotal();
}
System.out.println("Total Penjualan : " + totalPenjualan);
}
else
{
// keluar program
System.exit(0);
}
}
}
}
}
System.out.println();
}
while(true);
}
}
No comments:
Post a Comment