- withFirstName() returns a new Person object with firstName as parameter and a null lastName.
- withLastName() returns a new Person object with lastName as parameter and a null firstName.
- becomeOlder() adds 5 to age value and turns a person in a vampire ir age reaches 138 value.
- askIfVampore() checks if a person is a vampire.
- killVampire() recives a person object and kills vampire if is a vampire.
Expand to reveal code
Person.java
@TestpublicvoidbecomeOlderTest(){ Faker fake =newFaker(); Person fakePerson =newPerson(fake.name().firstName(), fake.name().lastName(), fake.number().numberBetween(1,15));int originalAge = fakePerson.getAge();assertEquals(originalAge, fakePerson.getAge()); fakePerson.becomeOlder(); fakePerson.becomeOlder(); fakePerson.becomeOlder();int newAge = originalAge +(5*3);assertEquals(newAge, fakePerson.getAge());}@TestpublicvoidwithFirstNameTest(){ Faker faker =newFaker();String firstName= faker.name().firstName(); Person person =newPerson(); person = person.withFirstName(firstName);assertEquals(firstName, person.getFirstName());}@TestpublicvoidwithLastNameTest(){ Faker faker =newFaker();String lastName = faker.name().lastName(); Person person =newPerson(); person = person.witLastName(lastName);assertEquals(lastName, person.getLastName());}@TestpublicvoidaskIfVampireTest(){ Faker fake =newFaker(); Person fakeVampire =newPerson(fake.name().firstName(), fake.name().lastName(), fake.number().numberBetween(150,200)); fakeVampire.becomeOlder();assertEquals("This person is a vampire!", fakeVampire.askIfVampire());}@TestpublicvoidkillVampireTest(){ Faker fake =newFaker(); Person fakeVampire =newPerson(fake.name().firstName(), fake.name().lastName(), fake.number().numberBetween(150,200)); fakeVampire.becomeOlder(); fakeVampire.killVampire();assertEquals(0, fakeVampire.getAge());}
5.2 New methods for Student class
Student class has new methods:
- changesUniversity() recives a String and changes university of the student.
- buyBook() recives a Book object and add the book to the booksList
- authorWritesNewBook() recives parameters and returns a new book.
- addBooksToList() recives a Book object and add the book to the booksList.
- getOneBook() finds a book of the author that matches the bookTitle.
- getInTheCa() checks if there's seat or not to enter the car.
- startTheCar() if all the seats are occupied, the car start.
Expand to reveal code
car.java
publicvoidgetInTheCar(){if(passengers < seats){this.passengers++;}}publicStringstartTheCar(){if(passengers == seats){return"Let's drive!";}else{return"The car must be full to start driving";}}
5.4.1 Testing Car Class methods
Expand to reveal code
CarClassTest.java
@TestpublicvoidgetInTheCarTest(){// Creating a new person object and passing all parameters Car fakeCar =newCar(4,3,"Red");assertEquals(0, fakeCar.getPassengers()); fakeCar.getInTheCar();assertEquals(1, fakeCar.getPassengers()); fakeCar.getInTheCar();assertEquals(2, fakeCar.getPassengers()); fakeCar.getInTheCar();assertEquals(3, fakeCar.getPassengers()); fakeCar.getInTheCar();assertEquals(4, fakeCar.getPassengers()); fakeCar.getInTheCar();assertNotEquals(5, fakeCar.getPassengers());}@TestpublicvoidstartTheCarTest(){ Car fakeCar =newCar(4,3,"Red");for(int i =0; i < fakeCar.getSeats(); i++){ fakeCar.getInTheCar();}String isTheCarReady = fakeCar.startTheCar();assertEquals(isTheCarReady, fakeCar.startTheCar());}
5.5 New methods for Book class
Book class has new methods:
- readBook() counts how many pages of the book has been read.
- finishedBook() checks if a book is finished or not.
Expand to reveal code
Book.java
publicStringreadBook(int pages){this.readPages+= pages;return(readPages- pages)+" remaining to finish the book";}publicBooleanfinishedBook(){if(readPages >= pages){this.bookRead=true;}else{this.bookRead=false;}return bookRead;}