Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kata/collections/practice #48

Open
wants to merge 8 commits into
base: kata/collections/start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* A utility class to generate pet hotels with pets already booked
*/
public class APetHotel {

public static PetAdder with(int petCount) {
return new PetAdder(petCount);
}
Expand Down Expand Up @@ -38,5 +39,13 @@ private Breed someBreed() {
private String someName(int petCount) {
return PET_NAMES.get(random.nextInt(PET_NAMES.size())) + " " + petCount;
}

public PetHotel petsCheckIn() {
PetHotel petHotel= new PetHotel();
for (int i=0;i<petCount; i++)
petHotel.checkIn(Pet.dog().named("Fido-"+i));

return petHotel;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package serenitylabs.tutorials.vetclinic.collections.katas;

/**
* Created by kimahale on 9/20/2016.
*/
public class BookingResponse {

private final boolean confirmedStatus;

public BookingResponse(boolean confirmedStatus) {
this.confirmedStatus = confirmedStatus;
}

public boolean isConfirmed() {
return confirmedStatus;
}

public boolean isOnWaitingList() {
return !confirmedStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package serenitylabs.tutorials.vetclinic.collections.katas;

import serenitylabs.tutorials.vetclinic.Pet;

import java.util.*;

/**
* Created by kimahale on 9/20/2016.
*/
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could refactor this nicely using the Strategy pattern.

boolean confirmStatus= false;
if (pets.size() <DEFAULT_MAXIMUM_CAPACITY) {
pets.add(pet);
confirmStatus=true;
}
else
Copy link
Contributor

Choose a reason for hiding this comment

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

Always put curly brackets around statements.

waitingList.add(pet);
return new BookingResponse(confirmStatus);
}

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

public void checkOut(Pet pet) {
pets.remove(pet);
if (!waitingList.isEmpty()) {
checkIn(waitingList.poll());
}
}
}
1 change: 1 addition & 0 deletions src/test/java/serenitylabs/tutorials/vetclinic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your test code goes here.
Original file line number Diff line number Diff line change
@@ -1,54 +1,178 @@
package serenitylabs.tutorials.vetclinic.collections.katas;
Copy link
Contributor

Choose a reason for hiding this comment

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

Good clean tests.


import org.junit.Test;
import serenitylabs.tutorials.vetclinic.Pet;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class WhenBookingPetsIntoAPetHotel {


@Test
public void the_hotel_should_initially_have_no_pets_booked() {
// When
PetHotel petHotel = new PetHotel();
// Then
assertThat(petHotel.getPets(), hasSize(0));
}

@Test
public void should_be_able_to_check_a_pet_into_the_hotel() throws Exception {
// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
//When
petHotel.checkIn(fido);
// Then
assertThat(petHotel.getPets(), hasItems(fido));
}

@Test
public void should_be_able_to_check_in_several_pets() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");

//When
petHotel.checkIn(fido);
petHotel.checkIn(felix);
// Then
assertThat(petHotel.getPets(), hasItems(fido, felix));

}


@Test
public void should_not_be_able_to_check_in_the_same_pet_twice() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");

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

// When
petHotel.checkIn(fido);
// Then
assertThat(petHotel.getPets(), containsInAnyOrder(fido, felix));

}

@Test
public void should_be_able_to_retrieve_checked_in_pets_in_alphabetical_order() throws Exception {
// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
Pet felix= Pet.cat().named("Felix");
Pet tommy= Pet.dog().named("Tommy");


// When
petHotel.checkIn(fido);
petHotel.checkIn(felix);
petHotel.checkIn(tommy);
// Then
assertThat(petHotel.getPets(), contains(felix, fido, tommy));


}

@Test
public void should_be_able_to_obtain_a_booking_confirmation_when_we_check_in_a_pet() throws Exception {

// Given
PetHotel petHotel=new PetHotel();
Pet fido= Pet.dog().named("Fido");
// When
BookingResponse bookingResponse= petHotel.checkIn(fido);
// Then
assertThat(bookingResponse.isConfirmed(), equalTo(true));
}

@Test
public void should_not_be_able_to_check_in_pets_beyond_hotel_capacity() throws Exception {
// Given
PetHotel petHotel=APetHotel.with(20).petsCheckIn();
Pet fido= Pet.dog().named("Fido");

// When
petHotel.checkIn(fido);

// Then
assertThat(petHotel.getPets(), hasSize(20));

}

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

}

This file was deleted.