-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSushiRoll.java
More file actions
82 lines (72 loc) · 2.38 KB
/
Copy pathSushiRoll.java
File metadata and controls
82 lines (72 loc) · 2.38 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
/**
* This class represents a sushi roll.
* A sushi roll has a name and is served on a colored plate, where
* the color of the plate indicates the price of the sushi roll.
*
* @author CS1331 TAs
* @version 13.31
*/
public class SushiRoll implements Comparable<SushiRoll> {
/**
* The name of the sushi roll.
*/
private final String name;
/**
* The color of the sushi roll's plate.
*/
private final Color color;
/**
* Constructs a new SushiRoll instance with a name and color.
*
* @param name the name of this sushi roll, must be non-null
* @param color the color of this sushi roll's plate, must be non-null
* @throws IllegalArgumentException if either name or color is null
*/
public SushiRoll(String name, Color color) {
if (name == null || color == null) {
throw new IllegalArgumentException("name and color must be non-null!");
}
this.name = name;
this.color = color;
}
/**
* Creates a new SushiRoll instance with a name and a plate color of Color.GREEN.
*
* @param name the name of this sushi roll, must be non-null
*/
public SushiRoll(String name) {
this(name, Color.GREEN);
}
/**
* Getter for color.
*
* @return the color of this sushi roll's plate
*/
public Color getColor() {
return color;
}
/**
* Returns a String representation of this sushi roll in the format "{name} {color}".
*/
@Override
public String toString() {
return String.format("%s %s", name, color.name());
}
/**
* Compares this sushi roll with another sushi roll for order by comparing
* the name of the two sushi rolls lexicographically. This method returns
* an integer equivalent to the return value of name.compareTo(other.name).
*
* Note: Since this class does not override Object's implementation of equals,
* this class has a natural ordering that is inconsistent with equals.
*
* @param other the other sushi roll to be compared
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object
* @throws NullPointerException if the specified object is null
*/
@Override
public int compareTo(SushiRoll other) {
return name.compareTo(other.name);
}
}