Last active
August 12, 2017 13:33
-
-
Save vasildakov-zz/8b02886f70d87f2ca76749d7c3ee0abf to your computer and use it in GitHub Desktop.
Fims
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Film implements FilmInterface { | |
public String getTitle() {} | |
public Float getRating() {} | |
public int addRating(int rating) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
}); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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