From fa5b57eff9c0f3918b024e139b294f346ca09e1e Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 9 May 2023 09:30:50 +0100 Subject: [PATCH] [POO] aula11 ex3 added --- .../poo/src/aula11/ex3/Customer.java | 29 +++++++++ .../poo/src/aula11/ex3/EnergyUsageReport.java | 60 +++++++++++++++++++ .../aula11/ex3/EnergyUsageReportTester.java | 34 +++++++++++ 3 files changed, 123 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula11/ex3/Customer.java create mode 100644 1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReport.java create mode 100644 1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReportTester.java diff --git a/1ano/2semestre/poo/src/aula11/ex3/Customer.java b/1ano/2semestre/poo/src/aula11/ex3/Customer.java new file mode 100644 index 0000000..40e56b5 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex3/Customer.java @@ -0,0 +1,29 @@ +package aula11.ex3; + +import java.util.LinkedList; + +public class Customer { + private int customerId; + private LinkedList meterReadings; + + public Customer(int customerId, LinkedList meterReadings) { + this.customerId = customerId; + this.meterReadings = meterReadings; + } + + public int getCustomerId() { + return customerId; + } + + public void setCustomerId(int customerId) { + this.customerId = customerId; + } + + public LinkedList getMeterReadings() { + return meterReadings; + } + + public void setMeterReadings(LinkedList meterReadings) { + this.meterReadings = meterReadings; + } +} diff --git a/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReport.java b/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReport.java new file mode 100644 index 0000000..f99fd2c --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReport.java @@ -0,0 +1,60 @@ +package aula11.ex3; + +import utils.MathTools; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; + +public class EnergyUsageReport { + private final LinkedList customers = new LinkedList(); + + public void load(String file) { + LinkedList lines = new LinkedList<>(); + Path path = Paths.get(file); + try { + lines = new LinkedList<>(Files.readAllLines(path)); + } catch (IOException e) { + System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path); + System.exit(1); + } + + for (String line : lines) { + String[] data = line.split("\\|"); + int customerId = Integer.parseInt(data[0]); + LinkedList meterReadings = new LinkedList<>(Arrays.stream(data).toList().subList(1, data.length).stream().map(Double::parseDouble).toList()); + customers.add(new Customer(customerId, meterReadings)); + } + } + + public void addCustomer(Customer newCustomer) { + customers.add(newCustomer); + } + + public void removeCustomer(int id) { + customers.removeIf(customer -> customer.getCustomerId() == id); + } + + public Customer getCustomer(int id) { + return customers.stream().filter(customer -> customer.getCustomerId() == id).findFirst().orElse(null); + } + + public double calculateTotalUsage(int id) { + return MathTools.sum(getCustomer(id).getMeterReadings()); + } + + public void generateReport(String path) { + for (Customer customer : customers) { + try { + Files.writeString(Path.of(path), String.format("Customer ID: %d%nTotal usage: %.1f%n%n", customer.getCustomerId(), calculateTotalUsage(customer.getCustomerId())), (new File(path)).exists() ? java.nio.file.StandardOpenOption.APPEND : java.nio.file.StandardOpenOption.CREATE); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReportTester.java b/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReportTester.java new file mode 100644 index 0000000..6669324 --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex3/EnergyUsageReportTester.java @@ -0,0 +1,34 @@ +package aula11.ex3; + +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; + +public class EnergyUsageReportTester { + + public static void main(String[] args) throws IOException { + + // Create an instance of the EnergyUsageReport class + EnergyUsageReport energyReport = new EnergyUsageReport(); + + // Load the customer data from a text file using the load() method + energyReport.load("datafiles/aula11/ex3/clients.txt"); + + // Add one or more customers to the collection using the addCustomer() method + Customer newCustomer = new Customer(999, new LinkedList<>(List.of(1500.0, 2000.0, 2500.0, 3000.0))); + energyReport.addCustomer(newCustomer); + + // Remove one or more customers from the collection using the removeCustomer() method + energyReport.removeCustomer(1015); + + // Retrieve a customer from the collection using the getCustomer() method + Customer retrievedCustomer = energyReport.getCustomer(1025); + + // Calculate the total energy usage for a specific customer using the calculateTotalUsage() method + double totalEnergyUsage = energyReport.calculateTotalUsage(1003); + System.out.println("Total energy usage for customer 1003: " + totalEnergyUsage); + + // Generate a report of all customers and their total energy usage using the generateReport() method + energyReport.generateReport("datafiles/aula11/ex3/energy_report.txt"); + } +}