2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/ToDoList.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// A more complex use of PriorityQueue
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
2017-01-09 14:26:12 -08:00
|
|
|
class ToDoItem implements Comparable<ToDoItem> {
|
|
|
|
private char primary;
|
|
|
|
private int secondary;
|
|
|
|
private String item;
|
2017-05-01 14:33:10 -06:00
|
|
|
ToDoItem(String td, char pri, int sec) {
|
2017-01-09 14:26:12 -08:00
|
|
|
primary = pri;
|
|
|
|
secondary = sec;
|
|
|
|
item = td;
|
|
|
|
}
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public int compareTo(ToDoItem arg) {
|
2017-01-09 14:26:12 -08:00
|
|
|
if(primary > arg.primary)
|
|
|
|
return +1;
|
|
|
|
if(primary == arg.primary)
|
|
|
|
if(secondary > arg.secondary)
|
2015-06-15 17:47:35 -07:00
|
|
|
return +1;
|
2017-01-09 14:26:12 -08:00
|
|
|
else if(secondary == arg.secondary)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public String toString() {
|
2017-01-09 14:26:12 -08:00
|
|
|
return Character.toString(primary) +
|
|
|
|
secondary + ": " + item;
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2017-01-09 14:26:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
class ToDoList {
|
2015-06-15 17:47:35 -07:00
|
|
|
public static void main(String[] args) {
|
2017-01-22 16:48:11 -08:00
|
|
|
PriorityQueue<ToDoItem> toDo =
|
|
|
|
new PriorityQueue<>();
|
2017-01-09 14:26:12 -08:00
|
|
|
toDo.add(new ToDoItem("Empty trash", 'C', 4));
|
|
|
|
toDo.add(new ToDoItem("Feed dog", 'A', 2));
|
|
|
|
toDo.add(new ToDoItem("Feed bird", 'B', 7));
|
|
|
|
toDo.add(new ToDoItem("Mow lawn", 'C', 3));
|
|
|
|
toDo.add(new ToDoItem("Water lawn", 'A', 1));
|
|
|
|
toDo.add(new ToDoItem("Feed cat", 'B', 1));
|
2016-10-22 13:35:19 -07:00
|
|
|
while(!toDo.isEmpty())
|
|
|
|
System.out.println(toDo.remove());
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
A1: Water lawn
|
|
|
|
A2: Feed dog
|
|
|
|
B1: Feed cat
|
|
|
|
B7: Feed bird
|
|
|
|
C3: Mow lawn
|
|
|
|
C4: Empty trash
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|