How to use a "compareTo" method to find the oldest movie?

0

I was doing an exercise on the sales of a video club, the video club has 3 products: movies, series and videogames. Each product has its class and in the main method I declare 4 objects of movie type, 4 of series and so on. Each class has overwritten the toString method, and use it to show all the data of the object (in the case of a movie, title, year, gender, director, etc.), each class has a comparative method that compares the antiquity in case of being a film, the number of seasons if it is a series, and the total duration if it is a videogame, in the exercise it does not specify if this method returns something or not, that it is passed by parameter or nothing.

I leave you the Film class

package video_club;

public class Pelicula implements Entregable{



    private String titulo;
    private Integer año ;
    private boolean prestado = false;
    private String genero = "no definido";
    private String director;

    Pelicula(){

    }

    Pelicula(String titulo, String director){
        this.titulo = titulo;
        this.director = director;
        año = 9999;

    }

    Pelicula(String titulo, Integer año, String genero, String director){
        this.titulo = titulo;
        this.año = año;
        this.genero = genero;
        this.director = director;
    }



    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public Integer getAño() {
        return año;
    }

    public void setAño(Integer año) {
        this.año = año;
    }

    public String getGenero() {
        return genero;
    }

    public void setGenero(String genero) {
        this.genero = genero;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public void entregar() {
        prestado = true;
    }

    public void devolver() {
        prestado = false;
    }

    public boolean isPrestado() {
        return prestado;
    }

    public String compareTo(Object a) {
        String pelicula = "";
        if(año < ((Pelicula)a).getAño()) pelicula = toString();
        else pelicula = ((Pelicula) a).toString();
        return pelicula;
    }

    @Override
    public String toString() {
        String info = "";
        info = "Titulo: " + titulo + ", Año: " + año + ", Genero: " + genero + ", Director: " + director;
        return info;

    }


}

"Method compareTo (Object a), compares the estimated hours in the videogames and in the series the number of seasons. and in the Movies the year "This is what the exercise says.

My big question is how do I do the following part of the exercise:

"Finally, it indicates that Videogame has more estimated hours, the series with more seasons and the oldest movie. Show them on screen with all your information (use the method toString ()). "

My question is how can I compare the method and how to use it to find the oldest movie, the series with the most seasons etc. I tried to do it with the compareTo and a For in the main method but it did not work, I pass the code of the main.

package video_club;

public class App {

    private Integer a = 0;
    private Integer b = 0;
    private Integer c = 0;

    public Integer getA() {
        return a;
    }

    public void setA(Integer a) {
        this.a = a;
    }

    public Integer getB() {
        return b;
    }

    public void setB(Integer b) {
        this.b = b;
    }

    public Integer getC() {
        return c;
    }

    public void setC(Integer c) {
        this.c = c;
    }

    public void ProductosEntregados(Pelicula a, Serie b, Videojuego c){

        if(a.isPrestado() == true) this.a++;
        if(b.isPrestado() == true) this.b++;
        if(c.isPrestado() == true) this.c++;

    }



    public static void main(String[] args) {

        Integer i;
        String serie = "";
        String pelicula = "";
        String videojuego = "";


        App a = new App();
        Pelicula [] peliculas = new Pelicula[4];
        Serie [] series = new Serie[4];
        Videojuego [] videojuegos = new Videojuego[4]; 


        peliculas[0] = new Pelicula("El Exorcista",1200,"Terror","K.Ramone");
        peliculas[1] = new Pelicula("El Conjuro", "J.Rawson");
        peliculas[2] = new Pelicula("La Vida Es Bella", 40, "Drama", "B.Smith");
        peliculas[3] = new Pelicula("Rapido y Furioso",50,"Accion","S.Rhoodes");


        series [0] = new Serie();
        series [1] = new Serie("Breaking Bad",21,"Suspenso", "J.Jhonson");
        series [2] = new Serie("The Walking Dead", 17, "Accion/Aventura", "M.Kennedy");
        series [3] = new Serie("Lost",15,"Suspenso","H.Ted");


        videojuegos [0] = new Videojuego();
        videojuegos [1] = new Videojuego("NFSHP", 700);
        videojuegos [2] = new Videojuego("League of Legends",600,"MOBA","Riot Games");
        videojuegos [3] = new Videojuego("Lineage II",20,"MMORPG","Eidos");


        peliculas[2].entregar();
        peliculas[0].entregar();
        peliculas[1].entregar();
        series[1].entregar();
        series[2].entregar();
        videojuegos[0].entregar();
        videojuegos[2].entregar();
        videojuegos[3].entregar();
        videojuegos[0].devolver();


        for(i=0;i<peliculas.length;i++) a.ProductosEntregados(peliculas[i], series[i], videojuegos[i]);


        System.out.println("Peliculas entregadas: " + a.getA() + "// Series entregadas: " + a.getB() + "// Videojuegos entregados: " + a.getC() + "\n-----------------------------------------------------------------");

    }

}
    
asked by Jorge DeSpringfield 09.10.2018 в 14:42
source

1 answer

0

If you can use a TreeSet as a movie container, since with it only overwriting the compareTo method, it automatically sorts them per year (if you specify it) in ascending order.

In your class Film overwrites the compareTo method:

What this method does is return a negative number if this.getYear () < p.getYear (); zero in the case that they are the same data or a positive value if this.getYear () > p.getYear ()

@Override
public int compareTo(Object o) {
    Pelicula p = (Pelicula) o;
    return this.getAño() - p.getAño();
}

Then in your App class, you create the four instances and then add them to the list. The elements will be ordered as you enter them. This is a disadvantage for when a large amount of data is entered, as it becomes too slow to enter and sort the data.

Set<Pelicula> peliculas = new TreeSet<>();
peliculas.add(new Pelicula("El Exorcista", 1200, "Terror", "K.Ramone"));
peliculas.add(new Pelicula("El Conjuro", "J.Rawson"));
peliculas.add(new Pelicula("La Vida Es Bella", 40, "Drama", "B.Smith"));
peliculas.add(new Pelicula("Rapido y Furioso", 50, "Accion", "S.Rhoodes"));
System.out.println(peliculas);

The output is:

[Titulo: La Vida Es Bella, Año: 40, Genero: Drama, Director: B.Smith
, Titulo: Rapido y Furioso, Año: 50, Genero: Accion, Director: S.Rhoodes
, Titulo: El Exorcista, Año: 1200, Genero: Terror, Director: K.Ramone
, Titulo: El Conjuro, Año: 9999, Genero: no definido, Director: J.Rawson
]

After that you will surely know how to obtain the item you want.

    
answered by 09.10.2018 / 19:38
source