-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.java
44 lines (37 loc) · 1.28 KB
/
Car.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
import java.util.*;
public class Car {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Queue<String> cars = new LinkedList<>();
String car;
System.out.println("Type the car you want! if you are finished typing the cars, type \"Finish\".");
do {
car = scan.next();
if (car.equals("Finish")) {
break;
}
cars.add(car);
} while (!car.equals("Finish"));
Iterator<String> carIterator = cars.iterator();
System.out.print("Cars: ");
while (carIterator.hasNext()) {
System.out.print(carIterator.next() + ", ");
}
System.out.println();
while (!cars.isEmpty()) { // polling cars from the queue
String removedCar = dequeue(cars);
System.out.println(removedCar+ " is removed!");
}
if (cars.isEmpty()) {
System.out.println("All of the cars have left!");
} else {
System.out.println(cars);
}
}
public static void enqueue(Queue<String> cars,String car) {
cars.add(car);
}
public static String dequeue(Queue<String> cars) {
return cars.poll();
}
}