-
Cours :
- cours 1 transparents : pdf
- cours 2 transparents : pdf
- cours 3 transparents : pdf
- cours 4 transparents : pdf
- cours 5 transparents : pdf
- cours 6 transparents : pdf
- cours 7 transparents : pdf
- cours 8 transparents : pdf
- cours 9 transparents : pdf
- cours 10 transparents : pdf
- cours 11 transparents : pdf
- cours 12 transparents : pdf
- Documents :
- TD :
- TP :
- TP 8 (pdf)
- TP 9 (pdf)
- TP 10 (pdf), Corrigé TP 10 (pdf)
- Projet astéroïdes (pdf)
Correction TP 10 : TP noté
Type de chambre d’hôtel : énumération RoomType
Tâche 1 : Compléter l’énumération
RoomType
dans le fichiersrc/main/java/RoomType.java
qui respecte le diagramme ci-dessous.
/**
* An enum to model a type of room in a hotel.
*
* A type of room has a maximum occupancy (the largest number of people who can legally
* sleep in a room of this type) and a price per night.
*/
public enum RoomType {
/**
* A type of room corresponding to a single room with a price of 60 and
* a maximum occupancy of 1.
*/
SINGLE(60,1),
/**
* A type of room corresponding to a double room with a price of 90 and
* a maximum occupancy of 2.
*/
DOUBLE(90,2),
/**
* A type of room corresponding to a double-double room with a price of 150
* and a maximum occupancy of 4.
*/
DOUBLE_DOUBLE(150, 4),
/**
* A type of room corresponding to a suite room with a price of 300 and
* a maximum occupancy of 4.
*/
SUIT(300, 4);
private final int price;
private final int maximumOccupancy;
/**
* create a type of room with the specified price and maximum occupancy
* @param price the price of the type of room
* @param maximumOccupancy the maximum occupancy of this type of room
*/
RoomType(int price, int maximumOccupancy) {
this.price = price;
this.maximumOccupancy = maximumOccupancy;
}/**
* return the price of this type of room
* @return the price of this type of room
*/
public int getPrice() {
return price;
}/**
* return the maximum occupancy of this type of room
* @return the maximum occupancy of this type of room
*/
public int getMaximumOccupancy() {
return maximumOccupancy;
} }
Classes de test RoomType
Tâche 2 : Compléter la classe de test
RoomTypeTest
dans le fichiersrc/test/java/RoomTypeTest.java
pour qu’elle contienne des tests pour vérifier le bon fonctionnement de la valeurDOUBLE_DOUBLE
deRoomType
.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class RoomTypeTest {
@Test
void testSinglePrice(){
assertThat(RoomType.SINGLE.getPrice()).isEqualTo(60);
}@Test
void testSingleMaximumOccupancy(){
assertThat(RoomType.SINGLE.getMaximumOccupancy()).isEqualTo(1);
}@Test
void testSingleToString(){
assertThat(RoomType.SINGLE.toString()).isEqualTo("SINGLE");
}@Test
void testDoubleDoublePrice(){
assertThat(RoomType.DOUBLE_DOUBLE.getPrice()).isEqualTo(150);
}@Test
void testDoubleDoubleMaximumOccupancy(){
assertThat(RoomType.DOUBLE_DOUBLE.getMaximumOccupancy()).isEqualTo(4);
}@Test
void testDoubleDoubleToString(){
assertThat(RoomType.DOUBLE_DOUBLE.toString()).isEqualTo("DOUBLE_DOUBLE");
} }
Chambre d’hôtel : classe Room
Tâche 3 : Complétez la classe
Room
dans le fichiersrc/main/java/Room.java
qui respecte le diagramme ci-dessous.
/**
* A class to model Room in a hotel.
*
* A room has a unique number and can be rented or not.
*/
public class Room {
private final int number;
private final RoomType roomType;
private boolean isRented = false;
/**
* create a room with given number and room type, initially a room is not rented
* @param number number of this room
*/
public Room(RoomType roomType, int number) {
this.roomType = roomType;
this.number = number;
}/**
* return this room number
* @return this room number
*/
public int getNumber() {
return number;
}/**
* return the type of this room
* @return the type of this room
*/
public RoomType getRoomType() {
return roomType;
}/**
* return the price of this room
* @return the price of this room
*/
public int getPrice() {
return roomType.getPrice();
}/**
* return the maximum occupancy of this room
* @return the maximum occupancy of this room
*/
int getMaximumOccupancy(){
return roomType.getMaximumOccupancy();
}/**
* return <code>true</code> if and only if this room is rented
* @return <code>true</code> if and only if this room is rented
*/
public boolean isRented() {
return isRented;
}/**
* rent this room if it was not rented, returning {@code true} if the room was not rented
*
* @return {@code true} if the room was not rented and {@code false} otherwise
*/
public boolean rent() {
if(isRented) return false;
true;
isRented = return true;
}/**
* free this room if it was rented, returning {@code true} if the room was rented
*
* @return {@code true} if the room was rented and {@code false} otherwise
*/
public boolean leave() {
if(!isRented) return false;
false;
isRented = return true;
}
/**
* return a string representation of the room containing the room in the following format :
* ZZ room, number XX, price YY$ where ZZ is the type of this room, XX is the number of this
* room and YY is the price of this room.
*
* @return a string representation of this room
*/
public String toString() {
return roomType + " room, number " + number + ", price " + getPrice() + "$";
}
/**
* Determines whether two rooms are equal. Two instances of Room are equal if their number
* are the same.
*
* @return {@code true} if the object to be compared is an instance of Room and has the
* same number; false otherwise.
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if(! (o instanceof Room))
return false;
Room room = (Room) o;return room.number == number;
} }
Classes de test RoomTest
Tâche 4 : Compléter la classe de test
RoomTest
dans le fichiersrc/test/java/RoomTest.java
pour qu’elle contienne des tests pour vérifier le bon fonctionnement des méthodesequals
,free
etrent
de la classeRoom
.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class RoomTest {
@Test
void testToStringContains(){
new Room(RoomType.SINGLE, 666);
Room room = assertThat(room.toString()).contains("666")
contains(RoomType.SINGLE.toString())
.contains(String.valueOf(RoomType.SINGLE.getPrice()))
.contains(String.valueOf(room.getPrice()));
.
}@Test
void testToStringStrictFormat(){
new Room(RoomType.SINGLE, 12);
Room room = assertThat(room.toString()).isEqualTo("%s room, number %d, price %d$",
SINGLE, room.getNumber(), RoomType.SINGLE.getPrice());
RoomType.
}@Test
void testEquals(){
new Room(RoomType.SINGLE, 1);
Room room1 = new Room(RoomType.SINGLE, 1);
Room room1Bis = new Room(RoomType.DOUBLE, 1);
Room room1Ter = new Room(RoomType.SINGLE, 11);
Room room11 = assertThat(room1).isEqualTo(room1)
isEqualTo(room1Bis)
.isEqualTo(room1Ter)
.isNotEqualTo(room11);
.
}
@Test
void testRentFreeRoom(){
new Room(RoomType.SINGLE, 666);
Room room = assertThat(room.isRented()).isFalse();
assertThat(room.rent()).isTrue();
assertThat(room.isRented()).isTrue();
}@Test
void testRentAlreadyRentedRoom(){
new Room(RoomType.SINGLE, 666);
Room room = rent();
room.assertThat(room.isRented()).isTrue();
assertThat(room.rent()).isFalse();
assertThat(room.isRented()).isTrue();
}@Test
void testFreeRentedRoom(){
new Room(RoomType.SINGLE, 666);
Room room = rent();
room.assertThat(room.isRented()).isTrue();
assertThat(room.leave()).isTrue();
assertThat(room.isRented()).isFalse();
}@Test
void testFreeAlreadyFreeRoom(){
new Room(RoomType.SINGLE, 666);
Room room = assertThat(room.isRented()).isFalse();
assertThat(room.leave()).isFalse();
assertThat(room.isRented()).isFalse();
} }
Hôtel : classe Hotel
Une chambre d’hôtel sera représentée par une instance de la classe Room
.
Tâche 5 : Complétez la classe
Hotel
dans le fichiersrc/main/java/Hotel.java
qui respecte le diagramme ci-dessous.
import java.util.List;
import java.util.ArrayList;
/**
* A class to model a hotel.
*
* A Hotel has a name and a list of rooms.
*/
public class Hotel {
private final String name;
private final List<Room> rooms = new ArrayList<>();
/**
* create a Hotel with the given name and no room
* @param name this hotel name
*/
public Hotel(String name) {
this.name = name;
}
/**
* return this hotel name
* @return this hotel name
*/
public String getName() {
return name;
}
/**
* return the number of rooms of this hotel
* @return the number of rooms of this hotel
*/
public int getNumberOfRooms() {
return rooms.size();
}
/**
* provide the room corresponding to the given number, return {@code null}
* if there is no room in the hotel with that number.
*
* @param number number of the room
* @return the room with given number if it exists and {@code null} otherwise
*/
public Room getRoom(int number) {
for(Room room : rooms){
if(room.getNumber() == number)
return room;
}return null;
}/**
* return the revenue of the hotel, i.e., the sum of the prices of the rented rooms
*
* @return the sum of the prices of the rented rooms
*/
public int getRevenue() {
int sum = 0;
for(Room room : rooms){
if(room.isRented())
getPrice();
sum += room.
}return sum;
}/**
* return the total occupancy of the hotel, i.e., the sum of the maximum occupancies
* of its rooms
*
* @return the sum of the maximum occupancies of the rooms
*/
public int getTotalOccupancy() {
int sum = 0;
for(Room room : rooms){
getMaximumOccupancy();
sum += room.
}return sum;
}/**
* add the specified room if there is not already a room in this hotel with the same
* room number
*
* @param room the room to be added
* @return {@code true} if the room was added as a result of the call (there was no
* room with the same number)
*/
public boolean addRoom(Room room) {
if (containsRoomWithSameNumberAs(room))
return false;
add(room);
rooms.return true;
}
private boolean containsRoomWithSameNumberAs(Room room) {
return getRoom(room.getNumber()) != null;
}
/**
* add each room in the specified list of rooms if there is not already a room in
* this hotel with the same
* room number
*
* @param rooms the rooms to be added
*/
public void addRooms(List<Room> rooms) {
for(Room room : rooms)
addRoom(room);
}
/**
* rent the room of the hotel corresponding to the specified room number if it was
* not rented, return {@code true} if such a room exists and was not rented
*
* @param number the number of the room to be rented
* @return {@code true} if the room corresponding to the room number exists and
* was not rented and {@code false} otherwise
*/
public boolean rentRoom(int number) {
getRoom(number);
Room room = if(room == null)
return false;
return room.rent();
}
/**
* leave the room of the hotel corresponding to the specified room number if it
* was rented, return {@code true} if such a room exists and was rented
*
* @param number the number of the room to be left
* @return {@code true} if the room corresponding to the room number exists and
* was rented and {@code false} otherwise
*/
public boolean leaveRoom(int number) {
getRoom(number);
Room room = if(room == null)
return false;
return room.leave();
}
/**
* return a non rented room corresponding to the specified type of room if such a
* room exists and {@code null} otherwise
*
* @param roomType the desired type of room
* @return a free room of the desired type or {@code null} if there is no such room
*/
public Room getFreeRoom(RoomType roomType) {
for(Room room : rooms)
if(room.getRoomType() == roomType && !room.isRented())
return room;
return null;
}/**
* print the hotel name followed by the string representations of the rooms
*/
public void print(){
System.out.println(getName());
for (Room room : rooms) {
System.out.println(room);
}
} }
Hôtel : classe HotelTest
(bonus)
Tâche 6 : Compléter la classe de test
HotelTest
dans le fichiersrc/test/java/HotelTest.java
pour qu’elle contienne des tests pour vérifier le bon fonctionnement des méthodesgetTotalOccupancy
,getNumberOfRooms
,getRevenue
,addRooms
etgetFreeRoom
de la classeHotel
.
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
public class HotelTest {
@Test
void testPrint_whenHotelHasNoRoom() {
new Hotel("Infogrames hotel");
Hotel hotel = new StandardOutputSandbox(hotel::print);
StandardOutputSandbox standardOutputSandbox = String expectedOutput = hotel.getName() + StandardOutputSandbox.NEW_LINE;
run();
standardOutputSandbox.assertThat(standardOutputSandbox.getProducedOutput()).isEqualTo(expectedOutput);
}@Test
void testPrint_whenHotelContainsRooms() {
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SINGLE, 1);
Room room1 = new Room(RoomType.SINGLE, 4);
Room room4 = addRooms(List.of(room1, room4));
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(2);
assertThat(hotel.getRoom(1)).isEqualTo(room1);
assertThat(hotel.getRoom(4)).isEqualTo(room4);
new StandardOutputSandbox(hotel::print);
StandardOutputSandbox standardOutputSandbox = String expectedOutput = hotel.getName() + StandardOutputSandbox.NEW_LINE
NEW_LINE
+ room1 + StandardOutputSandbox.NEW_LINE;
+ room4 + StandardOutputSandbox.run();
standardOutputSandbox.assertThat(standardOutputSandbox.getProducedOutput()).isEqualTo(expectedOutput);
}@Test
void testAddRoom(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SINGLE, 1);
Room room1 = new Room(RoomType.SINGLE, 1);
Room room1Bis = assertThat(hotel.addRoom(room1)).isTrue();
assertThat(hotel.getRoom(1)).isEqualTo(room1);
assertThat(hotel.addRoom(room1Bis)).isFalse();
new Room(RoomType.SINGLE, 4);
Room room4 = assertThat(hotel.addRoom(room4)).isTrue();
assertThat(hotel.getRoom(4)).isEqualTo(room4);
}
@Test
void testGetRoom(){
new Hotel("Davilex hotel");
Hotel hotel = int[] roomNumbers = {1, 2, 4};
new Room[roomNumbers.length];
Room[] rooms = for(int index = 0; index< rooms.length; index++){
new Room(RoomType.SINGLE, roomNumbers[index]);
rooms[index] = addRoom(rooms[index]);
hotel.
}assertThat(hotel.getNumberOfRooms()).isEqualTo(rooms.length);
for(int index = 0; index< rooms.length; index++){
assertThat(hotel.getRoom(roomNumbers[index])).isEqualTo(rooms[index]);
}
}
@Test
void testLeaveFreeRoom(){
new Hotel("Davilex hotel");
Hotel hotel = addRoom(new Room(RoomType.SINGLE, 1));
hotel.assertThat(hotel.getRoom(1).isRented()).isFalse();
assertThat(hotel.leaveRoom(1)).isFalse();
assertThat(hotel.getRoom(1).isRented()).isFalse();
}@Test
void testLeaveRentedRoom(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SINGLE, 4);
Room room4 = rent();
room4.addRoom(room4);
hotel.assertThat(hotel.getRoom(4).isRented()).isTrue();
assertThat(hotel.leaveRoom(4)).isTrue();
assertThat(hotel.getRoom(4).isRented()).isFalse();
}@Test
void testRentFreeRoom(){
new Hotel("Davilex hotel");
Hotel hotel = addRoom(new Room(RoomType.SINGLE, 1));
hotel.assertThat(hotel.getRoom(1).isRented()).isFalse();
assertThat(hotel.rentRoom(1)).isTrue();
assertThat(hotel.getRoom(1).isRented()).isTrue();
}@Test
void testRentRentedRoom(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SINGLE, 4);
Room room4 = rent();
room4.addRoom(room4);
hotel.assertThat(hotel.getRoom(4).isRented()).isTrue();
assertThat(hotel.rentRoom(4)).isFalse();
assertThat(hotel.getRoom(4).isRented()).isTrue();
}@Test
void testGetTotalOccupancy(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getTotalOccupancy()).isEqualTo(0);
addRoom(new Room(RoomType.SINGLE, 1));
hotel.assertThat(hotel.getTotalOccupancy()).isEqualTo(1);
addRoom(new Room(RoomType.DOUBLE, 4));
hotel.assertThat(hotel.getTotalOccupancy()).isEqualTo(3);
}@Test
void testGetNumberOfRooms(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getNumberOfRooms()).isEqualTo(0);
addRoom(new Room(RoomType.SINGLE, 1));
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(1);
addRoom(new Room(RoomType.DOUBLE, 4));
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(2);
}@Test
void testGetRevenue(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getNumberOfRooms()).isEqualTo(0);
assertThat(hotel.getRevenue()).isEqualTo(0);
new Room(RoomType.SINGLE, 1);
Room room1 = addRoom(room1);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(1);
assertThat(hotel.getRevenue()).isEqualTo(0);
rent();
room1.assertThat(hotel.getRevenue()).isEqualTo(room1.getPrice());
new Room(RoomType.DOUBLE, 4);
Room room4 = addRoom(room4);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(2);
assertThat(hotel.getRevenue()).isEqualTo(room1.getPrice());
rent();
room4.assertThat(hotel.getRevenue()).isEqualTo(room1.getPrice() + room4.getPrice());
}@Test
void testAddRooms_onEmptyHotel(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getNumberOfRooms()).isEqualTo(0);
new Room(RoomType.SINGLE, 1);
Room room1 = new Room(RoomType.SUITE, 2);
Room room2 = new Room(RoomType.DOUBLE, 4);
Room room4 = List<Room> rooms = new ArrayList<>();
add(room1);
rooms.add(room2);
rooms.add(room4);
rooms.addRooms(rooms);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(3);
}@Test
void testAddRooms_onHotelWithRooms(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getNumberOfRooms()).isEqualTo(0);
new Room(RoomType.SINGLE, 1);
Room room1 = new Room(RoomType.DOUBLE, 1);
Room room1Bis = new Room(RoomType.SUITE, 2);
Room room2 = new Room(RoomType.DOUBLE, 4);
Room room4 = new Room(RoomType.DOUBLE_DOUBLE, 4);
Room room4Bis = addRoom(room1);
hotel.addRoom(room4);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(2);
List<Room> rooms = new ArrayList<>();
add(room1Bis);
rooms.add(room2);
rooms.add(room4Bis);
rooms.addRooms(rooms);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(3);
assertThat(hotel.getRoom(1).getRoomType()).isEqualTo(RoomType.SINGLE);
assertThat(hotel.getRoom(4).getRoomType()).isEqualTo(RoomType.DOUBLE);
}@Test
void testGetFreeRoom_onEmptyHotel(){
new Hotel("Davilex hotel");
Hotel hotel = assertThat(hotel.getFreeRoom(RoomType.SINGLE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.SUITE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE_DOUBLE)).isNull();
}@Test
void testGetFreeRoom_onHotelWithOneFreeRoom(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SINGLE, 1);
Room room = addRoom(room);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(1);
assertThat(hotel.getFreeRoom(RoomType.SINGLE)).isEqualTo(room);
assertThat(hotel.getFreeRoom(RoomType.DOUBLE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.SUITE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE_DOUBLE)).isNull();
}@Test
void testGetFreeRoom_onHotelWithTwoFreeRooms(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SUITE, 1);
Room room1 = addRoom(room1);
hotel.new Room(RoomType.DOUBLE, 4);
Room room4 = addRoom(room4);
hotel.assertThat(hotel.getNumberOfRooms()).isEqualTo(2);
assertThat(hotel.getFreeRoom(RoomType.SUITE)).isEqualTo(room1);
assertThat(hotel.getFreeRoom(RoomType.DOUBLE)).isEqualTo(room4);
assertThat(hotel.getFreeRoom(RoomType.SINGLE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE_DOUBLE)).isNull();
}@Test
void testGetFreeRoom_onHotelWithTwoRentedRoomsAndOneFreeRoom(){
new Hotel("Davilex hotel");
Hotel hotel = new Room(RoomType.SUITE, 1);
Room room1 = rent();
room1.assertThat(room1.isRented()).isTrue();
addRoom(room1);
hotel.new Room(RoomType.DOUBLE_DOUBLE, 2);
Room room2 = addRoom(room2);
hotel.new Room(RoomType.DOUBLE, 4);
Room room4 = addRoom(room4);
hotel.rent();
room4.assertThat(room4.isRented()).isTrue();
assertThat(hotel.getNumberOfRooms()).isEqualTo(3);
assertThat(hotel.getFreeRoom(RoomType.SUITE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.SUITE)).isNull();
assertThat(hotel.getFreeRoom(RoomType.DOUBLE_DOUBLE)).isEqualTo(room2);
} }