POO-ITBA / 2023_02

0 stars 0 forks source link

EJ1, parcial 2 cuatri 2022 #8

Open lulostupnik opened 1 year ago

lulostupnik commented 1 year ago

Hola Franco, como estas?

Resolvi el ej de manera parecida que la sol, pero creando comparadores estaticos en la clase TVEpisode. Queria saber si eso es correcto, o si deberia si o si estar en TVSeason. Gracias


package ej1;
import java.time.LocalDate;
import java.util.Comparator;

public class TVEpisode implements Comparable<TVEpisode>{
   private final int id;
   private final String title;
   private final double rating;
   private final LocalDate airedDate;

   public TVEpisode(int id, String title, double rating, LocalDate airedDate) {
       this.id = id;
       this.title = title;
       this.rating = rating;
       this.airedDate = airedDate;
   }

   public int getId() {
       return id;
   }

   public String getTitle() {
       return title;
   }

   public double getRating() {
       return rating;
   }

   public LocalDate getAiredDate() {
       return airedDate;
   }

   @Override
   public int compareTo(TVEpisode ep2){
       return Integer.compare(this.getId(), ep2.getId());
   }

   static public Comparator<TVEpisode> getDateComp(){
    return new Comparator<TVEpisode>(){
        @Override
        public int compare(TVEpisode o1, TVEpisode o2){
            int aux = o2.getAiredDate().compareTo(o1.getAiredDate()); 
            if(aux == 0){
                return o1.compareTo(o2); // asc
            }
            return aux;
        }
    };
   }
   static public Comparator<TVEpisode> getRankingComp(){
    return (o1,o2)->{
        int aux = Double.compare(o2.getRating(), o1.getRating());
        if(aux == 0){
            return o1.compareTo(o2);
        }
        return aux;
    };
   }

   static public Comparator<TVEpisode> getNameComp(){
    return (o1,o2)->{
        int aux = o1.getTitle().compareTo(o2.getTitle());
        if(aux ==0){
            return o1.compareTo(o2);
        }
        return aux;
    };
   }

   @Override
    public String toString(){
       return "(%d) Episode %s aired on %s has a rating of %.2f".formatted(id, title, airedDate.toString(), rating);
   }
}

package ej1;
import java.util.Arrays;
import java.util.Comparator;

public class TVSeason{
    private final TVEpisode[] array;

    public TVSeason(TVEpisode[] arr){
        array = arr;
    }
    private TVEpisode[] getVec(Comparator<TVEpisode> cmp){
        TVEpisode[] rta = Arrays.copyOf(array, array.length);
        Arrays.sort(rta, cmp);
        return rta;
    }   
    public TVEpisode[] getDateOrderCopy(){
        return getVec(TVEpisode.getDateComp());
    }
    public TVEpisode[] getNameOrderCopy(){
                return getVec(TVEpisode.getNameComp());
        }
    public TVEpisode[] getRatingOrderCopy(){
                return getVec(TVEpisode.getRankingComp());
        }
}
fmeola commented 1 year ago

Hola @lulostupnik Está perfecto así.