Skip to content
Snippets Groups Projects
Commit 70f481de authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

première version correction

parent 7b7f4395
Branches
No related tags found
No related merge requests found
Pipeline #2716 passed
Showing
with 405 additions and 27 deletions
public class App {
public static void main(String[] args){
}
}
package agency;
import util.TimeProvider;
public abstract class AbstractVehicle implements Vehicle {
private static final int MINIMAL_YEAR_VALUE = 1900;
private final String brand;
private final String model;
private final int productionYear;
@Override
public int hashCode() {
int result = brand.hashCode();
result = 31 * result + model.hashCode();
result = 31 * result + productionYear;
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
AbstractVehicle that = (AbstractVehicle) o;
if (productionYear != that.productionYear) return false;
if (!brand.equals(that.brand)) return false;
return model.equals(that.model);
}
AbstractVehicle(String brand, String model, int productionYear) {
int currentYearValue = TimeProvider.currentYearValue();
if(productionYear < MINIMAL_YEAR_VALUE || productionYear > currentYearValue)
throw new IllegalArgumentException(productionYear + " is not a correct year (comprised between "
+ MINIMAL_YEAR_VALUE + " and " + currentYearValue + ")");
this.brand = brand;
this.productionYear = productionYear;
this.model = model;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String getModel() {
return model;
}
@Override
public int getProductionYear() {
return productionYear;
}
public abstract String kindOfVehicle();
public abstract String vehicleDetails();
@Override
public String toString() {
return kindOfVehicle() + " " + brand + " " + model + " " + productionYear + " (" + vehicleDetails() + ")" + " : "
+ dailyRentalPrice() + "€";
}
}
package agency;
import java.util.List;
import java.util.function.Predicate;
public class App {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[]{new Car("Tim", "Oleon", 2014, 3),
new Car("Tim", "Oleon", 2015, 1),
new Car("Tim", "Oleon", 2016, 2),
new Car("Linux", "Ubuntu", 2017, 3),
new Car("Linux", "Ubuntu", 2018, 3),
new Motorbike("Yamamoto", "Ride", 2019, 500),
new Motorbike("Tim", "Ride", 2019, 500)};
RentalAgency agency = new RentalAgency();
for (Vehicle car : vehicles)
agency.add(car);
Predicate<Vehicle> criterion1 = new MaxPriceCriterion(100);
Predicate<Vehicle> criterion2 = new BrandCriterion("Tim");
IntersectionCriterion<Vehicle> intersectionCriterion = new IntersectionCriterion<>(List.of(criterion1, criterion2));
System.out.println(agency.select(intersectionCriterion));
}
}
package agency;
import java.util.function.Predicate;
public class BrandCriterion implements Predicate<Vehicle> {
private final String brand;
BrandCriterion(String brand) {
this.brand = brand;
}
@Override
public boolean test(Vehicle vehicle) {
return vehicle.getBrand().equals(brand);
}
}
package agency;
import util.TimeProvider;
public class Car extends AbstractVehicle {
private static final int MAXIMUM_AGE_FOR_NEW_CAR = 5;
private final int numberOfSeats;
private static final int PRICE_PER_SEAT_FOR_NEW_CAR = 40;
private static final int PRICE_PER_SEAT_FOR_OLD_CAR = 20;
public boolean isNew(){
return age() <= MAXIMUM_AGE_FOR_NEW_CAR;
}
private int age() {
return TimeProvider.currentYearValue() - getProductionYear();
}
public Car(String brand, String model, int productionYear, int numberOfSeats) {
super(brand, model, productionYear);
if (numberOfSeats < 1)
throw new IllegalArgumentException("Cannot construct a car with "+ numberOfSeats+ " seats");
this.numberOfSeats = numberOfSeats;
}
@Override
public double dailyRentalPrice() {
final int priceBySeat = isNew() ? PRICE_PER_SEAT_FOR_NEW_CAR : PRICE_PER_SEAT_FOR_OLD_CAR;
return priceBySeat * numberOfSeats;
}
@Override
public String kindOfVehicle() {
return "Car";
}
@Override
public String vehicleDetails() {
return numberOfSeats + " " + (hasSeveralSeats() ? "seats" : "seat");
}
private boolean hasSeveralSeats() {
return numberOfSeats > 1;
}
}
package agency;
import java.util.Objects;
public class Client {
private final String firstName;
private final String lastName;
private final int yearOfBirth;
public Client(String firstName, String lastName, int yearOfBirth) {
this.firstName = firstName;
this.lastName = lastName;
this.yearOfBirth = yearOfBirth;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getYearOfBirth() {
return yearOfBirth;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Client client = (Client) o;
if (yearOfBirth != client.yearOfBirth) return false;
if (!Objects.equals(firstName, client.firstName)) return false;
return Objects.equals(lastName, client.lastName);
}
@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + yearOfBirth;
return result;
}
}
package agency;
import java.util.List;
import java.util.function.Predicate;
public class IntersectionCriterion<T> implements Predicate<T> {
private final List<Predicate<T>> predicates;
IntersectionCriterion(List<Predicate<T>> predicates) {
this.predicates = predicates;
}
@Override
public boolean test(T t) {
for (Predicate<T> predicate : predicates){
if (!predicate.test(t)){
return false;
}
}
return true;
}
}
package agency;
import java.util.function.Predicate;
public class MaxPriceCriterion implements Predicate<Vehicle> {
private final double maxPrice;
MaxPriceCriterion(double maxPrice) {
this.maxPrice = maxPrice;
}
@Override
public boolean test(Vehicle vehicle) {
return vehicle.dailyRentalPrice() <= maxPrice;
}
}
package agency;
public class Motorbike extends AbstractVehicle {
private static final double PRICE_PER_UNIT_OF_CYLINDER_CAPACITY = 0.25;
private static final int MINIMAL_CAPACITY = 50;
private static final String UNIT_OF_CYLINDER_CAPACITY = "cm³";
private final int cylinderCapacity;
Motorbike(String brand, String model, int productionYear, int cylinderCapacity) {
super(brand, model, productionYear);
if (!(cylinderCapacity < MINIMAL_CAPACITY))
throw new IllegalArgumentException("Incorrect cylinder capacity "+ cylinderCapacity + "(must be at least " + MINIMAL_CAPACITY +")");
this.cylinderCapacity = cylinderCapacity;
}
@Override
public double dailyRentalPrice() {
return cylinderCapacity * PRICE_PER_UNIT_OF_CYLINDER_CAPACITY;
}
@Override
public String kindOfVehicle() {
return "Motorbike";
}
@Override
public String vehicleDetails() {
return cylinderCapacity + UNIT_OF_CYLINDER_CAPACITY;
}
}
package agency;
import java.util.*;
import java.util.function.Predicate;
public class RentalAgency {
private final List<Vehicle> vehicles = new ArrayList<>();
private final Map<Client, Vehicle> rentedVehicles = new HashMap<>();
void remove(Vehicle vehicle){
requiresExisting(vehicle);
vehicles.remove(vehicle);
}
private void requiresExisting(Vehicle vehicle) {
if(!contains(vehicle)) {
throw new UnknownVehicleException(vehicle);
}
}
boolean add(Vehicle vehicle){
if(contains(vehicle)) {
return false;
}
vehicles.add(vehicle);
return true;
}
double rentVehicle(Client client, Vehicle vehicle)
throws UnknownVehicleException, IllegalStateException{
requiresExisting(vehicle);
requiresNoVehicleRentedBy(client);
requiresAvailable(vehicle);
rentedVehicles.put(client, vehicle);
return vehicle.dailyRentalPrice();
}
private void requiresNoVehicleRentedBy(Client client) {
if(aVehicleIsRentedBy(client)){
throw new IllegalStateException(client + " already rents a vehicle.");
}
}
private void requiresAvailable(Vehicle vehicle) {
if(vehicleIsRented(vehicle)){
throw new IllegalStateException(vehicle + " is already rented.");
}
}
private boolean vehicleIsRented(Vehicle vehicle) {
return rentedVehicles.containsValue(vehicle);
}
private boolean aVehicleIsRentedBy(Client client) {
return rentedVehicles.containsKey(client);
}
private boolean contains(Vehicle vehicle) {
return vehicles.contains(vehicle);
}
void returnVehicle(Client client){
rentedVehicles.remove(client);
}
Collection<Vehicle> allRentedVehicles(){
return rentedVehicles.values();
}
List<Vehicle> select(Predicate<Vehicle> criterion){
List<Vehicle> selectedVehicles = new ArrayList<>();
for (Vehicle vehicle : vehicles){
if (criterion.test(vehicle)){
selectedVehicles.add(vehicle);
}
}
return selectedVehicles;
}
public List<Vehicle> getVehicles(){
return vehicles;
}
public void printVehicles(){
for(Vehicle vehicle : vehicles){
System.out.println(vehicle);
}
}
}
package agency;
public class UnknownVehicleException extends RuntimeException {
private final Vehicle vehicle;
public UnknownVehicleException(Vehicle vehicle) {
this.vehicle = vehicle;
}
@Override
public String getMessage() {
return vehicle.toString() + " is not an available vehicle in the agency.";
}
}
package agency;
public interface Vehicle {
String getBrand();
String getModel();
int getProductionYear();
double dailyRentalPrice();
boolean equals(Object o);
String toString();
}
package agency; package agency;
class TestCar { import org.junit.jupiter.api.BeforeEach;
// TODO : décommenter le code ci-dessous pour tester la tâche 2. import org.junit.jupiter.api.Test;
/* import util.TimeProvider;
import java.time.LocalDateTime;
import java.util.Objects;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
class TestCar {
Car catalinaCar = new Car(apple, catalina, catalinaYear, catalinaNumberOfSeats); Car catalinaCar = new Car(apple, catalina, catalinaYear, catalinaNumberOfSeats);
Car windows95Car = new Car(microsoft, windows95, windows95Year, windows95NumberOfSeats); Car windows95Car = new Car(microsoft, windows95, windows95Year, windows95NumberOfSeats);
static final String apple = "Apple"; static final String apple = "Apple";
...@@ -109,5 +116,4 @@ class TestCar { ...@@ -109,5 +116,4 @@ class TestCar {
String expectedStringWin = "Car Microsoft Windows95 1995 (1 seat) : 20.0€"; String expectedStringWin = "Car Microsoft Windows95 1995 (1 seat) : 20.0€";
assertThat(windows95Car).returns(expectedStringWin, Objects::toString); assertThat(windows95Car).returns(expectedStringWin, Objects::toString);
} }
*/
} }
package agency; package agency;
class TestClient { import org.junit.jupiter.api.BeforeEach;
// TODO : décommenter le code ci-dessous pour tester la classe Client. import org.junit.jupiter.api.Test;
/*
import static org.assertj.core.api.Assertions.assertThat;
class TestClient {
private Client arnaud; private Client arnaud;
@BeforeEach @BeforeEach
...@@ -23,5 +25,4 @@ class TestClient { ...@@ -23,5 +25,4 @@ class TestClient {
void testGetYearOfBirth(){ void testGetYearOfBirth(){
assertThat(arnaud.getYearOfBirth()).isEqualTo(1981); assertThat(arnaud.getYearOfBirth()).isEqualTo(1981);
} }
*/
} }
package agency; package agency;
class TestRentalAgency extends agency.TestCar { import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
// TODO : décommenter le code ci-dessous pour tester la tâche 2. import static org.assertj.core.api.Assertions.*;
/*
class TestRentalAgency extends TestCar{
private RentalAgency rentalAgency; private RentalAgency rentalAgency;
@BeforeEach @BeforeEach
...@@ -34,21 +36,13 @@ class TestRentalAgency extends agency.TestCar { ...@@ -34,21 +36,13 @@ class TestRentalAgency extends agency.TestCar {
assertThat(rentalAgency.getVehicles()).doesNotContain(catalinaCar); assertThat(rentalAgency.getVehicles()).doesNotContain(catalinaCar);
} }
*/
// TODO : décommenter le code ci-dessous pour tester la méthode select.
/*
@Test @Test
void testRemovingNonExistingVehicle(){ void testRemovingNonExistingVehicle(){
assertThatThrownBy(() -> rentalAgency.remove(catalinaCar)) assertThatThrownBy(() -> rentalAgency.remove(catalinaCar))
.isInstanceOf(UnknownVehicleException.class) .isInstanceOf(UnknownVehicleException.class)
.hasMessageContaining(catalinaCar.toString()); .hasMessageContaining(catalinaCar.toString());
} }
*/
// TODO : décommenter le code ci-dessous pour tester la tâche 6.
/*
@Test @Test
void testSelect(){ void testSelect(){
rentalAgency.add(catalinaCar); rentalAgency.add(catalinaCar);
...@@ -61,8 +55,8 @@ class TestRentalAgency extends agency.TestCar { ...@@ -61,8 +55,8 @@ class TestRentalAgency extends agency.TestCar {
private Client arnaud = new Client("Arnaud", "Labourel", 1981); private final Client arnaud = new Client("Arnaud", "Labourel", 1981);
private Client paul = new Client("Paul", "Calcul", 2018); private final Client paul = new Client("Paul", "Calcul", 2018);
@Test @Test
void testRentingVehicles(){ void testRentingVehicles(){
...@@ -96,5 +90,4 @@ class TestRentalAgency extends agency.TestCar { ...@@ -96,5 +90,4 @@ class TestRentalAgency extends agency.TestCar {
.isInstanceOf(UnknownVehicleException.class) .isInstanceOf(UnknownVehicleException.class)
.hasMessageContaining(catalinaCar.toString()); .hasMessageContaining(catalinaCar.toString());
} }
*/
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment