[POO] aula11 ex3 added

This commit is contained in:
TiagoRG 2023-05-09 09:30:50 +01:00
parent e40301c1e3
commit fa5b57eff9
3 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package aula11.ex3;
import java.util.LinkedList;
public class Customer {
private int customerId;
private LinkedList<Double> meterReadings;
public Customer(int customerId, LinkedList<Double> meterReadings) {
this.customerId = customerId;
this.meterReadings = meterReadings;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public LinkedList<Double> getMeterReadings() {
return meterReadings;
}
public void setMeterReadings(LinkedList<Double> meterReadings) {
this.meterReadings = meterReadings;
}
}

View File

@ -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<Customer> customers = new LinkedList<Customer>();
public void load(String file) {
LinkedList<String> 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<Double> 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);
}
}
}
}

View File

@ -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");
}
}