Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Solution;
- import Provided.PizzaLover;
- import Provided.PizzaPlace;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- public class PizzaLoverImpl implements PizzaLover {
- private int id;
- private String name;
- private Set<PizzaPlace> favorites;
- private Set<PizzaLover> friends;
- public PizzaLoverImpl(int id, String name) {
- this.id = id;
- this.name = name;
- favorites = new HashSet<PizzaPlace>();
- friends = new HashSet<PizzaLover>();
- }
- @Override
- public int getId() {
- return this.id;
- }
- @Override
- public PizzaLover favorite(PizzaPlace pp) throws UnratedFavoritePizzaPlaceException {
- if(!((PizzaPlaceImpl)pp).isRated(this)){
- throw new UnratedFavoritePizzaPlaceException();
- }
- this.favorites.add(pp);
- return this;
- }
- @Override
- public Collection<PizzaPlace> favorites() {
- return favorites;
- }
- @Override
- public PizzaLover addFriend(PizzaLover pl) throws SamePizzaLoverException, ConnectionAlreadyExistsException {
- if(equals(pl)) throw new SamePizzaLoverException(); //fixme: check if this should be ==
- if(friends.contains(pl)) throw new ConnectionAlreadyExistsException();
- friends.add(pl);
- return this;
- }
- @Override
- public Set<PizzaLover> getFriends() {
- return friends;
- }
- private int compareByRating(PizzaPlaceImpl a, PizzaPlaceImpl b) {
- if(b.getRate(this) == a.getRate(this)) {
- if(a.distance() == b.distance()) {
- return a.getId() - b.getId();
- }
- return a.distance() - b.distance();
- }
- return b.getRate(this) - a.getRate(this);
- }
- @Override
- public Collection<PizzaPlace> favoritesByRating(int rLimit) {
- return this.favorites().stream()
- .filter(p-> p.averageRating() >= rLimit)
- .sorted((a,b)-> compareByRating((PizzaPlaceImpl) a,(PizzaPlaceImpl) b))
- .collect(Collectors.toList());
- }
- private int compareByDist(PizzaPlaceImpl a, PizzaPlaceImpl b) {
- if(a.distance() == b.distance()) {
- if(b.getRate(this) == a.getRate(this))if(a.distance() == b.distance()) {
- return a.getId() - b.getId();
- }
- return b.getRate(this) - a.getRate(this);
- }
- return a.distance() - b.distance();
- }
- @Override
- public Collection<PizzaPlace> favoritesByDist(int dLimit) {
- return this.favorites().stream()
- .filter(p-> p.distance() <= dLimit)
- .sorted((a,b)-> compareByDist((PizzaPlaceImpl) a,(PizzaPlaceImpl) b))
- .collect(Collectors.toList());
- }
- @Override
- public int compareTo(PizzaLover o) {
- return getId() - o.getId();
- }
- @Override
- public boolean equals(Object o) {
- return o != null && o.getClass() == getClass() && ((PizzaLoverImpl) o).getId() == getId();
- }
- @Override
- public String toString() {
- Stream<PizzaPlace> stream = this.favorites.stream();
- Stream<String> stringStream = stream.map((s) -> ((PizzaPlaceImpl)s).getName());
- stringStream = stringStream.sorted();
- String message = "";
- Iterator<String> iter = stringStream.collect(Collectors.toSet()).iterator();
- while(iter.hasNext()){
- message += iter.next();
- if(iter.hasNext()){
- message += ", ";
- }
- else{
- message += ".";
- }
- }
- return "Pizza lover: " +
- this.name + ".\n" +
- "Id: " + this.id + ".\n" +
- "Favorites: " + message;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement