Skip to content

Commit

Permalink
Implemented the returning the item based on user checked out history.
Browse files Browse the repository at this point in the history
  • Loading branch information
h4rikris committed Jun 22, 2015
1 parent c4f5984 commit f887222
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/com/tw/pathashala/models/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String checkOut(String bookName) {
}

public String checkedOutItems() {
return displayItems(checkedOutRentableItems);
return displayItems(userHistory.getItemList());
}

private String displayItems(ArrayList<RentableItem> listOfRentableItems) {
Expand All @@ -56,7 +56,7 @@ private String displayItems(ArrayList<RentableItem> listOfRentableItems) {
}

public String returnItem(String bookName) {
ArrayList<RentableItem> rentableItems = searchAgent.search(checkedOutRentableItems, bookName);
ArrayList<RentableItem> rentableItems = searchAgent.search(userHistory.getItemList(), bookName);
for (RentableItem book : rentableItems) {
RentableItem returnedRentableItem = book.returnItem();
checkedOutRentableItems.remove(book);
Expand Down
5 changes: 5 additions & 0 deletions src/com/tw/pathashala/models/UserHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ private User getCurrentLoggedInUser() {
return authentication.getCurrentLoggedInUser();
}

public ArrayList<RentableItem> getItemList() {
User currentLoggedInUser = getCurrentLoggedInUser();
return userCheckOutHistory.get(currentLoggedInUser);
}

@Override
public String toString() {
StringBuilder output = new StringBuilder();
Expand Down
27 changes: 25 additions & 2 deletions tests/com/tw/pathashala/models/LibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class LibraryTest {

Expand Down Expand Up @@ -71,8 +72,7 @@ public void testForNotToDisplayBookDetailsHavingFutureYearOfPublication() {

@Test
public void testForDisplayCheckedOutBookDetails() {
checkedOutRentableItems.clear();
checkedOutRentableItems.add(new CheckedOutBook("Java", "Hari", 2015));
userHistory.addItem(new CheckedOutBook("Java", "Hari", 2015));
Library library = new Library(availableRentableItems, checkedOutRentableItems, search, userHistory);

String booksDetails = library.checkedOutItems();
Expand Down Expand Up @@ -163,11 +163,34 @@ public void shouldAddCheckOutItemToUserHistory() {
@Test
public void shouldRemoveReturnedItemFromUserHistory() {
UserHistory userHistory = Mockito.mock(UserHistory.class);
when(userHistory.getItemList()).thenReturn(checkedOutRentableItems);
Library library = new Library(availableRentableItems, checkedOutRentableItems, search, userHistory);

library.returnItem("Oriented");
RentableItem item = new CheckedOutBook("Oriented", "SSS", 2014);

verify(userHistory).removeItem(item);
}

@Test
public void shouldGetReturnItemsFromUserHistory() {
UserHistory userHistory = Mockito.mock(UserHistory.class);
Library library = new Library(availableRentableItems, checkedOutRentableItems, search, userHistory);

library.checkedOutItems();

verify(userHistory).getItemList();
}

@Test
public void shouldGetCheckedItemsFromHistoryWhileReturningItem() {
UserHistory userHistory = Mockito.mock(UserHistory.class);
when(userHistory.getItemList()).thenReturn(checkedOutRentableItems);
Library library = new Library(availableRentableItems, checkedOutRentableItems, search, userHistory);

library.returnItem("Oriented");
RentableItem item = new CheckedOutBook("Oriented", "SSS", 2014);

verify(userHistory).getItemList();
}
}

0 comments on commit f887222

Please sign in to comment.