-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.java
More file actions
73 lines (58 loc) · 1.56 KB
/
Copy pathItem.java
File metadata and controls
73 lines (58 loc) · 1.56 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
public class Item implements Comparable<Item>{
private String name;
public String getName() {
return name;
}
public void setName(String newName){
if(newName.matches("^[ A-Za-z]+$") && newName != "" ) {
this.name = newName;
}
else
throw new IllegalArgumentException(String.format("\n\nItem name must consist at least 1 letter and letters only\nError input: " + newName));
}
private int weight;
public int getWeight() {
return this.weight;
}
public void setWeight(int newWeight) {
if(newWeight > 0) {
this.weight = newWeight;
}
else
throw new IllegalArgumentException(String.format("\n\nItem weight must be greater than 0\nError input: " + newWeight));
}
private int value;
public int getValue() {
return this.value;
}
public void setValue(int newValue) {
if(newValue > 0) {
this.value = newValue;
}
else
throw new IllegalArgumentException(String.format("\n\nItem value must be greater than 0\nError input: " + newValue));
}
private float density;
public float getDensity(){
return density;
}
public void setDensity(float density){
this.density = density;
}
public Item(String name, int weight, int value) {
setName(name);
setWeight(weight);
setValue(value);
}
public String ToString() {
return name + "\t\tWeight = " + weight + "\tValue = " + value;
}
@Override
public int compareTo(Item o) {
if((this.density>o.getDensity()))
return 1;
else if(this.density==o.getDensity())
return 0;
else return -1;
}
}