-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
122 lines (109 loc) · 3.98 KB
/
Main.java
File metadata and controls
122 lines (109 loc) · 3.98 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
//https://pastebin.com/4xUaaLnz
//copy in this code from this pastebin
import java.util.*;
class Main
{
public static void main(String[] args)
{
ArrayList<Movie> list = new ArrayList<Movie>();
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));
list.add(new Movie("Avengers", 8.2, 1983));
list.add(new Movie("Justice", 8.3, 1983));
//all unique
list.add(new Movie("Force Awakens", 8.3, 2015));
list.add(new Movie("Star Wars", 8.7, 1977));
list.add(new Movie("Empire Strikes Back", 8.8, 1980));
list.add(new Movie("Return of the Jedi", 8.4, 1983));
//same year
list.add(new Movie("Avengers", 8.2, 1944));
list.add(new Movie("Willump", 8.4, 1944));
list.add(new Movie("Justice", 8.3, 1944));
//same year and rating
list.add(new Movie("Hello", 8.3, 1957));
list.add(new Movie("Returned", 8.3, 1957));
list.add(new Movie("Blades", 8.3, 1957));
// Collections.sort(list);
// for (Movie movie: list)
// System.out.println(movie.getYear() + " " +
// movie.getRating() + " " +
// movie.getName()+" ");
// System.out.println();
// // uses comparator to sort by name
// NameCompare nameCompare = new NameCompare();
// Collections.sort(list, nameCompare);
// for (Movie movie: list)
// System.out.println(movie.getYear() + " " +
// movie.getRating() + " " +
// movie.getName()+" ");
// System.out.println();
// RatingCompare ratingCompare = new RatingCompare();
// Collections.sort(list, ratingCompare);
// for (Movie movie: list)
// System.out.println(movie.getYear() + " " +
// movie.getRating() + " " +
// movie.getName()+" ");
System.out.println();
Collections.sort(list);
for (Movie movie: list)
System.out.println(movie.getYear() + " " +
movie.getRating() + " " +
movie.getName()+" ");
System.out.println();
YearCompare yearCompare = new YearCompare();
Collections.sort(list, yearCompare);
for (Movie movie: list)
System.out.println(movie.getYear() + " " +
movie.getRating() + " " +
movie.getName()+" ");
}
}
class Movie implements Comparable<Movie>
{
private double rating;
private String name;
private int year;
// Constructor
public Movie(String nm, double rt, int yr)
{
this.name = nm;
this.rating = rt;
this.year = yr;
}
public int compareTo(Movie m){
return this.year - m.year;
}
// Getter methods for accessing private data
public double getRating() { return rating; }
public String getName() { return name; }
public int getYear() { return year; }
}
class NameCompare implements Comparator<Movie>{
public int compare(Movie m1, Movie m2){
return m1.getName().compareTo(m2.getName());
}
}
class RatingCompare implements Comparator<Movie>{
public int compare(Movie m1, Movie m2){
if(m1.getRating() < m2.getRating())
return -1;
if(m1.getRating() > m2.getRating())
return 1;
else return 0;
}
}
class YearCompare implements Comparator<Movie>{
public int compare(Movie m1, Movie m2){
if(m1.getYear() < m2.getYear())
return -1;
if(m1.getYear() > m2.getYear())
return 1;
if(m1.getRating() < m2.getRating())
return -1;
if(m1.getRating() > m2.getRating())
return 1;
return 0;
}
}