-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExercise1336_RandomQueueRandomIterator.java
72 lines (62 loc) · 2.58 KB
/
Exercise1336_RandomQueueRandomIterator.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
public class Exercise1336_RandomQueueRandomIterator {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
MixedDeck mixedDeck = MixedDeck.MixedDeck();
RandomQueueRandomIterator<Card> randomQueue = RandomQueueRandomIterator.RandomQueueRandomIterator();
// enqueue 13 times, dequeue 13 times, enqueue 13 times, sample 4 times
test1(randomQueue, mixedDeck);
// enqueue 4, dequeue 2, sample 2, enqueue 11, dequeue 7, sample 3
test2(randomQueue, mixedDeck);
}
private static void test1 (RandomQueueRandomIterator<Card> randomQueue, MixedDeck mixedDeck){
StdOut.println("Test 1: ");
enqueue(13, randomQueue, mixedDeck);
dequeue(13, randomQueue);
enqueue(13, randomQueue, mixedDeck);
sample(4, randomQueue);
}
private static void test2 (RandomQueueRandomIterator<Card> randomQueue, MixedDeck mixedDeck){
StdOut.println("Test 2: ");
enqueue(4, randomQueue, mixedDeck);
dequeue(2, randomQueue);
sample(2, randomQueue);
enqueue(11, randomQueue, mixedDeck);
dequeue(7, randomQueue);
sample(3, randomQueue);
}
private static void enqueue(int times, RandomQueueRandomIterator<Card> randomQueue, MixedDeck mixedDeck) {
StdOut.println("Enqueue " + times + " times ");
StdOut.println();
for (int i = 0; i < times; i++) {
randomQueue.enqueue(mixedDeck.getCard());
}
StdOut.println(times + " card(s) bridge hand: ");
printRandomQueue(randomQueue);
StdOut.println();
}
private static void dequeue(int times, RandomQueueRandomIterator<Card> randomQueue) {
for (int i = 0; i < times; i++) {
StdOut.println("Dequeued card: " + randomQueue.dequeue().getCard() + ", bridge hand left: ");
StdOut.println();
printRandomQueue(randomQueue);
StdOut.println();
}
}
private static void sample(int times, RandomQueueRandomIterator<Card> randomQueue) {
for (int i = 0; i < times; i++) {
StdOut.println("Sample: " + randomQueue.sample().getCard() + ", brigde hand now is: ");
StdOut.println();
printRandomQueue(randomQueue);
StdOut.println();
}
}
private static void printRandomQueue(RandomQueueRandomIterator<Card> randomQueue) {
if (randomQueue.isEmpty()) {
StdOut.println("Deck is empty");
} else {
for (Card s : randomQueue) {
StdOut.println(s.getCard());
}
}
}
}