Ejercicio De Zapatos

Es una posible solucion se puede mejorar pero es una posibilidad

package shoes;

import java.util.ArrayList;

/**
 *
 * @author Carlos Alejandro
 */
public class Shoe {

    private int precio;
    private int tamaño;
    private String modelo;

    public Shoe(int precio, int tamaño, String modelo){
        this.modelo=modelo;
        this.precio=precio;
        this.tamaño=tamaño;
    }

    public Shoe(){
    }

    public int getPrice(){
        return precio;
    }

    public int getTamaño(){
        return tamaño;
    }

    public String getModelo(){
        return modelo;
    }

    @Override
    public String toString(){
        return modelo+", "+tamaño+", "+precio;
    }

    public void printList(ArrayList<Shoe> lista){
        for(int i=0; i<lista.size();i++){
            System.out.println(lista.get(i));
        }
    }

}
package shoes;

import java.util.Comparator;

/**
 *
 * @author Carlos Alejandro
 */
public class ShoeComparatorPrice implements Comparator{

    public int compare(Object o1, Object o2) {
        Shoe s1= (Shoe) o1;
        Shoe s2= (Shoe) o2;
        return s1.getPrice()-s2.getPrice();
    }

    @Override
    public boolean equals(Object o){
        return this==o;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }

}
package shoes;

import java.util.Comparator;

/**
 *
 * @author Carlos Alejandro
 */
public class ShoeComparatorSize implements Comparator{

    public int compare(Object o1, Object o2) {
        Shoe s1= (Shoe) o1;
        Shoe s2= (Shoe) o2;
        return s1.getTamaño()-s2.getTamaño();
    }

    @Override
    public boolean equals(Object o){
        return this==o;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }
}
package shoes;

import java.util.Comparator;

/**
 *
 * @author Carlos Alejandro
 */
public class ShoeComparatorModel implements Comparator{

    public int compare(Object o1, Object o2) {
        Shoe s1= (Shoe) o1;
        Shoe s2= (Shoe) o2;
        return s1.getModelo().compareTo(s2.getModelo());
    }

    @Override
    public boolean equals(Object o){
        return this==o;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }
}
package shoes;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author Carlos Alejandro
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Shoe s1 = new Shoe();
        ArrayList <Shoe>list = new ArrayList();

        list.add(new Shoe (200,4,"Nike"));
        list.add(new Shoe (120,8,"Adidas"));
        list.add(new Shoe(150,3,"New Balance"));
        s1.printList(list);
        System.out.println("");
        System.out.println("Precio:");
        Collections.sort(list, new ShoeComparatorPrice());
        s1.printList(list);
        System.out.println("");
        System.out.println("Size:");
        Collections.sort(list, new ShoeComparatorSize());
        s1.printList(list);
        System.out.println("");
        System.out.println("Model:");
        Collections.sort(list, new ShoeComparatorModel());
        s1.printList(list);
    }

}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License