diff --git a/src/Temi d'esame/Negozio/Archivio.java b/src/Temi d'esame/Negozio/Archivio.java new file mode 100644 index 0000000..a725044 --- /dev/null +++ b/src/Temi d'esame/Negozio/Archivio.java @@ -0,0 +1,55 @@ +import java.util.Scanner; +import java.util.ArrayList; +import java.util.Iterator; +import java.io.PrintStream; + +public class Archivio { + public Archivio(Scanner scN, Scanner scR){ + negozi=new ArrayList(); + responsabili=new ArrayList(); + Responsabile r=Responsabile.read(scR); + while(r!=null){ + responsabili.add(r); + r=Responsabile.read(scR); + } + Negozio n=Negozio.read(scN); + while(n!=null){ + try{ + r=searchResponsabile(n.getMatricola()); + n.setResponsabile(r); + r.addNegozio(n); + negozi.add(n); + } + catch(ResponsabileNonTrovato e){ + System.err.println(e.getMessage()); + } + n=Negozio.read(scN); + } + } + + private Responsabile searchResponsabile(String matricola){ + Iterator iter = responsabili.iterator(); + Responsabile r=null; + boolean trovato=false; + while(iter.hasNext() && !trovato){ + r=iter.next(); + if(r.getMatricola().equals(matricola)) + trovato=true; + } + + if(trovato) + return r; + throw new ResponsabileNonTrovato("Il responsabile con matricola. "+matricola+" non è stato trovato"); + } + + public void printReportResponsabili(PrintStream ps){ + for(Responsabile r: responsabili){ + ps.println(r.toString()); + r.printNegozi(ps); + ps.println("*****"); + } + } + + private ArrayList negozi; + private ArrayList responsabili; +} diff --git a/src/Temi d'esame/Negozio/Negozio.java b/src/Temi d'esame/Negozio/Negozio.java new file mode 100644 index 0000000..834569f --- /dev/null +++ b/src/Temi d'esame/Negozio/Negozio.java @@ -0,0 +1,110 @@ +import java.util.Date; +import java.util.Scanner; +import java.io.PrintStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +public class Negozio { + + + public Negozio(String iD, String matricola, Date dataApertura, Date dataChiusura, String ubicazione) { + ID = iD; + this.matricola = matricola; + this.dataApertura = dataApertura; + this.dataChiusura = dataChiusura; + this.ubicazione = ubicazione; + } + + public String getMatricola() { + return matricola; + } + + public void setMatricola(String matricola) { + this.matricola = matricola; + } + + public Date getdataChiusura() { + return dataChiusura; + } + + public void setdataChiusura(Date dataChiusura) { + this.dataChiusura = dataChiusura; + } + + public String getID() { + return ID; + } + + public Date getDataApertura() { + return dataApertura; + } + + public String getUbicazione() { + return ubicazione; + } + + public String toString() { + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + return ID+" "+matricola+" "+sdf.format(dataApertura)+"-"+sdf.format(dataChiusura)+ " "+ubicazione; + } + + public Responsabile getResponsabile() { + return responsabile; + } + + public void setResponsabile(Responsabile responsabile) { + this.responsabile = responsabile; + } + + public static Negozio read(Scanner sc){ + String ID, matricola, ubicazione; + Date dataApertura; + Date dataChiusura; + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + + if(!sc.hasNextLine()) return null; + ID=sc.nextLine(); + if(!sc.hasNextLine()) return null; + matricola=sc.nextLine(); + if(!sc.hasNextLine()) return null; + try{ + dataApertura=sdf.parse(sc.nextLine()); + } + catch(ParseException e){ + System.err.println("Impossibile leggere la data di inizio attività"); + dataApertura=new Date(0); + } + if(!sc.hasNextLine()) return null; + try{ + dataChiusura=sdf.parse(sc.nextLine()); + } + catch(ParseException e){ + System.err.println("Impossibile leggere la data di fine attività attività"); + dataChiusura=new Date(); + } + if(!sc.hasNextLine()) return null; + ubicazione=sc.nextLine(); + + return new Negozio(ID, matricola, dataApertura, dataChiusura, ubicazione); + } + + public void print(PrintStream ps){ + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + ps.println(ID); + ps.println(matricola); + ps.println(sdf.format(dataApertura)); + ps.println(sdf.format(dataChiusura)); + ps.println(ubicazione); + } + + + + private String ID; + private String matricola; + private Date dataApertura; + private Date dataChiusura; + private String ubicazione; + private Responsabile responsabile; + + +} diff --git a/src/Temi d'esame/Negozio/Responsabile.java b/src/Temi d'esame/Negozio/Responsabile.java new file mode 100644 index 0000000..6813a18 --- /dev/null +++ b/src/Temi d'esame/Negozio/Responsabile.java @@ -0,0 +1,109 @@ +import java.util.ArrayList; +import java.util.Scanner; +import java.util.Date; +import java.io.PrintStream; +import java.text.SimpleDateFormat; +import java.text.ParseException; + +public class Responsabile { + + + + public Responsabile(String matricola, String nome, String cnome, Date dataNascita, String comuneNascita) { + + this.matricola = matricola; + this.nome = nome; + this.cnome = cnome; + this.dataNascita = dataNascita; + this.comuneNascita = comuneNascita; + this.negozi = new ArrayList(); //inizializzato + + + } + + public String getMatricola() { + return matricola; + } + + public String getNome() { + return nome; + } + + public String getCnome() { + return cnome; + } + + public Date getDataNascita() { + return dataNascita; + } + + public String getComuneNascita() { + return comuneNascita; + } + + public String toString() { + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + return matricola+" "+nome+ " "+cnome+" "+sdf.format(dataNascita)+" "+comuneNascita; + } + + public ArrayList getNegozi() { + return negozi; + } + + public void setNegozi(ArrayList negozi) { + this.negozi = negozi; + } + + public void addNegozio(Negozio n){ + negozi.add(n); + + } + + public void printNegozi(PrintStream ps){ + for(Negozio n: negozi) + ps.println(n.toString()); + } + + public static Responsabile read(Scanner sc){ + String matricola, nome, cnome, comuneNascita; + Date dataNascita; + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + + if(!sc.hasNextLine()) return null; + matricola=sc.nextLine(); + if(!sc.hasNextLine()) return null; + nome=sc.nextLine(); + if(!sc.hasNextLine()) return null; + cnome=sc.nextLine(); + if(!sc.hasNextLine()) return null; + try{ + dataNascita=sdf.parse(sc.nextLine()); + } + catch(ParseException e){ + System.err.println("Impossibile leggere la data di nascita, impostata a base."); + dataNascita=new Date(0); + } + if(!sc.hasNextLine()) return null; + comuneNascita=sc.nextLine(); + + return new Responsabile(matricola, nome, cnome, dataNascita, comuneNascita); + } + + public void print(PrintStream ps){ + SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + ps.println(matricola); + ps.println(nome); + ps.println(cnome); + ps.println(sdf.format(dataNascita)); + ps.println(comuneNascita); + } + + private String matricola; + private String nome; + private String cnome; + private Date dataNascita; + private String comuneNascita; + private ArrayList negozi; + + +} diff --git a/src/Temi d'esame/Negozio/ResponsabileNonTrovato.java b/src/Temi d'esame/Negozio/ResponsabileNonTrovato.java new file mode 100644 index 0000000..d91fde7 --- /dev/null +++ b/src/Temi d'esame/Negozio/ResponsabileNonTrovato.java @@ -0,0 +1,5 @@ +public class ResponsabileNonTrovato extends RuntimeException{ + public ResponsabileNonTrovato(String msg){ + super(msg); + } +} \ No newline at end of file diff --git a/src/Temi d'esame/Negozio/Test.java b/src/Temi d'esame/Negozio/Test.java new file mode 100644 index 0000000..eac96e2 --- /dev/null +++ b/src/Temi d'esame/Negozio/Test.java @@ -0,0 +1,19 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class Test{ + public static void main(String [] args){ + Scanner scN=null, scR=null; + try{ + scN=new Scanner(new File("negozi.dat")); + scR=new Scanner(new File("responsabili.dat")); + } + catch(FileNotFoundException e){ + System.err.println("uno dei due file non esiste."); + System.exit(1); + } + Archivio ar1=new Archivio(scN, scR); + ar1.printReportResponsabili(System.out); + } +} \ No newline at end of file diff --git a/src/Temi d'esame/Negozio/negozi.dat b/src/Temi d'esame/Negozio/negozi.dat new file mode 100644 index 0000000..60ab293 --- /dev/null +++ b/src/Temi d'esame/Negozio/negozi.dat @@ -0,0 +1,22 @@ +NG01 +MT01 +06/11/2012 +18/12/2014 +Roma +NG02 +MT03 +01/06/2011 +13/09/2012 +Firenze +NG03 +MT01 +01/06/2011 +07/08/2014 +Venezia +NG04 +MT02 +09/08/1970 +08/02/186 +Verona + + diff --git a/src/Temi d'esame/Negozio/responsabili.dat b/src/Temi d'esame/Negozio/responsabili.dat new file mode 100644 index 0000000..92a1de0 --- /dev/null +++ b/src/Temi d'esame/Negozio/responsabili.dat @@ -0,0 +1,15 @@ +MT01 +Mario +Rossi +10/11/1980 +Milano +MT02 +Raffaele +Mignone +03/04/2016 +Benevento +MT03 +Giudo +Bianchi +06/07/1960 +Roma