-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaItem.java
72 lines (55 loc) · 1.46 KB
/
MediaItem.java
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
import java.text.DecimalFormat;
public abstract class MediaItem implements Comparable {
private double price;
private String title;
DecimalFormat twoDec = new DecimalFormat("#.00");
/**Constructor for MediaItem superclass
*
* @param t the object's title
* @param p the object's price
*/
public MediaItem(String t, double p) {
this.title = t;
this.price = p;
}
/**Must return the average rating as a double
* @return the average ratign
*/
public abstract double overallRating();
/* Attempts to compare this and object o's ratings, returns 1 if
* this has a greater rating, -1 if o has a greater rating and
* 0 if they are equal. If an exception is thrown, -2 is returned.
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @param o the object to be compared to this
*/
public int compareTo(Object o) {
try {
if (this.overallRating() > ((MediaItem) o).overallRating())
return 1;
else if (this.overallRating() < ((MediaItem) o).overallRating())
return -1;
else
return 0;
} catch (ClassCastException e) {
e.printStackTrace();
System.exit(1);
}
return -2;
}
public String toString() {
return "$" + twoDec.format(this.getPrice()) + " \"" + this.getTitle() + "\" ";
}
public double getPrice() {
return price;
}
public String getTitle() {
return title;
}
public void setPrice(double price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
}