implements confrontatore

This commit is contained in:
Orange 2016-11-09 18:17:16 +01:00
parent 2073920306
commit 8e1d4fb096
5 changed files with 36 additions and 27 deletions

View File

@ -0,0 +1,3 @@
public interface Confrontatore{
boolean confronto(Object ob1, Object ob2);
}

View File

@ -0,0 +1,9 @@
public class ConfrontoArea implements Confrontatore{
public boolean confronto(Object ob1, Object ob2){
FiguraGeometrica fg1 = (FiguraGeometrica) ob1;
FiguraGeometrica fg2 = (FiguraGeometrica) ob2;
return fg1.calcolaArea()>fg2.calcolaArea();
}
}

View File

@ -0,0 +1,9 @@
public class ConfrontoPerimetro implements Confrontatore{
public boolean confronto(Object ob1, Object ob2){
FiguraGeometrica fg1 = (FiguraGeometrica) ob1;
FiguraGeometrica fg2 = (FiguraGeometrica) ob2;
return fg1.calcolaPerimetro()>fg2.calcolaPerimetro();
}
}

View File

@ -26,47 +26,35 @@ public class Manager{
return fg; return fg;
} }
public FiguraGeometrica getAreaMax() { public FiguraGeometrica getMax(String tipo){
Confrontatore conf = null;
if(tipo.equals("perimetro")) conf = new ConfrontoPerimetro();
if(tipo.equals("area")) conf = new ConfrontoArea();
FiguraGeometrica max = figure.get(0); FiguraGeometrica max = figure.get(0);
for (int i = 1; i < figure.size(); i++) for (int i = 1; i < figure.size(); i++)
if (max.calcolaArea() < figure.get(i).calcolaArea()) if (!conf.confronto(max, figure.get(i)))
max = figure.get(i); max = figure.get(i);
return max; return max;
} }
public FiguraGeometrica getAreaMin() { public FiguraGeometrica getMin(String tipo){
FiguraGeometrica min = figure.get(0); Confrontatore conf = null;
if(tipo.equals("perimetro")) conf = new ConfrontoPerimetro();
if(tipo.equals("area")) conf = new ConfrontoArea();
for (int i = 1; i < figure.size(); i++)
if (min.calcolaArea() > figure.get(i).calcolaArea())
min = figure.get(i);
return min;
}
public FiguraGeometrica getPerimetroMax(){
FiguraGeometrica max = figure.get(0); FiguraGeometrica max = figure.get(0);
for (int i = 1; i < figure.size(); i++) for (int i = 1; i < figure.size(); i++)
if (max.calcolaPerimetro() < figure.get(i).calcolaPerimetro()) if (conf.confronto(max, figure.get(i)))
max = figure.get(i); max = figure.get(i);
return max; return max;
} }
public FiguraGeometrica getPerimetroMin(){
FiguraGeometrica min = figure.get(0);
for (int i = 1; i < figure.size(); i++)
if (min.calcolaPerimetro() > figure.get(i).calcolaPerimetro())
min = figure.get(i);
return min;
}
public void print(PrintStream ps){ public void print(PrintStream ps){
for(FiguraGeometrica fg: figure) for(FiguraGeometrica fg: figure)
fg.print(ps); fg.print(ps);

View File

@ -9,8 +9,8 @@ public class Test {
PrintStream ps = System.out; PrintStream ps = System.out;
ps.println("Figura con area maggiore"); ps.println("Figura con area maggiore");
manager.getAreaMax().print(ps); manager.getMax("area").print(ps);
ps.println("Figura con perimetro minore"); ps.println("Figura con perimetro minore");
manager.getPerimetroMin().print(ps); manager.getMin("perimetro").print(ps);
} }
} }