Skip to content

Instantly share code, notes, and snippets.

@eleanor-em
Created October 10, 2019 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleanor-em/2a8ea0163d2b03e958b86ae00f5f21a7 to your computer and use it in GitHub Desktop.
Save eleanor-em/2a8ea0163d2b03e958b86ae00f5f21a7 to your computer and use it in GitHub Desktop.
Week 10 example
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
class WaitTimeComparator implements Comparator<Customer> {
@Override
public int compare(Customer c1, Customer c2) {
// if (c1.waitTime > c2.waitTime) {
// return -1;
// } else if (c1.waitTime == c2.waitTime) {
// return 0;
// } else {
// return 1;
// }
return c2.waitTime - c1.waitTime;
}
}
class Customer {
public final int waitTime;
public final String name;
Customer(int waitTime, String name) {
this.waitTime = waitTime;
this.name = name;
}
public static PriorityQueue<Customer> prioritise(ArrayList<Customer> customers) {
PriorityQueue<Customer> result = new PriorityQueue<>(new WaitTimeComparator());
for (Customer customer : customers) {
result.add(customer);
}
return result;
}
}
public class Program {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>();
customers.add(new Customer(10, "Bob"));
customers.add(new Customer(2, "Alice"));
customers.add(new Customer(1337, "Eve"));
PriorityQueue<Customer> prioritised = Customer.prioritise(customers);
while (!prioritised.isEmpty()) {
Customer next = prioritised.remove();
System.out.println("Now serving " + next.name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment