Skip to content

Instantly share code, notes, and snippets.

@vasildakov-zz
Last active August 12, 2017 13:33
Show Gist options
  • Save vasildakov-zz/8b02886f70d87f2ca76749d7c3ee0abf to your computer and use it in GitHub Desktop.
Save vasildakov-zz/8b02886f70d87f2ca76749d7c3ee0abf to your computer and use it in GitHub Desktop.
Fims
class Film implements FilmInterface {
public String getTitle() {}
public Float getRating() {}
public int addRating(int rating) {}
}
class FilmCollection implements FilmCollectionInterface {
String[] sorts = { "title", "rating", "year", "genre" };
// also you can use a constants:
// public static final String SORT_TITLE = "title";
// public static final String SORT_RATING = "rating";
// public static final String SORT_YEAR = "year";
public void getFilms(String sort)
{
// check is the sort argument exist in allowed sorts
if ( ! ArrayUtils.contains( sorts, sort ) ) {
// throw an exception
throw new IllegalArgumentException("invalid sortation argument")
}
switch (sort) {
case "year":
// sort by title
break;
case "rating":
// sort by rating
break;
case "genre":
// sort by genre
break;
default:
// sort by title
break;
// return sorted films
}
// examples:
// https://stackoverflow.com/questions/19471005/sorting-an-arraylist-of-objects-alphabetically
// https://stackoverflow.com/questions/13380908/sort-arraylist-of-complex-objects-alphabetically
private function sortByRating()
{
// sort by rating
Collections.sort(films, new Comparator<Film>() {
public int compare(Film f1, Film f2) {
return f1.getRating().compareTo(f2.getRating());
}
});
}
}
import java.lang.*;
public interface FilmInterface
{
public void getTitle();
public void getYear();
public void getGenre();
public void addRating(int rating); // add rating to array
public void getRating(float rating) // return sum of the ratings devided by number of elements
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment