-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.java
More file actions
126 lines (114 loc) · 2.79 KB
/
Item.java
File metadata and controls
126 lines (114 loc) · 2.79 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ads1ss13.pa;
/**
* Ein Item der Eingabe-Instanz.
*
* <p>
* Auf die Id des Items, den Profit und die Ressourcen kann einfach über die
* öffentlichen Variablen {@link #id}, {@link #profit} und
* {@link #resources} zugegriffen werden. Allerdings können diese Werte nicht
* verändert werden.
* </p>
*
* <p>
* <b>WICHTIG:</b> Nehmen Sie keine Änderungen an dieser Klasse vor! Diese
* werden vom Abgabesystem verworfen und es könnte sein, dass Ihr Programm
* dann nicht mehr korrekt funktioniert.
* </p>
*/
public class Item implements Comparable<Item> {
/** Die Id des Items. */
public final int id;
/** Der Profit des Items. */
public final int profit;
/** Die Ressourcen des Items als Array. */
public final int[] resources;
/**
* Erzeugt ein neues Item mit Id <code>id</code>, Wert <code>profit</code>
* und Ressourcen <code>resources</code>.
*
* <p>
* Das Erzeugen eines neuen Item-Objekts hat den Aufwand <i>O(1)</i>.
* </p>
*
* @param id
* Die Id des Items.
* @param p
* Der Profit des Items.
* @param r
* Die Ressourcen des Items als Array.
*/
public Item(int id, int p, int[] r) {
this.id = id;
this.profit = p;
this.resources = r;
}
/**
* Liefert eine String-Repräsentation dieses Items, d.h. die Id.
*
* Der Aufwand dieser Operation ist in <i>O(1)</i>.
*
* @return Ein {@link String} der dieses Item beschreibt.
*/
@Override
public String toString() {
return ""+id;
}
/**
* Testet zwei Items auf Gleichheit.
*
* <p>
* Zwei Items sind gleich, wenn Ihre Ids, Profite und Ressourcen gleich sind.
* </p>
*
* <p>
* Der Aufwand des Vergleichs ist in <i>O(|Ressourcen|)</i>.
* </p>
*
* @param other
* Das andere Item mit dem verglichen werden soll.
* @return <code>true</code> wenn die beiden Items gleich sind,
* <code>false</code> sonst.
*/
@Override
public boolean equals(Object other) {
if (other instanceof Item) {
Item o = (Item) other;
boolean equal = true;
// Vergleich der Ids
if (this.id != o.id) {
return false;
}
// Vergleich der Profite
if (this.profit != o.profit) {
return false;
}
// Vergleich der Ressourcen
if (this.resources.length != o.resources.length) {
return false;
} else {
for (int i=0;i<this.resources.length;i++) {
if (this.resources[i] != o.resources[i]) {
return false;
}
}
}
// wenn alles gleich ist
return true;
} else {
return false;
}
}
/**
* Vergleicht zwei Items gemäß ihrer Profite (<code>profit</code>).
*/
@Override
public int compareTo(Item o) {
if (this.profit < o.profit) {
return -1;
} else if (this.profit > o.profit) {
return 1;
} else {
return 0;
}
}
}