From f8872225520a232488273b762302088a5f2efe13 Mon Sep 17 00:00:00 2001 From: Hari krishna Date: Mon, 22 Jun 2015 18:27:52 +0530 Subject: [PATCH] Implemented the returning the item based on user checked out history. --- src/com/tw/pathashala/models/Library.java | 4 +-- src/com/tw/pathashala/models/UserHistory.java | 5 ++++ .../com/tw/pathashala/models/LibraryTest.java | 27 +++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/com/tw/pathashala/models/Library.java b/src/com/tw/pathashala/models/Library.java index a2291e0..f06f3fe 100644 --- a/src/com/tw/pathashala/models/Library.java +++ b/src/com/tw/pathashala/models/Library.java @@ -40,7 +40,7 @@ public String checkOut(String bookName) { } public String checkedOutItems() { - return displayItems(checkedOutRentableItems); + return displayItems(userHistory.getItemList()); } private String displayItems(ArrayList listOfRentableItems) { @@ -56,7 +56,7 @@ private String displayItems(ArrayList listOfRentableItems) { } public String returnItem(String bookName) { - ArrayList rentableItems = searchAgent.search(checkedOutRentableItems, bookName); + ArrayList rentableItems = searchAgent.search(userHistory.getItemList(), bookName); for (RentableItem book : rentableItems) { RentableItem returnedRentableItem = book.returnItem(); checkedOutRentableItems.remove(book); diff --git a/src/com/tw/pathashala/models/UserHistory.java b/src/com/tw/pathashala/models/UserHistory.java index 9dad4af..d7aa0c2 100644 --- a/src/com/tw/pathashala/models/UserHistory.java +++ b/src/com/tw/pathashala/models/UserHistory.java @@ -26,6 +26,11 @@ private User getCurrentLoggedInUser() { return authentication.getCurrentLoggedInUser(); } + public ArrayList getItemList() { + User currentLoggedInUser = getCurrentLoggedInUser(); + return userCheckOutHistory.get(currentLoggedInUser); + } + @Override public String toString() { StringBuilder output = new StringBuilder(); diff --git a/tests/com/tw/pathashala/models/LibraryTest.java b/tests/com/tw/pathashala/models/LibraryTest.java index 1dd4f19..0acf92b 100644 --- a/tests/com/tw/pathashala/models/LibraryTest.java +++ b/tests/com/tw/pathashala/models/LibraryTest.java @@ -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 { @@ -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(); @@ -163,6 +163,7 @@ 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"); @@ -170,4 +171,26 @@ public void shouldRemoveReturnedItemFromUserHistory() { 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(); + } } \ No newline at end of file