OggettoCD

Tutti i Progetti
public class Cd {
    // attributi
    private String titolo;
    private String autore;
    private int numBrani;
    private double durata;

    // costruttore
    public Cd(String titolo, String autore, int numBrani, double durata) {
        this.titolo = titolo;
        this.autore = autore;
        this.numBrani = numBrani;
        this.durata = durata;
    }

    // metodi
    public String toString() {
        return "\nTitolo: " + titolo + "\nAutore: " + autore + "\nNumero brani: " + numBrani + "\nDurata: " + durata + " minuti";
    }
    public int compareDurata(double durata) {
        if (this.durata > durata) {
            return 1;
        } else if (this.durata < durata) {
            return -1;
        } else {
            return 0;
        }
    }

    // getter
    public String getTitolo() {
        return titolo;
    }
    
    public String getAutore() {
        return autore;
    }
    
    public int getNumBrani() {
        return numBrani;
    }
    public double getDurata() {
        return durata;
    }
    
    // setter
    public void setTitolo(String titolo) {
        this.titolo = titolo;
    }
    public void setAutore(String autore) {
        this.autore = autore;
    }
    public void setNumBrani(int numBrani) {
        this.numBrani = numBrani;
    }
    public void setDurata(double durata) {
        this.durata = durata;
    }
    

    // main
    public static void main(String[] args) {
        Cd cd1 = new Cd("LA BELLAVITA", "Artie 5ive", 18, 54.00);
        Cd cd2 = new Cd("Milano Demons", "Shiva", 20, 59.30);
        Cd cd3 = new Cd("La Divina Commedia", "Tedua", 17, 53.50);

        System.out.println("\n------------------------------"+cd1.toString()+"\n------------------------------\n");
        System.out.println("\n------------------------------"+cd2.toString()+"\n------------------------------\n");
        System.out.println("\n------------------------------"+cd3.toString()+"\n------------------------------\n");

        Cd longestCd = cd1;
        if (cd2.compareDurata(longestCd.durata) > 0) {
            longestCd = cd2;
        }
        if (cd3.compareDurata(longestCd.durata) > 0) {
            longestCd = cd3;
        }

        System.out.println("Il CD con la durata maggiore è:");
        System.out.println(longestCd.toString());
        
    }

}

public class PortaCD {
    private final Cd[] slots;
    private int count;

    public PortaCD(int capacity) {
        this.slots = new Cd[capacity];
        this.count = 0;
    }

    public Cd getCD(int position) {
        validateIndex(position);
        return slots[position];
    }

    public void setCD(int position, Cd cd) {
        validateIndex(position);
        if (slots[position] == null) {
            count++;
        }
        slots[position] = cd;
    }

    public void killCD(int position) {
        if (slots[position] != null) {
            slots[position] = null;
            count--;
        }
    }

    public int getN() {
        return count;
    }

    public int cercaCDperTitolo(String titolo) {
        if (titolo == null) {
            return -1;
        }
        for (int i = 0; i < slots.length; i++) {
            Cd cd = slots[i];
            if (cd != null && Objects.equals(titolo, cd.getTitolo())) {
                return i;
            }
        }
        return -1;
    }

    public int confrontaCollezione(PortaCD altra) {
        if (altra == null) {
            return 0;
        }
        int matches = 0;
        boolean[] alreadyMatched = new boolean[altra.slots.length];
        for (Cd cd : slots) {
            if (cd == null) {
                continue;
            }
            for (int j = 0; j < altra.slots.length; j++) {
                if (alreadyMatched[j]) {
                    continue;
                }
                Cd other = altra.slots[j];
                if (other != null && equalsCd(cd, other)) {
                    alreadyMatched[j] = true;
                    matches++;
                    break;
                }
            }
        }
        return matches;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (Cd cd : slots) {
            if (cd == null) {
                continue;
            }
            if (builder.length() > 0) {
                builder.append(System.lineSeparator());
            }
            builder.append(cd.getTitolo());
        }
        return builder.length() == 0 ? "(nessun CD)" : builder.toString();
    }


    private boolean equalsCd(Cd first, Cd second) {
        return Objects.equals(first.getTitolo(), second.getTitolo())
            && Objects.equals(first.getAutore(), second.getAutore())
            && first.getNumBrani() == second.getNumBrani()
            && Double.compare(first.getDurata(), second.getDurata()) == 0;
    }
}

Output

------------------------------ Titolo: LA BELLAVITA Autore: Artie 5ive Numero brani: 18 Durata: 54.0 minuti ------------------------------ ------------------------------ Titolo: Milano Demons Autore: Shiva Numero brani: 20 Durata: 59.3 minuti ------------------------------ ------------------------------ Titolo: La Divina Commedia Autore: Tedua Numero brani: 17 Durata: 53.5 minuti ------------------------------ Il CD con la durata maggiore è: Titolo: Milano Demons Autore: Shiva Numero brani: 20 Durata: 59.3 minuti