Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. package Solution;
  2.  
  3. import Provided.PizzaLover;
  4. import Provided.PizzaPlace;
  5.  
  6. import java.util.Collection;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.Set;
  10. import java.util.stream.Collectors;
  11. import java.util.stream.Stream;
  12.  
  13. public class PizzaLoverImpl implements PizzaLover {
  14.  
  15.     private int id;
  16.     private String name;
  17.     private Set<PizzaPlace> favorites;
  18.     private Set<PizzaLover> friends;
  19.  
  20.     public PizzaLoverImpl(int id, String name) {
  21.         this.id = id;
  22.         this.name = name;
  23.         favorites = new HashSet<PizzaPlace>();
  24.         friends = new HashSet<PizzaLover>();
  25.     }
  26.  
  27.     @Override
  28.     public int getId() {
  29.         return this.id;
  30.     }
  31.  
  32.  
  33.     @Override
  34.     public PizzaLover favorite(PizzaPlace pp) throws UnratedFavoritePizzaPlaceException {
  35.         if(!((PizzaPlaceImpl)pp).isRated(this)){
  36.             throw new UnratedFavoritePizzaPlaceException();
  37.         }
  38.         this.favorites.add(pp);
  39.         return this;
  40.     }
  41.  
  42.     @Override
  43.     public Collection<PizzaPlace> favorites() {
  44.         return favorites;
  45.     }
  46.  
  47.     @Override
  48.     public PizzaLover addFriend(PizzaLover pl) throws SamePizzaLoverException, ConnectionAlreadyExistsException {
  49.         if(equals(pl)) throw new SamePizzaLoverException(); //fixme: check if this should be ==
  50.         if(friends.contains(pl)) throw new ConnectionAlreadyExistsException();
  51.         friends.add(pl);
  52.         return this;
  53.     }
  54.  
  55.     @Override
  56.     public Set<PizzaLover> getFriends() {
  57.         return friends;
  58.     }
  59.  
  60.     private int compareByRating(PizzaPlaceImpl a, PizzaPlaceImpl b) {
  61.         if(b.getRate(this) == a.getRate(this)) {
  62.             if(a.distance() == b.distance()) {
  63.                 return a.getId() - b.getId();
  64.             }
  65.             return a.distance() - b.distance();
  66.         }
  67.         return b.getRate(this) - a.getRate(this);
  68.     }
  69.  
  70.     @Override
  71.     public Collection<PizzaPlace> favoritesByRating(int rLimit) {
  72.         return this.favorites().stream()
  73.                 .filter(p-> p.averageRating() >= rLimit)
  74.                 .sorted((a,b)-> compareByRating((PizzaPlaceImpl) a,(PizzaPlaceImpl) b))
  75.                 .collect(Collectors.toList());
  76.     }
  77.  
  78.     private int compareByDist(PizzaPlaceImpl a, PizzaPlaceImpl b) {
  79.         if(a.distance() == b.distance()) {
  80.             if(b.getRate(this) == a.getRate(this))if(a.distance() == b.distance()) {
  81.                 return a.getId() - b.getId();
  82.             }
  83.             return b.getRate(this) - a.getRate(this);
  84.         }
  85.         return a.distance() - b.distance();
  86.     }
  87.  
  88.     @Override
  89.     public Collection<PizzaPlace> favoritesByDist(int dLimit) {
  90.         return this.favorites().stream()
  91.                 .filter(p-> p.distance() <= dLimit)
  92.                 .sorted((a,b)-> compareByDist((PizzaPlaceImpl) a,(PizzaPlaceImpl) b))
  93.                 .collect(Collectors.toList());
  94.     }
  95.  
  96.     @Override
  97.     public int compareTo(PizzaLover o) {
  98.         return getId() - o.getId();
  99.     }
  100.  
  101.     @Override
  102.     public boolean equals(Object o) {
  103.         return o != null && o.getClass() == getClass() && ((PizzaLoverImpl) o).getId() == getId();
  104.     }
  105.  
  106.     @Override
  107.     public String toString() {
  108.         Stream<PizzaPlace> stream = this.favorites.stream();
  109.         Stream<String> stringStream = stream.map((s) -> ((PizzaPlaceImpl)s).getName());
  110.         stringStream = stringStream.sorted();
  111.         String message = "";
  112.         Iterator<String> iter = stringStream.collect(Collectors.toSet()).iterator();
  113.         while(iter.hasNext()){
  114.             message += iter.next();
  115.             if(iter.hasNext()){
  116.                 message += ", ";
  117.             }
  118.             else{
  119.                 message += ".";
  120.             }
  121.         }
  122.         return "Pizza lover: " +
  123.                 this.name + ".\n" +
  124.                 "Id: " + this.id + ".\n" +
  125.                 "Favorites: " + message;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement