Rezolvare - Laborator FAIMA #6 / An 1 Semestru 1

Clasa Participant

public class Participant {
    public static enum INSTRUMENTE{
        VIOARA,PIAN,VIOLONCEL,DIRIJAT
    }
    private String nume;
    private int varsta;
    private INSTRUMENTE[] instrumente;

    public Participant(String nume, int varsta, INSTRUMENTE[] instrumente) {
        this.nume = nume;
        this.varsta = varsta;
        this.instrumente = instrumente;        
    }

    public String getNume() {
        return nume;
    }

    public void setNume(String nume) {
        this.nume = nume;
    }

    public int getVarsta() {
        return varsta;
    }

    public void setVarsta(int varsta) {
        this.varsta = varsta;
    }

    public INSTRUMENTE[] getInstrumente() {
        return instrumente;
    }

    public void setInstrumente(INSTRUMENTE[] instrumente) {
        this.instrumente = instrumente;
    }
    
    public boolean cantaLa(INSTRUMENTE instrument){
        for(INSTRUMENTE i:instrumente){
            if (i==instrument){
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + (this.nume != null ? this.nume.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Participant other = (Participant) obj;
        if ((this.nume == null) ? (other.nume != null) : !this.nume.equals(other.nume)) {
            return false;
        }
        return true;
    }
    
}

Clasa Grupa de Varsta


public class GrupaDeVarsta {
    private int min,max;
    private String nume;

    public GrupaDeVarsta(int min, int max, String nume) {
        this.min = min;
        this.max = max;
        this.nume = nume;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public String getNume() {
        return nume;
    }

    public void setNume(String nume) {
        this.nume = nume;
    }
    
    public boolean eInGrupaDeVarsta(Participant p){
        if(p.getVarsta()>=min && p.getVarsta()<=max){
            return true;
        }
        else{
            return false;
        }
    }    

Clasa Clasificare


public class Clasificare implements Comparable< Clasificare > {

    private Participant participant;
    private int nota;
    private Participant.INSTRUMENTE instrument;

    public Clasificare(Participant p, Participant.INSTRUMENTE instrument, int nota) {
        this.participant = p;
        this.instrument = instrument;
        this.nota = nota;
    }

    public Participant getParticipant() {
        return participant;
    }

    public void setParticipant(Participant p) {
        this.participant = p;
    }

    public Participant.INSTRUMENTE getInstrument() {
        return instrument;
    }

    public void setInstrument(Participant.INSTRUMENTE instrument) {
        this.instrument = instrument;
    }

    public int getNota() {
        return nota;
    }

    public void setNota(int nota) {
        this.nota = nota;
    }

    @Override
    public int compareTo(Clasificare o) {
        if (this.instrument == o.instrument) {
            return this.nota - o.nota;
        } else {
            throw new IllegalArgumentException("Cele doua clasificari nu pot fi comparate deoarece sunt la instrumente diferite");
        }
    }
}

Clasa principala


public class TestFAIMA {
    /*
     * Definim constantele din aplicatie: ingrediente, retete
     */

    private static final Participant[] participanti = new Participant[]{
        new Participant("Elizabeth Leonskaja", 12, new Participant.INSTRUMENTE[]{Participant.INSTRUMENTE.PIAN, Participant.INSTRUMENTE.DIRIJAT}),
        new Participant("Evgheni Kissin", 8, new Participant.INSTRUMENTE[]{Participant.INSTRUMENTE.PIAN, Participant.INSTRUMENTE.VIOARA})
            // si asa mai departe ...
    };
    private static final Clasificare[] clasificari = new Clasificare[]{
        new Clasificare(participanti[0], Participant.INSTRUMENTE.PIAN, 5),
        new Clasificare(participanti[0], Participant.INSTRUMENTE.DIRIJAT, 3),
        new Clasificare(participanti[1], Participant.INSTRUMENTE.PIAN, 5),
        new Clasificare(participanti[1], Participant.INSTRUMENTE.VIOARA, 2)
            //si asa mai departe ...
    };
    private static final GrupaDeVarsta[] grupe = new GrupaDeVarsta[]{
        new GrupaDeVarsta(5, 10, "Copii"),
        new GrupaDeVarsta(11, 15, "Juniori"),
        new GrupaDeVarsta(16, 18, "Tinere sperante")
    };

    public static void main(String[] args) {
        for (GrupaDeVarsta g:grupe){
            System.out.println("Grupa "+g.getNume());
            for(Participant.INSTRUMENTE instr:Participant.INSTRUMENTE.values()){
               System.out.println(instr.toString());
               TreeSet< Clasificare > clasament=new TreeSet< Clasificare >();
               for(Clasificare c:clasificari){
                   if(c.getInstrument()==instr && g.eInGrupaDeVarsta(c.getParticipant())){
                       try{
                           clasament.add(c);
                       }catch(IllegalArgumentException e){
                           System.out.println("Eroare in clasament: "+e.getMessage());
                       }
                   }
               }
               for(Clasificare c:clasament){
                   System.out.println(c.getParticipant().getNume()+" - "+c.getNota());
               }
            }            
        }
    }
}

0 comentarii: