add Anagrafe

This commit is contained in:
orange 2017-01-08 14:59:54 +01:00
parent 143eb5b9f4
commit 74b404f03f
5 changed files with 166 additions and 0 deletions

48
src/Nomi/Anagrafe.java Normal file
View File

@ -0,0 +1,48 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.io.PrintStream;
public class Anagrafe{
public Anagrafe(Scanner sc)throws Exception{
registro = new ArrayList<Persona>();
Persona p = Persona.read(sc);
while(p!=null){
registro.add(p);
p=Persona.read(sc);
}
}
private Anagrafe(ArrayList<Persona> registro){
this.registro=registro;
}
public void print(PrintStream ps){
for(Persona p: registro){
p.print(ps);
}
}
public Anagrafe filtroNome(String nome){
ArrayList<Persona> ar = new ArrayList<Persona>();
for(Persona p: registro){
if(p.getNome().equals(nome))
ar.add(p);
}
return new Anagrafe(ar);
}
public Anagrafe filtroCognome(String cognome){
ArrayList<Persona> ar = new ArrayList<Persona>();
for(Persona p: registro){
if(p.getCognome(). equals(cognome))
ar.add(p);
}
return new Anagrafe(ar);
}
private ArrayList<Persona> registro;
}

71
src/Nomi/Persona.java Normal file
View File

@ -0,0 +1,71 @@
import java.util.*;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
public class Persona{
private String nome;
private String cognome;
private Date dataDiNascita;
public Persona(String nome, String cognome, Date dataDiNascita){
this.nome= nome;
this.cognome = cognome;
this.dataDiNascita = dataDiNascita;
}
public String getNome(){
return nome;
}
public String getCognome(){
return cognome;
}
public Date getDataDiNascita(){
return dataDiNascita;
}
public void setNome(String nome){
this.nome = nome;
}
public void setDataDiNascita(Date dataDiNascita){
this.dataDiNascita = dataDiNascita;
}
public void setCognome (String cognome){
this.cognome = cognome;
}
public String toString(){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
return nome+" "+cognome+" "+sdf.format(dataDiNascita);
}
public void print(PrintStream ps){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
ps.println(nome);
ps.println(cognome);
ps.println(sdf.format(dataDiNascita));
}
public static Persona read(Scanner sc) throws Exception{
String nome, cognome;
Date dataDiNascita;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
if(!sc.hasNextLine()) return null;
nome=sc.nextLine();
if(!sc.hasNextLine())
return null;
cognome=sc.nextLine();
if(!sc.hasNextLine()) return null;
dataDiNascita = sdf.parse(sc.nextLine());
return new Persona(nome, cognome, dataDiNascita);
}
}

13
src/Nomi/Test.java Normal file
View File

@ -0,0 +1,13 @@
import java.util.Scanner;
import java.io.File;
public class Test{
public static void main(String [] args)throws Exception{
File fl = new File("persone.dat");
Scanner sc = new Scanner(fl);
Anagrafe an=new Anagrafe(sc);
an.print(System.out);
System.out.println("*******");
an.filtroCognome("due").filtroNome("noemi").print(System.out);
}
}

15
src/Nomi/persone.dat Normal file
View File

@ -0,0 +1,15 @@
pappa
papsp
12/12/1999
dffdfd
dhdhdh
14/08/1990
noemi
dhhf
13/05/1200
noemi
due
13/07/1300
jfjfjf
due
13/01/1400

19
src/mimmo.java Normal file
View File

@ -0,0 +1,19 @@
import java.io.PrintStream;
import java.util.Scanner;
public class mimmo {
public static void main(String [] args){
PrintStream ps =System.out;
while(true){
ps.println("Come ti chiami?");
Scanner sc = new Scanner(System.in);
String name=sc.next();
if(name.equalsIgnoreCase("erica"))
ps.println("Erica sei scema!!!");
else if(name.equalsIgnoreCase("marco"))
ps.println("Marco sei un figlio di puttana");
else
ps.println("Ciao, "+name);
}
}
}