package preparazione_esame; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; public class Esame7 { public static void main(String[] args) { ArrayList ristoranti = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader("src/preparazione_esame/esame7.txt")); PrintWriter pw1 = new PrintWriter(new FileWriter("esame7-1.txt")); PrintWriter pw2 = new PrintWriter(new FileWriter("esame7-2.txt")); PrintWriter pw3 = new PrintWriter(new FileWriter("esame7-3.txt")); PrintWriter pw4 = new PrintWriter(new FileWriter("esame7-4.txt")); PrintWriter pw5 = new PrintWriter(new FileWriter("esame7-5.txt"))) { /* * Acquisizione input */ String linea = br.readLine(); String nome; int stelle; String temp; double prezzoMedio; String[] coords; double[] coordinate = new double[2]; while (linea != null) { if (!linea.isEmpty()) { nome = linea.trim(); stelle = br.readLine().trim().length(); temp = br.readLine().trim(); prezzoMedio = Double.parseDouble(temp.substring(0, temp.length() - 4)); coords = br.readLine().split(","); coordinate[0] = Double.parseDouble(coords[0].trim()); coordinate[1] = Double.parseDouble(coords[1].trim()); ristoranti.add(new Ristorante(nome, stelle, prezzoMedio, coordinate)); } linea = br.readLine(); } for (Ristorante r : ristoranti) { System.out.println(r.asString()); } /* * Elaborazione */ // punto 1 for (Ristorante r : ristoranti) { if (r.getPrezzoMedio() < 40.0) { System.out.println("punto 1: " + r.asString()); pw1.println(r.asString()); } } // punto 2 HashMap> stelleRistorante = new HashMap<>(); ArrayList tempList; for (Ristorante r : ristoranti) { if (stelleRistorante.get(r.getStelle()) == null) { tempList = new ArrayList<>(); tempList.add(r); stelleRistorante.put(r.getStelle(), tempList); } else { stelleRistorante.get(r.getStelle()).add(r); } } for (Integer i : stelleRistorante.keySet()) { for (Ristorante r : stelleRistorante.get(i)) { System.out.println(r.asString()); } } double avg = 0; for (Integer i : stelleRistorante.keySet()) { System.out.print("Media ristoranti " + i + " stelle: "); pw2.print("Media ristoranti " + i + " stelle: "); for (Ristorante r : stelleRistorante.get(i)) { avg += r.getPrezzoMedio(); } System.out.println(avg / stelleRistorante.get(i).size()); pw2.println(avg / stelleRistorante.get(i).size()); } // punto 3 System.out.println("punto 3:"); for (Ristorante r : ristoranti) { if (r.getStelle() >= 2 && distanza(r, new double[] { 40, 10 }) < 5) { System.out.println(r.asString()); pw3.println(r.asString()); } } // punto 4 System.out.println("punto 4:"); for (Ristorante r : ristoranti) { for (Ristorante anotherR : ristoranti) { if (!r.getNome().equals(anotherR.getNome()) && r.getStelle() == 5 && anotherR.getStelle() == 5 && distanza(r, anotherR) < 5) { System.out.println(r.asString()); pw4.println(r.asString()); } } } // punto 5 Ristorante[] ristoArray = ristoranti.toArray(new Ristorante[] {}); Ristorante ristoTemp; for (int i = 0; i < ristoArray.length - 1; i++) { if (ristoArray[i].getPrezzoMedio() < ristoArray[i + 1].getPrezzoMedio()) { ristoTemp = ristoArray[i]; ristoArray[i] = ristoArray[i + 1]; ristoArray[i + 1] = ristoTemp; } } System.out.println("punto 5:"); for (Ristorante ristorante : ristoArray) { System.out.println(ristorante.asString()); pw5.println(ristorante.asString()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static double distanza(Ristorante r, Ristorante anotherR) { return Math.sqrt(Math.pow(r.getCoordinate()[0] - anotherR.getCoordinate()[0], 2) + Math.pow(r.getCoordinate()[1] - anotherR.getCoordinate()[1], 2)); } private static double distanza(Ristorante r, double[] ds) { return Math.sqrt(Math.pow(r.getCoordinate()[0] - ds[0], 2) + Math.pow(r.getCoordinate()[1] - ds[1], 2)); } }