Skip to content

Commit

Permalink
Kata/Collections/practice completed all test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kiran1552 committed Sep 20, 2016
1 parent ebf5486 commit 358db26
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public BookingResponse(boolean confirmedStatus) {
public boolean isConfirmed() {
return confirmedStatus;
}

public boolean isOnWaitingList() {
return !confirmedStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,31 @@ public class PetHotel {

private Collection<Pet> pets= new TreeSet<>(Comparator.comparing(Pet::getName));
public static int DEFAULT_MAXIMUM_CAPACITY = 20;
private Queue<Pet> waitingList = new LinkedList<>();

public List<Pet> getPets() {
return new ArrayList(pets);
}

public BookingResponse checkIn(Pet pet) {
boolean confirmStatus= false;
if (pets.size() <DEFAULT_MAXIMUM_CAPACITY) {
pets.add(pet);
confirmStatus=true;
}
else
waitingList.add(pet);
return new BookingResponse(confirmStatus);
}

public List<Pet> getWaitingList() {
return new ArrayList<>(waitingList);
}

if (pets.size() <DEFAULT_MAXIMUM_CAPACITY)
pets.add(pet);
return new BookingResponse(true);
public void checkOut(Pet pet) {
pets.remove(pet);
if (!waitingList.isEmpty()) {
checkIn(waitingList.poll());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void should_not_be_able_to_check_in_the_same_pet_twice() throws Exception
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");

petHotel.checkIn(fido);
petHotel.checkIn(felix);

Expand Down Expand Up @@ -110,20 +110,69 @@ public void should_not_be_able_to_check_in_pets_beyond_hotel_capacity() throws E

@Test
public void should_notify_owner_that_the_hotel_is_full() throws Exception {

// Given
PetHotel petHotel=APetHotel.with(20).petsCheckIn();
Pet fido= Pet.dog().named("Fido");
// When
BookingResponse bookingResponse= petHotel.checkIn(fido);
// Then
assertThat(bookingResponse.isConfirmed(), is(false));
assertThat(bookingResponse.isOnWaitingList(), is(true));


}


@Test
public void should_place_pets_on_a_waiting_list_when_the_hotel_is_full() throws Exception {

// Given
PetHotel petHotel=APetHotel.with(20).petsCheckIn();
Pet fido= Pet.dog().named("Fido");
// When
petHotel.checkIn(fido);
// Then
assertThat(petHotel.getWaitingList(), hasItem(fido));

}

@Test
public void pets_on_the_waiting_list_should_be_added_to_the_hotel_when_a_place_is_freed() throws Exception {

// Given
PetHotel petHotel=APetHotel.with(19).petsCheckIn();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");
// Pet roger= Pet.rabbit().named("Roger");

petHotel.checkIn(fido);
petHotel.checkIn(felix);
// petHotel.checkIn(roger);
petHotel.checkOut(fido);
// Then
assertThat(petHotel.getPets(), hasItem(felix));

}


@Test
public void pets_on_the_waiting_list_should_be_admitted_on_a_first_come_first_served_basis() throws Exception {

PetHotel hotel = APetHotel.with(19).petsCheckIn();
Pet felix = Pet.cat().named("FELIX");
Pet fido = Pet.dog().named("FIDO");
Pet roger = Pet.rabbit().named("ROGER");

hotel.checkIn(felix);
hotel.checkIn(fido);
hotel.checkIn(roger);

//WHEN
hotel.checkOut(felix);

//Then
assertThat(hotel.getPets(), hasItem(fido));
}

}

1 comment on commit 358db26

@wakaleo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff Kiran!

Please sign in to comment.