@@ -131,11 +131,12 @@ public class Book | |||||
status = "AVAILABLE"; | status = "AVAILABLE"; | ||||
} | } | ||||
/** | |||||
/* | |||||
* Calculate the overdue fine | * Calculate the overdue fine | ||||
* @param currentDay Today's date | * @param currentDay Today's date | ||||
* @return Price of the fine | * @return Price of the fine | ||||
*/ | */ | ||||
/* | |||||
public double overdueFine(int[] currentDay) | public double overdueFine(int[] currentDay) | ||||
{ | { | ||||
double fine; | double fine; | ||||
@@ -182,4 +183,5 @@ public class Book | |||||
return fine; | return fine; | ||||
} | } | ||||
} | } | ||||
*/ | |||||
} | } |
@@ -126,7 +126,7 @@ public class UserFactory | |||||
* @param name Name of the User to be found | * @param name Name of the User to be found | ||||
* @return User with the given name | * @return User with the given name | ||||
*/ | */ | ||||
public User getUser(String name) | |||||
User getUser(String name) | |||||
{ | { | ||||
for (User temp : users) { | for (User temp : users) { | ||||
if (temp.getName().equals(name)) { | if (temp.getName().equals(name)) { | ||||
@@ -68,11 +68,16 @@ public class BookFactoryTest extends TestCase | |||||
*/ | */ | ||||
private void outputAndInputTest() | private void outputAndInputTest() | ||||
{ | { | ||||
String filename = "someTestFile.json"; | |||||
String filename = "."; | |||||
bookFactory.setBookFileName(filename); | |||||
bookFactory.toJsonFile(); | |||||
assertFalse("File does not exist", (new File(filename)).exists() && !(new File(filename).isDirectory())); | |||||
filename = "someTestFile.json"; | |||||
bookFactory.setBookFileName(filename); | bookFactory.setBookFileName(filename); | ||||
bookFactory.toJsonFile(); | bookFactory.toJsonFile(); | ||||
BookFactory newBookFactory = new BookFactory(filename); | BookFactory newBookFactory = new BookFactory(filename); | ||||
assertTrue("newBookFactory : Book title is \"Whole new Book\"", newBookFactory.getBook(0).getTitle().equals("Whole new Book")); | assertTrue("newBookFactory : Book title is \"Whole new Book\"", newBookFactory.getBook(0).getTitle().equals("Whole new Book")); | ||||
assertEquals("newBookFactory : Book id is 0", newBookFactory.getBook("Whole new Book").getId(), 0); | |||||
assertTrue("newBookFactory : Book status is \"AVAILABLE\"", newBookFactory.getBook(0).getStatus().equals("AVAILABLE")); | assertTrue("newBookFactory : Book status is \"AVAILABLE\"", newBookFactory.getBook(0).getStatus().equals("AVAILABLE")); | ||||
} | } | ||||
@@ -93,6 +98,28 @@ public class BookFactoryTest extends TestCase | |||||
{ | { | ||||
assert true; | assert true; | ||||
} | } | ||||
try | |||||
{ | |||||
Book someBook = new Book(20); | |||||
Book anotherBook = someBook; | |||||
anotherBookFactory.update(someBook, anotherBook); | |||||
assert false; | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
assert true; | |||||
} | |||||
try | |||||
{ | |||||
anotherBookFactory.getBook(12); | |||||
assert false; | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
assert true; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@@ -108,7 +108,8 @@ public class BookTest extends TestCase | |||||
Book testBook2 = new Book("Test Title", 20, "RESERVED"); | Book testBook2 = new Book("Test Title", 20, "RESERVED"); | ||||
Book testBook3 = new Book(15, "Test Title 2", "RENTED", (new int[]{2019, 3, 23})); | Book testBook3 = new Book(15, "Test Title 2", "RENTED", (new int[]{2019, 3, 23})); | ||||
assertTrue("Book1 id is 10", testBook1.getId() == 10); | |||||
assertEquals("Book name is \"UNDEFINED\"", testBook1.getTitle(), "UNDEFINED"); | |||||
assertEquals("Book1 id is 10", testBook1.getId(), 10); | |||||
assertEquals("Book1 status is \"NOT AVAILABLE\"", testBook1.getStatus(), "NOT AVAILABLE"); | assertEquals("Book1 status is \"NOT AVAILABLE\"", testBook1.getStatus(), "NOT AVAILABLE"); | ||||
assertEquals("Book2 title is \"Test Title\"", testBook2.getTitle(), "Test Title"); | assertEquals("Book2 title is \"Test Title\"", testBook2.getTitle(), "Test Title"); | ||||
@@ -36,7 +36,6 @@ public class SettingsTest extends TestCase | |||||
public void testApp() | public void testApp() | ||||
{ | { | ||||
constructorTest(); | constructorTest(); | ||||
setTitleTest(); | |||||
setUsersFilenameTest(); | setUsersFilenameTest(); | ||||
setBooksFilenameTest(); | setBooksFilenameTest(); | ||||
} | } | ||||
@@ -46,20 +45,32 @@ public class SettingsTest extends TestCase | |||||
*/ | */ | ||||
private void constructorTest() | private void constructorTest() | ||||
{ | { | ||||
settings = new Settings("noSuchFile.txt"); | |||||
settings = new Settings("."); | |||||
settings.setTitle("Failed to save"); | |||||
File file = new File("."); | |||||
if (file.exists() && !file.isDirectory()) | |||||
{ | |||||
assert false; | |||||
} | |||||
else | |||||
{ | |||||
assert true; | |||||
} | |||||
String filename = "noSuchFile.txt"; | |||||
file = new File(filename); | |||||
if (file.exists()) | |||||
{ | |||||
file.delete(); | |||||
} | |||||
settings = new Settings(filename); | |||||
assertEquals("Default size is 3", settings.size(), 3); | |||||
assertEquals("Default title", settings.get(settings.getKey(0)), "Welcome to the library"); | assertEquals("Default title", settings.get(settings.getKey(0)), "Welcome to the library"); | ||||
assertEquals("Default users filename", settings.get(settings.getKey(1)), "users.json"); | assertEquals("Default users filename", settings.get(settings.getKey(1)), "users.json"); | ||||
assertEquals("Default books filename", settings.get(settings.getKey(2)), "books.json"); | assertEquals("Default books filename", settings.get(settings.getKey(2)), "books.json"); | ||||
} | |||||
/** | |||||
* Test the setTitle function | |||||
*/ | |||||
private void setTitleTest() | |||||
{ | |||||
String newTitle = "New Title"; | |||||
settings.setTitle(newTitle); | |||||
assertEquals("Set settings title to \"New Title\"", settings.get(settings.getKey(0)), newTitle); | |||||
settings.setTitle("noSuchFile"); | |||||
assertTrue("noSuchFile.txt is created", file.exists()); | |||||
settings = new Settings(filename); | |||||
assertEquals("Default title", settings.get(settings.getKey(0)), "noSuchFile"); | |||||
} | } | ||||
/** | /** | ||||
@@ -49,6 +49,7 @@ public class UserFactoryTest extends TestCase | |||||
newUserTest(); | newUserTest(); | ||||
fileIOTest(); | fileIOTest(); | ||||
exceptionTest(); | exceptionTest(); | ||||
updateTest(); | |||||
} | } | ||||
/** | /** | ||||
@@ -72,12 +73,24 @@ public class UserFactoryTest extends TestCase | |||||
assertEquals("Limit of User2 in userFactory is 3", userFactory.getUser(1).getLimit(), 3); | assertEquals("Limit of User2 in userFactory is 3", userFactory.getUser(1).getLimit(), 3); | ||||
} | } | ||||
private void updateTest() | |||||
{ | |||||
User user1 = userFactory.getUser(0); | |||||
user1.borrowNewBook(0); | |||||
user1.borrowNewBook(1); | |||||
userFactory.update(userFactory.getUser(0), user1); | |||||
} | |||||
/** | /** | ||||
* Test the class in writing to and reading from files | * Test the class in writing to and reading from files | ||||
*/ | */ | ||||
private void fileIOTest() | private void fileIOTest() | ||||
{ | { | ||||
String filename = "someTestFile.json"; | |||||
String filename = "."; | |||||
userFactory.setUserFileName(filename); | |||||
userFactory.toJsonFile(); | |||||
assertFalse("File does not exist", (new File(filename)).exists() && !(new File(filename).isDirectory())); | |||||
filename = "someTestFile.json"; | |||||
userFactory.setUserFileName(filename); | userFactory.setUserFileName(filename); | ||||
userFactory.toJsonFile(); | userFactory.toJsonFile(); | ||||
UserFactory newUserFactory = new UserFactory(filename); | UserFactory newUserFactory = new UserFactory(filename); | ||||
@@ -104,6 +117,16 @@ public class UserFactoryTest extends TestCase | |||||
{ | { | ||||
assert true; | assert true; | ||||
} | } | ||||
try | |||||
{ | |||||
anotherUserFactory.getUser("alk;jcnalk"); | |||||
assert false; | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
assert true; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@@ -100,6 +100,7 @@ public class UserTest extends TestCase | |||||
int[] bookIds = user1.bookStatus(); | int[] bookIds = user1.bookStatus(); | ||||
assertEquals("User1 is now borrowing Book3", bookIds[0], 3); | assertEquals("User1 is now borrowing Book3", bookIds[0], 3); | ||||
assertFalse("User1 reached the limit", user1.status()); | assertFalse("User1 reached the limit", user1.status()); | ||||
assertFalse("User1 fail to borrow another book", user1.borrowNewBook(5)); | |||||
assertFalse("User1 failed to return Book2 that was not borrowed", user1.returnBook(2)); | assertFalse("User1 failed to return Book2 that was not borrowed", user1.returnBook(2)); | ||||
assertTrue("User1 return Book3", user1.returnBook(3)); | assertTrue("User1 return Book3", user1.returnBook(3)); | ||||
assertTrue("User1 can now borrow book", user1.status()); | assertTrue("User1 can now borrow book", user1.status()); | ||||