From dd843435b047559d9f045a66c9ff860b2b570c0a Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Tue, 30 May 2023 14:38:01 +0100 Subject: [PATCH] [POO] ATP1 added --> Exercise made in Codecheck --- .../poo/src/aval/atp1/RainfallInfo.java | 38 ++++++ .../poo/src/aval/atp1/RainfallTest.java | 116 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 1ano/2semestre/poo/src/aval/atp1/RainfallInfo.java create mode 100644 1ano/2semestre/poo/src/aval/atp1/RainfallTest.java diff --git a/1ano/2semestre/poo/src/aval/atp1/RainfallInfo.java b/1ano/2semestre/poo/src/aval/atp1/RainfallInfo.java new file mode 100644 index 0000000..1a1df79 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/atp1/RainfallInfo.java @@ -0,0 +1,38 @@ +package aval.atp1; + +import java.time.LocalDate; +// YOU MAY ADD IMPORTS HERE + +public class RainfallInfo { + private LocalDate date; + private String location; + private double rainfall; + public RainfallInfo(LocalDate date, String location, double rainfall) { + this.date = date; + this.location = location; + this.rainfall=rainfall; + } + public LocalDate date() { + return date; + } + + public String location() { + return location; + } + + public double rainfall() { + return rainfall; + } + + // YOU MAY ADD METHODS HERE + @Override + public String toString() { + return String.format("(%s, %s, %.1f)", date(), location(), rainfall()); + } + + + @Override + public int hashCode() { + return date.hashCode() | location.hashCode() | Double.hashCode(rainfall); + }; +} diff --git a/1ano/2semestre/poo/src/aval/atp1/RainfallTest.java b/1ano/2semestre/poo/src/aval/atp1/RainfallTest.java new file mode 100644 index 0000000..1f3c55c --- /dev/null +++ b/1ano/2semestre/poo/src/aval/atp1/RainfallTest.java @@ -0,0 +1,116 @@ +package aval.atp1; + +import java.io.FileReader; +import java.io.IOError; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.time.LocalDate; +import java.time.Month; +import java.util.*; +// YOU MAY ADD IMPORTS HERE + +public class RainfallTest { + public static List loadData(String filePath) { + List data = null; + try { + // Check if file is csv + if (!filePath.endsWith(".csv")) { + return null; + } + + if (!Files.exists(Paths.get(filePath))) { + return null; + } + + // 1) Read the file and add RainfallInfo objects to a list. + Scanner sc = new Scanner(new FileReader(filePath)); + sc.nextLine(); // skip header + + while (sc.hasNextLine()) { + String[] datacomps = sc.nextLine().split(","); + LocalDate date = LocalDate.parse(datacomps[0]); + String location = datacomps[1]; + double rainfall = Double.parseDouble(datacomps[2]); + if (data == null) data = new ArrayList<>(); + data.add(new RainfallInfo(date, location, rainfall)); + } + } catch (Exception e) { + throw new IOError(e.getCause()); // repackage as error + } + return data; + } + public static String printLocationData(List data, String loc) { + System.out.printf("Rainfall for location %s:\n", loc); + // 2) Print rainfall values for the given location + data.stream().filter(r -> r.location().equals(loc)).forEach(System.out::println); + return ""; + } + + public static Map totalPerMonth(List data) { + // 3) Calculate and return a map with the total rainfall per month + Map map = new HashMap<>(); + data.forEach(r -> { + Month m = r.date().getMonth(); + if (map.containsKey(m)) { + map.put(m, map.get(m) + r.rainfall()); + } else { + map.put(m, r.rainfall()); + } + }); + return map; + } + + public static String printMapSorted (Map map) { + // 4) Print map sorted by key + map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(e -> System.out.printf("%s: %.1f\n", e.getKey(), e.getValue())); + return ""; + } + + // YOU MAY ADD METHODS HERE + public static String main() { + Test.lst = null; Test.map = null; + // Load the file data to the list + List rainfallData = loadData("rainfall_data.csv"); + System.out.printf("Data size: %d\n", rainfallData.size()); + System.out.printf("Data hash: %d\n", rainfallData.hashCode()); + System.out.println(); + + // Show data for a single town + printLocationData(rainfallData, "Braga"); + System.out.println(); + + // Find total rainfall per month + Map rainfallPerMonth = totalPerMonth (rainfallData); + + // Print sorted results + printMapSorted(rainfallPerMonth); + return ""; + } + + public static void main(String[] args) { + main(); + } +} + +class Test { + // Variables used in unit tests (DON'T USE IN YOUR IMPLEMENTATION!): + private static LocalDate[] dates = { + LocalDate.parse("2023-03-02"), + LocalDate.parse("2023-04-17"), + LocalDate.parse("2023-05-27"), + }; + static List lst = new ArrayList<>(); + static Map map = new HashMap<>(); + + static { + lst.add(new RainfallInfo(dates[2], "Aveiro", 2.2)); + lst.add(new RainfallInfo(dates[1], "Braga", 3.0)); + lst.add(new RainfallInfo(dates[0], "Aveiro", 0.0)); + lst.add(new RainfallInfo(dates[2], "Braga", 4.0)); + lst.add(new RainfallInfo(dates[1], "Aveiro", 1.1)); + map.put(Month.JULY,7.7); + map.put(Month.MAY,5.5); + map.put(Month.JUNE,6.6); + map.put(Month.MARCH,3.3); + } +} \ No newline at end of file