QUEUE
QUEUE
NAMA: SYURAINI NOOR CHAMSYAH
NPM:22082010033
PARALEL: A
Source code class aplikasi queue
package queue.bp;
import java.util.Scanner;
public class QueueBP {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
cAntrian antrian = new cAntrian();
Scanner scanner = new Scanner(System.in);
int pilih = 0;
do {
System.out.println("----------------------------------");
System.out.println("-------------- Queue -------------");
System.out.println("----------------------------------");
System.out.println(" 1. Tambah Antrian ");
System.out.println(" 2. Tampilkan Antrian ");
System.out.println(" 3. Proses Antrian ");
System.out.println(" 4. Keluar ");
System.out.println("----------------------------------");
System.out.print("Pilih : ");
pilih = scanner.nextInt();
System.out.println("");
switch (pilih) {
case 1:
System.out.print("Nama pembeli: ");
String namaPembeli = scanner.next();
System.out.print("Barang : ");
String Barang = scanner.next();
System.out.print("Harga barang: ");
int hargaBarang = scanner.nextInt();
antrian.enqueue(namaPembeli, Barang, hargaBarang);
System.out.println("");
System.out.println("Pembelian " + Barang + " oleh "
+ namaPembeli + " dengan harga "
+ hargaBarang + " telah ditambahkan ke dalam antrian");
System.out.println("");
break;
case 2:
antrian.tampilAntrian();
break;
case 3:
cPesanan processedItem = antrian.dequeue();
if(processedItem == null){
System.out.println("Antrian kosong..");
}else{
System.out.println("");
System.out.println("Pembelian " + processedItem.getBarang() + " oleh "
+ processedItem.getnamaPembeli() + " dengan harga "
+ processedItem.gethargaBarang() + " telah diproses");
System.out.println("");
}
break;
case 4:
System.out.println("Terimakasih...");
break;
default:
System.out.println("Pilihan tidak ada..");
break;
}
}while(pilih != 4);
}
}
Source code Class Antrian
package queue.bp;
import java.util.LinkedList;
import java.util.Queue;
public class cAntrian {
private Queue<cPesanan> pesan;
public cAntrian() {
pesan = new LinkedList<>();
}
public void enqueue(String namaPembeli, String Barang, int hargaBarang) {
cPesanan pesanan = new cPesanan(namaPembeli, Barang, hargaBarang);
pesan.add(pesanan);
}
public cPesanan dequeue() {
return pesan.poll();
}
public void tampilAntrian() {
System.out.println("========== Daftar Antrian ==========");
if (pesan.isEmpty()) {
System.out.println("Antrian kosong..");
} else {
for(cPesanan p : pesan) {
System.out.println("Nama Pembeli: " + p.getnamaPembeli()
+ "\nBarang yang dibeli: " + p.getBarang()
+ "\nHarga Barang: " + p.gethargaBarang() +"\n");
}
}
}
}
Source code class pesanan
package queue.bp;
public class cPesanan {
private String namaPembeli;
private String Barang;
private int hargaBarang;
public cPesanan(String namaPembeli, String Barang, int hargaBarang) {
this.namaPembeli = namaPembeli;
this.Barang = Barang;
this.hargaBarang = hargaBarang;
}
public String getnamaPembeli() {
return namaPembeli;
}
public String getBarang() {
return Barang;
}
public int gethargaBarang() {
return hargaBarang;
}
}
Komentar
Posting Komentar