import java.io.File;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Collections;
public class MVD {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Syntax: java MVD datafile.txt");
System.exit(0);
}
try {
File file = new File(args[0]);
Scanner in = new Scanner(file);
in.nextLine(); // Skip over example line
char transaction, payment;
int time;
String name;
LinkedList<Person> signin = new LinkedList<Person>();
LinkedList<Person> renew = new LinkedList<Person>();
LinkedList<Person> register = new LinkedList<Person>();
LinkedList<Person> cashier = new LinkedList<Person>();
while (in.hasNext()) {
// This populates the main queue completely, which has a 'time' field which simulates various timed entries.
transaction = (char)in.next().charAt(0);
payment = (char)in.next().charAt(0);
time = Integer.parseInt(in.next());
name = in.nextLine().trim();
Person signedin = new Person(transaction, payment, time, name);
signin.add(signedin);
}
// Go through each element of each queue until done
while (signin.size() != 0 && renew.size() != 0 && register.size() != 0 && cashier.size() != 0) {
// Cycle through those in the sign-in queue
if (signin.size() != 0) {
for (int i=0; i<signin.peek().getTimeToWait(); i++) {
// Do nothing, but simulate waiting -very- fast seconds
}
switch (signin.peek().getTransaction()) {
case 'L':
signin.peek().setTimeToWait(90);
renew.add(signin.poll());
Collections.sort(renew);
break;
case 'R':
signin.peek().setTimeToWait(60);
register.add(signin.poll());
break;
}
}
// Cycle through those in the license-renewal queue
if (renew.size() != 0) {
for (int i=0; i<renew.peek().getTimeToWait(); i++) {
// Do nothing, but simulate waiting -very- fast seconds
}
renew.peek().setTimeToWait(30);
cashier.add(renew.poll());
}
// Cycle through those in the vehicle-registration queue
if (register.size() != 0) {
for (int i=0; i<register.peek().getTimeToWait(); i++) {
// Do nothing, but simulate waiting -very- fast seconds
}
register.peek().setTimeToWait(30);
cashier.add(register.poll());
}
// Cycle through the cashier queue and finish each person
if (cashier.size() != 0) {
for (int i=0; i<cashier.peek().getTimeToWait(); i++) {
// Do nothing, but simulate waiting -very- fast seconds
}
if (!cashier.peek().getCheckApproved()) {
cashier.peek().setTimeToWait(40); // 30+10, right?
cashier.add(cashier.poll());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.Random;
public class Person implements Comparable<Person> {
private char transaction;
private char payment;
private int timeToWait;
private int totalTime;
private String name;
public Person(char trans, char pay, int timeEntered, String personName) {
transaction = trans;
payment = pay;
timeToWait = timeEntered+10;
totalTime = timeEntered+10;
name = personName;
}
public int getTimeToWait() {
return timeToWait;
}
public void setTimeToWait(int time) {
timeToWait = time;
totalTime += time;
}
public int getTotalTime() {
return totalTime;
}
public String getName() {
return name;
}
public char getTransaction() {
return transaction;
}
public char getPayment() {
return payment;
}
/**"Decide" whether someone's check is approved or not.
* @return true if approved, false if not
*/
public boolean getCheckApproved() {
Random rand = new Random();
return (rand.nextInt(1) == 0);
}
public int compareTo(Person person) {
return name.compareTo(person.getName());
}
}