Selasa, 02 November 2010

Queue

Nih bagi yang pingin tau contoh program Queue tentang antrian di Rumah Sakit,,
Cekidot,,,!!!


import java.util.Scanner;
class antrian {
private int max;
private pasien[] isi;
private int ekor;


//--------------------------------------------------------------
public antrian(int n) { // constructor
max = n;
isi = new pasien[max];
ekor = -1;
}


//--------------------------------------------------------------
public boolean isFull() { // bernilai true jika queue penuh
return (ekor == max-1);
}
//--------------------------------------------------------------
public boolean isEmpty() { // bernilai true jika queue kosong
return (ekor == -1);
}
//--------------------------------------------------------------
public void enQueue(String nama, String keluhan, String alamat) {
// memasukkan data di bagian paling belakang dari antrian
ekor++; // increment ekor
isi[ekor] = new pasien(nama, keluhan,alamat);
// nama, keluhan, alamat; // masukkan data
}
//--------------------------------------------------------------
public void deQueue() { // ambil data di bagian depan dari queue
for(int i=0; i < ekor; i++)
isi[i]=isi[i+1];
ekor--;
}
//--------------------------------------------------------------
public String peek() { // melihat data di bagian paling depan dari queue

return isi[0].ambilNama();
}
//--------------------------------------------------------------
public void tampil() { // menampilkan isi dari queue
for(int i=0; i < ekor+1; i++) {
isi[i].tampilPsn();
System.out.print(" ");
}
System.out.println("");
}
//--------------------------------------------------------------
public void clear() {
while( !isEmpty() ) // hapus data sampai kosong
deQueue();
}
//--------------------------------------------------------------
} // end class antrian
////////////////////////////////////////////////////////////////

class pasien{
private String namaPsn, kelPsn, alamatPsn;
public pasien( String nama, String keluhan, String alamat){
namaPsn = nama;
kelPsn = keluhan;
alamatPsn = alamat;
}


public void tampilPsn() {
System.out.println(namaPsn + " --> "+ kelPsn + "---->"+ alamatPsn );
}
public String ambilNama()
{ return namaPsn; }
public String ambilKel()
{ return kelPsn; }
public String ambilAlamat()
{ return alamatPsn; }

}

class antrianPasien {
public static void main(String[] args) {
String nama, keluhan, alamat;
antrian antri = new antrian(5); // queue menampung sampai 5 data
int menu;
do {
System.out.println("Pilih Menu");
System.out.println("1. Tambah pasien");
System.out.println("2. Panggil pasien");
System.out.println("3. Lihat Pasien");
System.out.println("4. Buat antrian baru");
System.out.println("5. Keluar");
System.out.print("Pilihan anda --> : ");
Scanner input = new Scanner(System.in);
menu = input.nextInt();
System.out.println("");
//--------------------------------------------------------------
switch(menu) {
case 1 : {
if(antri.isFull()) System.out.println("Queue sudah penuh!");
else {
System.out.print("Nama : ");
Scanner data = new Scanner(System.in);
nama= input.next();
System.out.print("keluhan : ");
keluhan = data.next();
System.out.print("Alamat : ");
alamat = data.next();
antri.enQueue(nama, keluhan, alamat);
}
break;
}
case 2 : {
if(antri.isEmpty()) System.out.println("Queue kosong!");
else {
System.out.println("Data " + antri.peek() + " diambil");
antri.deQueue();
}
break;
}
case 3 : {
if(antri.isEmpty()) System.out.println("Queue kosong!");
else {
antri.tampil();
}
break;
}
case 4 : antri.clear();
break;
}
} while(menu > 0 && menu < 5);
} // end main()
} // end class javaQueue

1 komentar:

Ada kesalahan di dalam gadget ini