-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantriBankCrc.java
104 lines (97 loc) · 3.39 KB
/
antriBankCrc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Random;
import java.util.Scanner;
public class antriBankCrc {
public static int ukuran;
public static int[] queue;
public static int front = -1;
public static int rear = -1;
public static int maxSize;
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
maxSize = 10;
queue = new int[maxSize];
Random random = new Random();
boolean aktif = true;
while (aktif) {
System.out.println("\n\nSistem Antri Nasabah Bank AI (maks 10 antrian)");
System.out.println("___________________________\n1.Ambil Nomor Antrian\n2.Panggil Nomor Antrian\n3.Tampilkan Semua Nomor Antrian\n4.Keluar");
int pilih = input.nextInt();
switch (pilih) {
case 1:
int antriRandom = random.nextInt(99);
if (isFull()) {
System.out.println("Antrian penuh!");
} else {
enqueue(antriRandom);
System.out.println("Nomor yang diberikan adalah " + antriRandom);
}
break;
case 2:
if (isEmpty()) {
System.out.println("Antrian kosong!");
} else {
System.out.println("PANGGILAN KEPADA NOMOR ANTRIAN " + dequeue() + "!!");
}
break;
case 3:
displayQueue();
break;
case 4:
aktif = false;
break;
default:
System.out.println("Maaf, Inputan anda tidak dikenali!!");
break;
}
}
}
public static boolean isEmpty() {
return front == -1;
}
public static boolean isFull() {
return (front == 0 && rear == maxSize - 1) || (front == rear + 1);
}
public static void enqueue(int item) {
if (isEmpty()) {
front = 0;
rear = 0;
} else if (rear == maxSize - 1) {
rear = 0;
} else {
rear++;
}
queue[rear] = item;
}
public static int dequeue() {
int item = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else if (front == maxSize - 1) {
front = 0;
} else {
front++;
}
return item;
}
public static void displayQueue() {
int i;
if (isEmpty()) {
System.out.println("Antrian kosong!");
return;
}
System.out.println("Antrian dari depan ke belakang:");
if (front <= rear) {
for (i = front; i <= rear; i++) {
System.out.println("Antrian ke-" + (i - front + 1) + " adalah Nomor Antrian " + queue[i]);
}
} else {
for (i = front; i < maxSize; i++) {
System.out.println("Antrian ke-" + (i - front + 1) + " adalah Nomor Antrian " + queue[i]);
}
for (i = 0; i <= rear; i++) {
System.out.println("Antrian ke-" + (i - front + 1 + maxSize) + " adalah Nomor Antrian " + queue[i]);
}
}
}
}