From ce924919d78375d14be2dbdd35f7b118708e0f01 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Mon, 26 Jun 2023 16:14:29 +0100 Subject: [PATCH] [POO] 2022-2023 final exam solution added --- .../poo/src/aval/ef_2223/Classes/Booking.java | 54 ++++ .../aval/ef_2223/Classes/CampingService.java | 162 ++++++++++++ .../Classes/CampingSpaces/CampingSpace.java | 75 ++++++ .../Classes/CampingSpaces/CarSpace.java | 26 ++ .../Classes/CampingSpaces/CaravanSpace.java | 25 ++ .../Classes/CampingSpaces/TentSpace.java | 23 ++ .../poo/src/aval/ef_2223/Classes/Client.java | 58 +++++ .../src/aval/ef_2223/Enums/ClientType.java | 13 + .../poo/src/aval/ef_2223/Enums/SpaceType.java | 23 ++ .../Interfaces/CampingServiceInterface.java | 32 +++ .../poo/src/aval/ef_2223/POO2223.java | 231 ++++++++++++++++++ .../poo/src/aval/ef_2223/POO_2223Ep.txt | 43 ++++ .../poo/src/aval/ef_2223/bookings.txt | 20 ++ 13 files changed, 785 insertions(+) create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/Booking.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingService.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CampingSpace.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CarSpace.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CaravanSpace.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/TentSpace.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Classes/Client.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Enums/ClientType.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Enums/SpaceType.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/Interfaces/CampingServiceInterface.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/POO2223.java create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/POO_2223Ep.txt create mode 100644 1ano/2semestre/poo/src/aval/ef_2223/bookings.txt diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/Booking.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/Booking.java new file mode 100644 index 0000000..3edd6e5 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/Booking.java @@ -0,0 +1,54 @@ +package aval.ef_2223.Classes; + +import aval.ef_2223.Classes.CampingSpaces.CampingSpace; + +import java.time.LocalDate; +import java.util.Objects; + +public class Booking { + private CampingSpace campingSpace; + private LocalDate startDate; + private LocalDate endDate; + + public Booking(CampingSpace campingSpace, LocalDate startDate, LocalDate endDate) { + this.campingSpace = campingSpace; + this.startDate = startDate; + this.endDate = endDate; + } + + public CampingSpace getCampingSpace() { + return campingSpace; + } + + public void setCampingSpace(CampingSpace campingSpace) { + this.campingSpace = campingSpace; + } + + public LocalDate getStartDate() { + return startDate; + } + + public void setStartDate(LocalDate startDate) { + this.startDate = startDate; + } + + public LocalDate getEndDate() { + return endDate; + } + + public void setEndDate(LocalDate endDate) { + this.endDate = endDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Booking booking)) return false; + return Objects.equals(getCampingSpace(), booking.getCampingSpace()) && Objects.equals(getStartDate(), booking.getStartDate()) && Objects.equals(getEndDate(), booking.getEndDate()); + } + + @Override + public int hashCode() { + return Objects.hash(getCampingSpace(), getStartDate(), getEndDate()); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingService.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingService.java new file mode 100644 index 0000000..a491c71 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingService.java @@ -0,0 +1,162 @@ +package aval.ef_2223.Classes; + +import aval.ef_2223.Classes.CampingSpaces.CampingSpace; +import aval.ef_2223.Enums.ClientType; +import aval.ef_2223.Enums.SpaceType; +import aval.ef_2223.Interfaces.CampingServiceInterface; + +import java.time.LocalDate; +import java.util.*; + +public class CampingService implements CampingServiceInterface { + private final List campingSpaces = new ArrayList<>(); + private final List clients = new ArrayList<>(); + private final Map> bookings = new HashMap<>(); + private String name; + private String address; + + public CampingService(String name, String address) { + this.name = name; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public boolean registerClient(int taxId, String name, ClientType type) { + try { + clients.add(new Client(taxId, name, type)); + return true; + } catch (Exception ignored) { + return false; + } + } + + @Override + public Client getClient(int taxId) { + return clients.stream().filter(client -> client.getTaxId() == taxId).findFirst().orElse(null); + } + + @Override + public void addCampingSpace(CampingSpace campingSpace) { + campingSpaces.add(campingSpace); + } + + @Override + public void addCampingSpaces(Collection campingSpaces) { + this.campingSpaces.addAll(campingSpaces); + } + + @Override + public boolean checkAvailable(CampingSpace campingSpace, LocalDate startDate, LocalDate endDate) { + if (campingSpace == null || startDate == null || endDate == null) return false; + if (startDate.isAfter(endDate)) return false; + if (!campingSpaces.contains(campingSpace)) return false; + for (Map.Entry> bookingList : bookings.entrySet()) { + for (Booking booking : bookingList.getValue()) + if (booking.getCampingSpace().equals(campingSpace)) + return !(startDate.isBefore(booking.getEndDate()) && endDate.isAfter(booking.getStartDate())); + } + return true; + } + + @Override + public List findAvailableCampingSpaces(SpaceType spaceType, LocalDate fromDate, int duration, int[] minDimensions) { + List availableCampingSpaces = new ArrayList<>(); + + for (CampingSpace campingSpace : campingSpaces) + if (checkAvailable(campingSpace, fromDate, fromDate.plusDays(duration)) && campingSpace.getSizes()[0] >= minDimensions[0] && campingSpace.getSizes()[1] >= minDimensions[1]) + availableCampingSpaces.add(campingSpace); + return availableCampingSpaces; + } + + @Override + public boolean bookCampingSpace(Client client, CampingSpace campingSpace, LocalDate startDate, int duration) { + if (client == null || campingSpace == null || startDate == null) return false; + if (duration < 1) return false; + if (!clients.contains(client)) return false; + if (!campingSpaces.contains(campingSpace)) return false; + if (client.getClientType() == ClientType.NORMAL && campingSpace.getType() == SpaceType.CARAVAN) return false; + if (duration > (campingSpace.getType() == SpaceType.CARAVAN ? 3 * 365 : (campingSpace.getType() == SpaceType.CAR ? 3 * 30 : 15))) + return false; + if (!checkAvailable(campingSpace, startDate, startDate.plusDays(duration))) return false; + bookings.putIfAbsent(client, new ArrayList<>()); + bookings.get(client).add(new Booking(campingSpace, startDate, startDate.plusDays(duration))); + return true; + } + + @Override + public double calculateTotalCost(CampingSpace campingSpace, int duration) { + return campingSpace.getPricePerNight() * duration; + } + + @Override + public List listBookings() { + List bookingsList = new ArrayList<>(); + for (Map.Entry> bookingList : bookings.entrySet()) { + for (Booking booking : bookingList.getValue()) + bookingsList.add(String.format("%s - [%s - %s] %s space located in %s, with dimension %dx%d, %.2f/day, total cost %.2f%n", + bookingList.getKey().toString(), + booking.getStartDate().toString(), + booking.getEndDate().toString(), + booking.getCampingSpace().getType().toString(), + booking.getCampingSpace().getLocation(), + booking.getCampingSpace().getSizes()[0], + booking.getCampingSpace().getSizes()[1], + booking.getCampingSpace().getPricePerNight(), + calculateTotalCost(booking.getCampingSpace(), (booking.getStartDate().until(booking.getEndDate()).getDays() + 1) * (booking.getStartDate().until(booking.getEndDate()).getMonths() + 1) * (booking.getStartDate().until(booking.getEndDate()).getYears() + 1)))); + } + return bookingsList; + } + + @Override + public List listBookings(SpaceType spaceType) { + List bookingsList = new ArrayList<>(); + for (Map.Entry> bookingList : bookings.entrySet()) { + for (Booking booking : bookingList.getValue()) + if (booking.getCampingSpace().getType() == spaceType) + bookingsList.add(booking.toString()); + } + return bookingsList; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CampingService that)) return false; + return Objects.equals(getName(), that.getName()) && Objects.equals(getAddress(), that.getAddress()); + } + + @Override + public int hashCode() { + return Objects.hash(getName(), getAddress()); + } + + public List getAvailableSpacesByTotalArea(LocalDate localDate, int area) { + List availableSpaces = new ArrayList<>(); + for (CampingSpace campingSpace : campingSpaces) + if (campingSpace.getSizes()[0] * campingSpace.getSizes()[1] >= area) + if (checkAvailable(campingSpace, localDate, localDate.plusDays(60))) + availableSpaces.add(campingSpace); + return availableSpaces; + } + + @Override + public String toString() { + return getName() + ", " + getAddress(); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CampingSpace.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CampingSpace.java new file mode 100644 index 0000000..d109a6e --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CampingSpace.java @@ -0,0 +1,75 @@ +package aval.ef_2223.Classes.CampingSpaces; + +import aval.ef_2223.Enums.SpaceType; + +import java.util.Arrays; +import java.util.Objects; + +public abstract class CampingSpace { + private SpaceType spaceType; + private String location; + private int[] sizes; + private double pricePerNight; + + + public CampingSpace(String location, int[] sizes, double pricePerNight) { + this.location = location; + this.sizes = sizes; + this.pricePerNight = pricePerNight; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public int[] getSizes() { + return sizes; + } + + public void setSizes(int[] sizes) { + this.sizes = sizes; + } + + public double getPricePerNight() { + return pricePerNight; + } + + public void setPricePerNight(double pricePerNight) { + this.pricePerNight = pricePerNight; + } + + public SpaceType getType() { + return spaceType; + } + + public void setSpaceType(SpaceType spaceType) { + this.spaceType = spaceType; + } + + public boolean checkMinDimentions(CampingSpace space2, int[] dimentions) { + return space2.getSizes()[0] >= dimentions[0] && space2.getSizes()[1] >= dimentions[1]; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CampingSpace that)) return false; + return Double.compare(that.getPricePerNight(), getPricePerNight()) == 0 && getType() == that.getType() && Objects.equals(getLocation(), that.getLocation()) && Arrays.equals(getSizes(), that.getSizes()); + } + + @Override + public int hashCode() { + int result = Objects.hash(getType(), getLocation(), getPricePerNight()); + result = 31 * result + Arrays.hashCode(getSizes()); + return result; + } + + @Override + public String toString() { + return String.format("%s space located in %s, with dimension %dx%d, %.2f/day", getType(), getLocation(), getSizes()[0], getSizes()[1], getPricePerNight()); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CarSpace.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CarSpace.java new file mode 100644 index 0000000..74f0ef8 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CarSpace.java @@ -0,0 +1,26 @@ +package aval.ef_2223.Classes.CampingSpaces; + + +import aval.ef_2223.Enums.SpaceType; + +import java.util.Objects; + +public class CarSpace extends CampingSpace { + + public CarSpace(String location, int[] sizes, double pricePerNight) { + super(location, sizes, pricePerNight); + super.setSpaceType(SpaceType.CAR); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CarSpace carSpace)) return false; + return super.equals(o); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode()); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CaravanSpace.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CaravanSpace.java new file mode 100644 index 0000000..1c47160 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/CaravanSpace.java @@ -0,0 +1,25 @@ +package aval.ef_2223.Classes.CampingSpaces; + +import aval.ef_2223.Enums.SpaceType; + +import java.util.Objects; + +public class CaravanSpace extends CampingSpace { + + public CaravanSpace(String location, int[] sizes, double pricePerNight) { + super(location, sizes, pricePerNight); + super.setSpaceType(SpaceType.CARAVAN); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CaravanSpace that)) return false; + return super.equals(o); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode()); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/TentSpace.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/TentSpace.java new file mode 100644 index 0000000..7d78bae --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/CampingSpaces/TentSpace.java @@ -0,0 +1,23 @@ +package aval.ef_2223.Classes.CampingSpaces; + +import aval.ef_2223.Enums.SpaceType; + +public class TentSpace extends CampingSpace { + + public TentSpace(String location, int[] sizes, double pricePerNight) { + super(location, sizes, pricePerNight); + super.setSpaceType(SpaceType.TENT); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TentSpace tentSpace)) return false; + return super.equals(o); + } + + @Override + public int hashCode() { + return super.hashCode(); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Classes/Client.java b/1ano/2semestre/poo/src/aval/ef_2223/Classes/Client.java new file mode 100644 index 0000000..b3bfe46 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Classes/Client.java @@ -0,0 +1,58 @@ +package aval.ef_2223.Classes; + +import aval.ef_2223.Enums.ClientType; + +import java.util.Objects; + +public class Client { + private int taxId; + private String name; + private ClientType clientType; + + public Client(int taxId, String name, ClientType clientType) { + this.taxId = taxId; + this.name = name; + this.clientType = clientType; + } + + public int getTaxId() { + return taxId; + } + + public void setTaxId(int taxId) { + this.taxId = taxId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ClientType getClientType() { + return clientType; + } + + public void setClientType(ClientType clientType) { + this.clientType = clientType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Client client)) return false; + return getTaxId() == client.getTaxId() && Objects.equals(getName(), client.getName()) && getClientType() == client.getClientType(); + } + + @Override + public int hashCode() { + return Objects.hash(getTaxId(), getName(), getClientType()); + } + + @Override + public String toString() { + return String.format("%s [%s: %d]", getName(), getClientType(), getTaxId()); + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Enums/ClientType.java b/1ano/2semestre/poo/src/aval/ef_2223/Enums/ClientType.java new file mode 100644 index 0000000..d2bb737 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Enums/ClientType.java @@ -0,0 +1,13 @@ +package aval.ef_2223.Enums; + +public enum ClientType { + NORMAL, MEMBER; + + @Override + public String toString() { + return switch (this) { + case NORMAL -> "Normal"; + case MEMBER -> "Member"; + }; + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Enums/SpaceType.java b/1ano/2semestre/poo/src/aval/ef_2223/Enums/SpaceType.java new file mode 100644 index 0000000..536a4a4 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Enums/SpaceType.java @@ -0,0 +1,23 @@ +package aval.ef_2223.Enums; + +public enum SpaceType { + CARAVAN, CAR, TENT; + + public static SpaceType fromString(String type) { + return switch (type.toLowerCase()) { + case "caravan" -> CARAVAN; + case "car" -> CAR; + case "tent" -> TENT; + default -> null; + }; + } + + @Override + public String toString() { + return switch (this) { + case CARAVAN -> "Caravan"; + case CAR -> "Car"; + case TENT -> "Tent"; + }; + } +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/Interfaces/CampingServiceInterface.java b/1ano/2semestre/poo/src/aval/ef_2223/Interfaces/CampingServiceInterface.java new file mode 100644 index 0000000..a091198 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/Interfaces/CampingServiceInterface.java @@ -0,0 +1,32 @@ +package aval.ef_2223.Interfaces; + +import aval.ef_2223.Classes.CampingSpaces.CampingSpace; +import aval.ef_2223.Classes.Client; +import aval.ef_2223.Enums.ClientType; +import aval.ef_2223.Enums.SpaceType; + +import java.time.LocalDate; +import java.util.Collection; +import java.util.List; + +public interface CampingServiceInterface { + boolean registerClient(int taxId, String name, ClientType type); + + Client getClient(int taxId); + + void addCampingSpace(CampingSpace campingSpace); + + void addCampingSpaces(Collection campingSpaces); + + boolean checkAvailable(CampingSpace campingSpace, LocalDate startDate, LocalDate endDate); + + List findAvailableCampingSpaces(SpaceType spaceType, LocalDate fromDate, int duration, int[] minDimensions); + + boolean bookCampingSpace(Client client, CampingSpace campingSpace, LocalDate startDate, int duration); + + double calculateTotalCost(CampingSpace campingSpace, int duration); + + List listBookings(); + + List listBookings(SpaceType spaceType); +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/POO2223.java b/1ano/2semestre/poo/src/aval/ef_2223/POO2223.java new file mode 100644 index 0000000..efe1626 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/POO2223.java @@ -0,0 +1,231 @@ +package aval.ef_2223; + + +import aval.ef_2223.Classes.CampingService; +import aval.ef_2223.Classes.CampingSpaces.CampingSpace; +import aval.ef_2223.Classes.CampingSpaces.CarSpace; +import aval.ef_2223.Classes.CampingSpaces.CaravanSpace; +import aval.ef_2223.Classes.CampingSpaces.TentSpace; +import aval.ef_2223.Classes.Client; +import aval.ef_2223.Enums.ClientType; +import aval.ef_2223.Enums.SpaceType; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintStream; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +// Notes: +// Não altere o código apresentado *** Do not change the code shown +// Deve apenas completar o código de alinea2, onde indicado *** You should only change the code where indicated in alinea2 +// Deve comentar o código para garantir que vai executando parcialmente *** You may comment the code to test and execute partially + +public class POO2223 { + + public static void main(String[] args) throws FileNotFoundException { + PrintStream fl = new PrintStream("src/aval/ep_2223/POO_2223Ep.txt"); + test(System.out); // executa e escreve na consola *** executes and writes to console + test(fl); // executa e escreve no ficheiro *** executes and writes to file + fl.close(); + } + + private static void test(PrintStream out) { + alinea1(out); + alinea2(out); + } + + // DO NOT CHANGE THIS METHOD + // CREATE THE CLASS DEFINITIONS ACCORDING TO THE DESCRIPTION IN THE EXAM AND THE CODE BELOW + private static void alinea1(PrintStream out) { + out.println("\nAlínea 1) ----------------------------------\n"); + + /* + ** Creating CampingService; Registering clients; Adding camping spaces + */ + + CampingService campingService = new CampingService("Best Camping", "Costa Nova"); + + // Registering 4 clients + campingService.registerClient(123456789, "João Sousa", ClientType.NORMAL); + campingService.registerClient(234567890, "Ana Pereira", ClientType.NORMAL); + campingService.registerClient(501234567, "Arlindo Marques", ClientType.MEMBER); + campingService.registerClient(501987654, "Júlio Pomar", ClientType.MEMBER); + + // Adding 2 caravan spaces + CampingSpace caravan1 = new CaravanSpace("Costa Nova", new int[]{300, 400}, 60); + CampingSpace caravan2 = new CaravanSpace("Costa Nova", new int[]{300, 450}, 70); + campingService.addCampingSpace(caravan1); + campingService.addCampingSpace(caravan2); + + // Adding 3 car spaces + CampingSpace[] carSpaces = new CampingSpace[]{ + new CarSpace("Costa Nova", new int[]{280, 400}, 40), + new CarSpace("Costa Nova", new int[]{250, 350}, 30), + new CarSpace("Costa Nova", new int[]{300, 450}, 45) + }; + campingService.addCampingSpaces(Arrays.asList(carSpaces)); + + // Adding 6 tent spaces, all the same size + for (int i = 0; i < 6; i++) { + campingService.addCampingSpace(new TentSpace("Costa Nova", new int[]{150, 200}, 10)); + } + // Adding 6 larger tent spaces + for (int i = 0; i < 6; i++) { + campingService.addCampingSpace(new TentSpace("Costa Nova", new int[]{200, 350}, 20)); + } + + + /* + ** Book camping spaces + */ + + // NON MEMBER client can not book a caravan space + Client c1 = campingService.getClient(123456789); + boolean result = campingService.bookCampingSpace(c1, caravan1, LocalDate.of(2023, 6, 1), 180); + if (!result) { + out.printf("** Could not book %s space for client %s.\n", caravan1.getType(), c1); + } + + // MEMBER client can book a caravan space + Client c2 = campingService.getClient(501234567); + result = campingService.bookCampingSpace(c2, caravan1, LocalDate.of(2023, 6, 1), 180); + if (!result) { + out.printf("** Could not book %s space for client %s.\n", caravan1.getType(), c2); + } + + // Client c1 books a car space + CampingSpace a1 = carSpaces[0]; + result = campingService.bookCampingSpace(c1, a1, LocalDate.of(2023, 6, 1), 90); + if (!result) { + out.printf("** Could not book %s space for client %s.\n", a1.getType(), c1); + } + + // Same car space cannot be booked for the same period + Client c3 = campingService.getClient(234567890); + result = campingService.bookCampingSpace(c3, a1, LocalDate.of(2023, 8, 1), 60); + if (!result) { + out.printf("** Could not book %s space for client %s.\n", a1.getType(), c3); + } + + // Client c3 books different car space + CampingSpace a2 = carSpaces[1]; + result = campingService.bookCampingSpace(c3, a2, LocalDate.of(2023, 8, 1), 60); + if (!result) { + out.printf("** Could not book %s space for client %s.\n", a2.getType(), c3); + } + + + /* + ** List bookings + */ + List bookings = campingService.listBookings(); + if (!bookings.isEmpty()) { + out.println("\n** All client bookings"); + bookings.forEach(out::print); + } + + } + + // CHANGE THIS METHOD ONLY WHERE INDICATED + private static void alinea2(PrintStream out) { + out.println("\n\nAlínea 2) ----------------------------------\n"); + + + // ** Creating CampingService; Registering clients; Adding camping spaces + + + CampingService campingService = new CampingService("Dunas", "São Jacinto"); + + // Registering clients + campingService.registerClient(123456789, "João Sousa", ClientType.NORMAL); + campingService.registerClient(150987654, "Ana Pereira", ClientType.NORMAL); + campingService.registerClient(157894320, "Carla Moura", ClientType.NORMAL); + campingService.registerClient(501234567, "Arlindo Marques", ClientType.MEMBER); + campingService.registerClient(507654321, "Júlio Pomar", ClientType.MEMBER); + campingService.registerClient(505321987, "Cátia Santos", ClientType.MEMBER); + + // Adding caravan spaces + campingService.addCampingSpace(new CaravanSpace("Costa Nova", new int[]{300, 400}, 60)); + campingService.addCampingSpace(new CaravanSpace("Costa Nova", new int[]{300, 450}, 70)); + + // Adding car spaces + campingService.addCampingSpace(new CarSpace("Costa Nova", new int[]{280, 400}, 40)); + campingService.addCampingSpace(new CarSpace("Costa Nova", new int[]{250, 350}, 30)); + campingService.addCampingSpace(new CarSpace("Costa Nova", new int[]{300, 450}, 45)); + + // Adding tent spaces + for (int i = 0; i < 6; i++) { + campingService.addCampingSpace(new TentSpace("Costa Nova", new int[]{150, 200}, 10)); + campingService.addCampingSpace(new TentSpace("Costa Nova", new int[]{200, 350}, 20)); + } + + + // ** Adicione a seguir o código necessário para a leitura do ficheiro e registo dos alugueres. + // ** Add the code to read from file and register the bookings. + + + Scanner fileReader = null; + try { + fileReader = new Scanner(new File("src/aval/ep_2223/bookings.txt")); + } catch (FileNotFoundException e) { + out.println("File not found"); + return; + } + + while (fileReader.hasNextLine()) { + String line = fileReader.nextLine(); + if (line.startsWith("#")) continue; + String[] parts = line.split(";"); + String[] dateParts = parts[1].split("-"); + LocalDate date = LocalDate.of(Integer.parseInt(dateParts[0]), Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[2])); + int[] sizes = new int[]{Integer.parseInt(parts[4].split("x")[0]), Integer.parseInt(parts[4].split("x")[1])}; + List availableCampingSpaces = campingService.findAvailableCampingSpaces(SpaceType.fromString(parts[3]), date, Integer.parseInt(parts[2]), sizes); + if (availableCampingSpaces.isEmpty()) continue; + CampingSpace space = availableCampingSpaces.get(0); + Client client = campingService.getClient(Integer.parseInt(parts[0])); + campingService.bookCampingSpace(client, space, date, Integer.parseInt(parts[2])); + } + + + // ** Não modifique o metodo a partir daqui. Pode comentar para executar o programa. + // ** Do not modify the method from this point on. You may comment to excute the programme. + + + out.println("\n" + campingService + ": Finished reading file.\n\n"); + + // Deve devolver uma lista vazia (ou null) pois não existem espaços para carro com a dimensão indicada (ou maiores) + // This should return empty (or null) since there are no car spaces with the required dimensions (or larger) + out.println("Looking for car space with dimensions 300x600"); + List available = campingService.findAvailableCampingSpaces(SpaceType.CAR, LocalDate.of(2023, 6, 1), 60, new int[]{300, 600}); + if (available == null || available.isEmpty()) { + out.println("** No options available for the type, dimensions, and dates requested.\n"); + } else { + CampingSpace u = available.get(0); + out.printf("%s, is available for a total price of %.2f.\n", u, campingService.calculateTotalCost(u, 60)); + } + + // Deve devolver uma lista de espaços disponíveis + // This should return a list of available spaces + out.println("Looking for car space with dimensions 300x400"); + available = campingService.findAvailableCampingSpaces(SpaceType.CAR, LocalDate.of(2023, 6, 1), 60, new int[]{300, 400}); + if (available == null || available.isEmpty()) { + out.println("** No options available for the type, dimensions, and dates requested.\n"); + } else { + CampingSpace u = available.get(0); + out.printf("%s, is available for a total price of %.2f.\n", u, campingService.calculateTotalCost(u, 60)); + } + + // listar todos os espaços disponíveis a partir de 2023/12/01 por 60 dias, por ordem decrescente de área total + // list all spaces available from 2023/12/01 for 60 days, in decreasing order of total area + out.println("\n\n" + campingService + ": AVAILABLE SPACES BY AREA"); + campingService.getAvailableSpacesByTotalArea(LocalDate.of(2023, 12, 1), 60).forEach(out::println); + + out.println(); + + + } + +} diff --git a/1ano/2semestre/poo/src/aval/ef_2223/POO_2223Ep.txt b/1ano/2semestre/poo/src/aval/ef_2223/POO_2223Ep.txt new file mode 100644 index 0000000..7d772e3 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/POO_2223Ep.txt @@ -0,0 +1,43 @@ + +Alínea 1) ---------------------------------- + +** Could not book Caravan space for client João Sousa [Normal: 123456789]. +** Could not book Car space for client Ana Pereira [Normal: 234567890]. + +** All client bookings +João Sousa [Normal: 123456789] - [2023-06-01 - 2023-08-30] Car space located in Costa Nova, with dimension 280x400, 40.00/day, total cost 3600.00 +Ana Pereira [Normal: 234567890] - [2023-08-01 - 2023-09-30] Car space located in Costa Nova, with dimension 250x350, 30.00/day, total cost 1800.00 +Arlindo Marques [Member: 501234567] - [2023-06-01 - 2023-11-28] Caravan space located in Costa Nova, with dimension 300x400, 60.00/day, total cost 10080.00 + + +Alínea 2) ---------------------------------- + + +Dunas, São Jacinto: Finished reading file. + + +Looking for car space with dimensions 300x600 +** No options available for the type, dimensions, and dates requested. + +Looking for car space with dimensions 300x400 +Caravan space located in Costa Nova, with dimension 300x400, 60.00/day, is available for a total price of 3600.00. + + +Dunas, São Jacinto: AVAILABLE SPACES BY AREA +Caravan space located in Costa Nova, with dimension 300x400, 60.00/day +Car space located in Costa Nova, with dimension 280x400, 40.00/day +Car space located in Costa Nova, with dimension 250x350, 30.00/day +Car space located in Costa Nova, with dimension 300x450, 45.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day +Tent space located in Costa Nova, with dimension 150x200, 10.00/day +Tent space located in Costa Nova, with dimension 200x350, 20.00/day + diff --git a/1ano/2semestre/poo/src/aval/ef_2223/bookings.txt b/1ano/2semestre/poo/src/aval/ef_2223/bookings.txt new file mode 100644 index 0000000..4753831 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/ef_2223/bookings.txt @@ -0,0 +1,20 @@ +# Fields +# Client tax id; rental start date; rental duration; type of camping space; minimum dimensions +123456789;2023-06-01;150;CARAVAN;250x350 +501234567;2023-06-05;40;CAR;250x400 +150987654;2023-06-10;10;TENT;150x200 +150987654;2023-06-20;60;CAR;200x300 +507654321;2023-06-25;5;TENT;160x200 +501234567;2023-06-30;15;CARAVAN;240x400 +507654321;2023-07-05;30;CAR;260x350 +157894320;2023-07-10;2;TENT;180x250 +509876543;2023-07-15;120;CARAVAN;300x450 +157894320;2023-07-20;20;CAR;210x300 +507654321;2023-07-25;15;TENT;160x240 +501234567;2023-07-30;150;CARAVAN;250x300 +505321987;2023-08-04;90;CAR;220x300 +157894320;2023-08-09;12;TENT;200x400 +505321987;2023-08-14;120;CARAVAN;300x350 +168975432;2023-08-19;6;CAR;200x290 +505321987;2023-08-29;150;CARAVAN;250x390 +157894320;2023-09-03;10;CAR;190x300