Add Albergo

This commit is contained in:
orange 2017-01-12 18:47:52 +01:00
parent f51111ce43
commit d4df59e483
5 changed files with 238 additions and 0 deletions

View File

@ -0,0 +1,80 @@
import java.util.Scanner;
import java.io.PrintStream;
public class Albergo{
public Albergo(String nome, double prezzo, boolean tv, boolean aria, boolean frigo, boolean internet){
this.nome=nome;
this.prezzo=prezzo;
this.tv=tv;
this.frigo=frigo;
this.internet=internet;
this.aria=aria;
}
public String getNome(){
return nome;
}
public double getPrezzo(){
return prezzo;
}
public boolean hasTV(){
return tv;
}
public boolean hasFrigo(){
return frigo;
}
public boolean hasInternet(){
return internet;
}
public boolean hasAria(){
return aria;
}
public static Albergo read(Scanner sc){
String nome;
double prezzo;
boolean tv, frigo, internet, aria;
if(!sc.hasNextLine()) return null;
nome=sc.nextLine();
if(!sc.hasNextLine()) return null;
prezzo=Double.parseDouble(sc.nextLine());
if(!sc.hasNextLine()) return null;
tv=sc.nextLine().equalsIgnoreCase("SI");
if(!sc.hasNextLine()) return null;
aria=sc.nextLine().equalsIgnoreCase("SI");
if(!sc.hasNextLine()) return null;
frigo=sc.nextLine().equalsIgnoreCase("SI");
if(!sc.hasNextLine()) return null;
internet=sc.nextLine().equalsIgnoreCase("SI");
return new Albergo(nome, prezzo, tv, aria, frigo, internet);
}
public void print(PrintStream ps){
String boole;
ps.println(nome);
ps.println(prezzo);
boole=tv?"SI":"NO";
ps.println(boole);
boole=aria?"SI":"NO";
ps.println(boole);
boole=frigo?"SI":"NO";
ps.println(boole);
boole=internet?"SI":"NO";
ps.println(boole);
}
private String nome;
private double prezzo;
private boolean tv;
private boolean frigo;
private boolean internet;
private boolean aria;
}

Binary file not shown.

View File

@ -0,0 +1,9 @@
import java.util.Scanner;
import java.io.File;
public class Test{
public static void main(String [] args)throws Exception{
Trivago tri=new Trivago(new Scanner(new File("albergo.dat")));
tri.filtroTV().print(System.out);
}
}

View File

@ -0,0 +1,131 @@
import java.util.Scanner;
import java.util.LinkedList;
import java.io.PrintStream;
import java.util.function.Predicate;
public class Trivago{
public Trivago(Scanner sc){
list=new LinkedList<Albergo>();
Albergo a=Albergo.read(sc);
while(a!=null){
list.add(a);
a=Albergo.read(sc);
}
}
private Trivago(LinkedList<Albergo> newList){
this.list=newList;
}
public void print(PrintStream ps){
for(Albergo a: list){
a.print(ps);
ps.println("****");
}
}
private Trivago filtro(Predicate<Albergo> p){
LinkedList<Albergo> listFilter = new LinkedList<Albergo>();
for(Albergo a: list)
if(p.test(a))
listFilter.add(a);
return new Trivago(listFilter);
}
public Trivago filtroTV(){
return filtro(new TVPredicate());
}
public Trivago filtroAria(){
return filtro(new AriaPredicate());
}
public Trivago filtroFrigo(){
return filtro(new FrigoPredicate());
}
public Trivago filtroInternet(){
return filtro(new InternetPredicate());
}
public Trivago filtroMaggiore(double prezzo){
return filtro(new MaggiorePredicate(prezzo));
}
public Trivago filtroMinore(double prezzo){
return filtro(new MinorePredicate(prezzo));
}
public Trivago filtroNome(String nome){
return filtro(new NomePredicate(nome));
}
private LinkedList<Albergo> list;
}
class TVPredicate implements Predicate<Albergo>{
public boolean test(Albergo a){
return a.hasTV();
}
}
class AriaPredicate implements Predicate<Albergo>{
public boolean test(Albergo a){
return a.hasAria();
}
}
class FrigoPredicate implements Predicate<Albergo>{
public boolean test(Albergo a){
return a.hasFrigo();
}
}
class InternetPredicate implements Predicate<Albergo>{
public boolean test(Albergo a){
return a.hasInternet();
}
}
class MaggiorePredicate implements Predicate<Albergo>{
public MaggiorePredicate(double prezzo){
this.prezzo=prezzo;
}
public boolean test(Albergo a){
return a.getPrezzo()>=prezzo?true:false;
}
private double prezzo;
}
class MinorePredicate implements Predicate<Albergo>{
public MinorePredicate(double prezzo){
this.prezzo=prezzo;
}
public boolean test(Albergo a){
return a.getPrezzo()<=prezzo?true:false;
}
private double prezzo;
}
class NomePredicate implements Predicate<Albergo>{
public NomePredicate(String nome){
this.nome=nome;
}
public boolean test(Albergo a){
return a.getNome().equals(nome);
}
private String nome;
}

View File

@ -0,0 +1,18 @@
Miramonti
330
SI
NO
NO
SI
Olimpia
660
NO
SI
SI
NO
Rosa
540
SI
NO
SI
SI