Biblioteca Java - Blame information for rev 29
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
29 | mihai | 1 | package exemple.fire.priorityqueue; |
2 | |||
3 | import java.util.PriorityQueue; | ||
4 | import java.util.Random; | ||
5 | |||
6 | public class Hospital { | ||
7 | public static void main(String[] args) { | ||
8 | PriorityQueue pq = new PriorityQueue(); | ||
9 | Random r = new Random(); | ||
10 | for(int i=0;i<10;i++){ | ||
11 | String name = "Patient "+i; | ||
12 | int eLevel = r.nextInt(); | ||
13 | Patient p = new Patient(name,eLevel); | ||
14 | System.out.println("New patient arrived:"+p); | ||
15 | pq.offer(p); | ||
16 | } | ||
17 | |||
18 | System.out.println("\nTreat patients.\n"); | ||
19 | |||
20 | for(int i=0;i<10;i++){ | ||
21 | Patient p = (Patient)pq.poll(); | ||
22 | System.out.println("Treat patient: "+p); | ||
23 | } | ||
24 | |||
25 | } | ||
26 | } | ||
27 | |||
28 | class Patient implements Comparable{ | ||
29 | String name; | ||
30 | int emergencyLevel; | ||
31 | |||
32 | Patient(String name, int eL){ | ||
33 | this.name = name;emergencyLevel = eL; | ||
34 | } | ||
35 | |||
36 | public int compareTo(Object o) { | ||
37 | Patient p = (Patient)o; | ||
38 | if(emergencyLevel>p.emergencyLevel) | ||
39 | return 1; | ||
40 | else if(emergencyLevel<p.emergencyLevel) | ||
41 | return -1; | ||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public String toString() { | ||
47 | return "[patient="+name+" emergencyLevel="+emergencyLevel+"]"; | ||
48 | } | ||
49 | |||
50 | } |