I can make it a lot harder than that ... but my job was to create a showtimes class that implements Comparable. At the same time, he builds a list of several different theaters with film screenings, over time, etc. Then I need to use the compareTo () method to sort it first in the movie in which it is displayed, and in the movie title. I have the following code ... confusion occurs when I try to output sorted results. I have it where it will output the -or + number from string comparison.
public class ShowTimeTest {
public static void main(String[] args) {
ShowTime[] movieTimes= new ShowTime[20];
ShowTime cinemark1 = new ShowTime("Cinemark", "Lorde", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark2 = new ShowTime("Amstar Cinemas", "Star Wars", "Thursday @ 12:00AM", "Monday @ 4:00PM", "Sunday @ 3:45PM","R", 1, 2);
ShowTime cinemark3 = new ShowTime("Fayette Movies", "Pokemon", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark4 = new ShowTime("Dollar Theatre", "Reincarnated", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark5 = new ShowTime("Rad Chads Cinemas", "Lorde", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
movieTimes[0] = cinemark1;
movieTimes[1] = cinemark2;
movieTimes[2] = cinemark3;
movieTimes[3] = cinemark4;
movieTimes[4] = cinemark5;
for(int i = 1; i < 5; i ++){
System.out.println(movieTimes[i].compareTo(movieTimes[i-1]));
}
}
}
prod ...
public class ShowTime implements Comparable<ShowTime>{
public String name, title, rating, showTime1, showTime2, showTime3;
public double ticket, adultTicket;
public String[] times = new String[3];
public ShowTime(String theatreName, String movieTitle, String showTime1, String showTime2, String showTime3 , String movieRating, double childTicket, double aTicket)
{
name = theatreName;
title = movieTitle;
times[0] = showTime1;
times[1] = showTime2;
times[2] = showTime3;
rating = movieRating;
ticket = childTicket;
adultTicket = aTicket;
}
public String toString(){
String out = "Theatre: ";
out += name+ " " + "Movie: " + title + " " + "Rating " + rating + " " + "ShowTimes " + times[0] + " " + times[1] + " " + times[2]
+ " " + "Adult Cost: " + adultTicket + " " + "Child Ticket: " +ticket;
return out;
}
public int compareTo(ShowTime other) {
int result;
if(name.equals(((ShowTime)other).name)){
result = title.compareTo(((ShowTime)other).title);
}
else{
result = name.compareTo(((ShowTime)other).name);
}
return result;
}
}