ECCO PRIMA LE MIE 3 CLASSI CD, ELENCOCD, MAIN CHE FUNZ
public class Cd implements java.io.Serializable{
private static final long serialVersionUID = 8742922483511444653L;
protected String titolo;
protected String artista;
protected String tempoTot;
protected String commenti;
protected boolean possesso;
public String tracce;
public Cd(String titolo, String artista, String tempoTot, String commenti, boolean possesso, String tracce) {
this.titolo = titolo;
this.artista = artista;
this.tempoTot = tempoTot;
this.commenti = commenti;
this.possesso = possesso;
this.tracce = tracce;
}
public String getArtista() {
return artista;
}
public void setArtista(String artista) {
this.artista = artista;
}
public String getCommenti() {
return commenti;
}
public void setCommenti(String commenti) {
this.commenti = commenti;
}
public boolean isPossesso() {
return possesso;
}
public void setPossesso(boolean possesso) {
this.possesso = possesso;
}
public String getTempoTot() {
return tempoTot;
}
public void setTempoTot(String tempoTot) {
this.tempoTot = tempoTot;
}
public String getTitolo() {
return titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public String getTracce() {
return tracce;
}
public void setTracce(String tracce) {
this.tracce = tracce;
}
public String toString() {
String mio = "";
if (possesso == false) {
mio = "prestato";
} else {
mio = "In possesso";
}
String stampa = "";
stampa += titolo + "\t" + artista + "\t" + tempoTot + "\t" + commenti + "\t" + mio + "\t" + tracce;
return stampa;
}
}
--------------------------------------------------------
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
/**
*
* @author Mister_M
*/
public class ElencoCd {
public ArrayList<Cd> elenco;
public ElencoCd() {
elenco = new ArrayList<Cd>();
}
public ArrayList<Cd> getElenco() {
return elenco;
}
public void setElenco(ArrayList<Cd> elenco) {
this.elenco = elenco;
}
public void addCD(String titolo, String artista, String tempoTot, String commenti, boolean possesso, String tracce) {
Cd c = new Cd(titolo, artista, tempoTot, commenti, possesso, tracce);
elenco.add(c);
}
// metodo per modificare i 2 oggetti passando prima l'ogg da modif
public void modificaCd(Cd cd, String tit, String art, String tempo, String comm, boolean poss, String trac) {
cd.setTitolo(tit);
cd.setArtista(art);
cd.setTempoTot(tempo);
cd.setPossesso(poss);
cd.setCommenti(comm);
cd.setTracce(trac);
}
public String toString() {
String report = "*****************************************\n";
report += "My CD Collection\n\n";
report += "Numero di CD : " + elenco.size() + "\n";
report += "\n\n CD List:\n\n";
report += "Titolo" + "\t" + "Artista" + "\t" + "TempoTot" + "\t" + "Commenti" + "\t" + "Possesso" + "\t" + "Tracce\n";
for (int i = 0; i < elenco.size(); i++) {
report += elenco.get(i) + "\n";
}
return report;
}
//Carico da dbcd.txt ogg precaricati di tipo Cd
void Caricamentocd() {
try {
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("dbcd.txt"));
boolean check = true;
while (check) {
try {
Cd object = (Cd) objIn.readObject();
elenco.add((Cd) object);
} catch (EOFException ex) {
check = false;
}
}
objIn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Salvo/Scrivo dall' ArreyList gli oggetti nel file dbdvd.txt in modo da poterli ricaricare all'avvio del programma
public void SalvataggioCd() throws IOException {
Cd disc = null;
try {
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("dbcd.txt")));
for (int i = 0; i < elenco.size(); i++) {
disc = (Cd) elenco.get(i);
oos.writeObject(disc); // scrivo l'oggetto
}
oos.close();
} catch (IOException e) {
System.out.println("Errore: " + e);
System.exit(1);
}
}
public void RicercaPerTitolo(String titolo) {
boolean b = false;
Cd disc;
for (int i = 0; i < elenco.size(); i++) {
disc = (Cd) elenco.get(i);
if (titolo.toLowerCase().equals(disc.titolo.toLowerCa se())) {
b = true;
if (b == true) {
System.out.println("Disco Esistente");
System.out.println(disc);
} else {
System.out.println("Nessuna corrispondenza");
}
}
}
}
public void RicercaPerArtista(String artista) {
boolean b = false;
Cd disc;
for (int i = 0; i < elenco.size(); i++) {
disc = (Cd) elenco.get(i);
if (artista.toLowerCase().equals(disc.artista.toLower Case())) {
b = true;
if (b == true) {
System.out.println("Disco Esistente");
System.out.println(disc);
} else {
System.out.println("Nessuna corrispondenza");
}
}
}
}
//Crea un file txt dove stampa il contenuto dell ArreyLISt come stringe un ogg per riga
public void StampaSuFile() {
Cd a = null;
try {
FileOutputStream fos = new FileOutputStream("prova.txt");
PrintStream scrivi = new PrintStream(fos);
for (int i = 0; i < elenco.size(); i++) {
a = (Cd) elenco.get(i);
scrivi.println(a);
}
} catch (IOException e) {
System.out.println("Errore: " + e);
System.exit(1);
}
}
//Metodo di controllo usando come parametri titolo e artista per controllare che l'oggetto non sia gia presente
public boolean isStored(String titolo, String artista) {
boolean isStored = false;
Cd c = new Cd(titolo, artista, "", "", true,"");
for (int i = 0; i < elenco.size(); i++) {
if (c.getTitolo().equals(elenco.get(i).getTitolo()) && c.getArtista().equals(elenco.get(i).getArtista())) {
isStored = true;
}
}
return isStored;
}
}
-------------------------------------------------------
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException, StreamCorruptedException {
//----------Inizzializzo ArrayList
ElencoCd musica = new ElencoCd()
//-----------Il Caricamento funz per ora lo commento FUNZIONA
//musica.Caricamentocd();
musica.addCD("Storm Front", "Billy Joel", "110", "Commento a", true, "10");
musica.addCD("Come on Over", "Shania Twain", "112", "Commento b", false, "16");
musica.addCD("Soundtrack", "Les Miserable", "110", "Commento c", true, "33");
musica.addCD("Graceland", "Paul Simon", "110", "Commento d", false, "11");
//------Stampo i cd e i dvd
System.out.print(musica);
//-------Stampo su file prova.txt la mia collezzione come Stringhe FUNZIONA
musica.StampaSuFile();
//------Salvo su file dbcd.txte la mia collezzione come oggetti FUNZIONA
//musica.SalvataggioCD();
//-------Cancello un cd FUNZIONA
// musica.elimina("Soundtrack", "Les Miserable");
//-------Prova di semplice ricerca per titolo FUNZIONA
musica.RicercaPerTitolo("Grceland");
musica.RicercaPerTitolo("Graceland");
}
}
--------------------------------

Rispondi quotando
