From 404c84aa20499425a42d860a9820e4495587232d Mon Sep 17 00:00:00 2001 From: tmnaqeesha <tmnaqeesha@users.noreply.github.com> Date: Sun, 11 May 2025 11:41:34 +0200 Subject: [PATCH] fix: Clean code and improve comments --- src/opinion/SocialNetwork.java | 29 +----------- src/tests/AddItemBookTest.java | 83 +++++++++++++--------------------- 2 files changed, 34 insertions(+), 78 deletions(-) diff --git a/src/opinion/SocialNetwork.java b/src/opinion/SocialNetwork.java index 281bc46..c2a9f4f 100644 --- a/src/opinion/SocialNetwork.java +++ b/src/opinion/SocialNetwork.java @@ -125,12 +125,6 @@ public class SocialNetwork implements ISocialNetwork { if(title == null || title.length() <= 1 ){ throw new BadEntryException("title is null or empty"); } -// for(Book b : listBook) { -// if(title.toUpperCase().trim().equals(book.getTitle().toUpperCase().trim())) { -// result = true; -// } -// } -// return result; for (Book b : listBook) { if (b.compareTitle(title)) { return true; @@ -191,29 +185,11 @@ public class SocialNetwork implements ISocialNetwork { throw new ItemBookAlreadyExistsException(); } } - Book newBook = new Book(title.trim(), kind.trim(), author.trim(), nbPages); - listBook.add(newBook); + book = new Book(title.trim(), kind.trim(), author.trim(), nbPages); + listBook.add(book); nbbooks++; } -// public void addItemBook(String login, String password, String title, -// String kind, String author, int nbPages) throws BadEntryException, -// NotMemberException, ItemBookAlreadyExistsException { -// if (validateuser(login,password)){ -// if (!memberRegister(login)) { -// throw new NotMemberException("This user does not exist"); -// -// } -// if (validatebook(title)) { -// throw new ItemBookAlreadyExistsException(); -// } -// else{ -// book = new Book(title, kind, author, nbPages); -// listBook.add(book); -// } -// } -// } - @Override public void addItemFilm(String login, String password, String title, String kind, String director, String scriptwriter, int duration) throws BadEntryException, NotMemberException, ItemFilmAlreadyExistsException { @@ -241,7 +217,6 @@ public class SocialNetwork implements ISocialNetwork { counter++; } return ret.trim(); - //return (listMember.toString()); } @Override diff --git a/src/tests/AddItemBookTest.java b/src/tests/AddItemBookTest.java index 9766796..cc3ac0d 100644 --- a/src/tests/AddItemBookTest.java +++ b/src/tests/AddItemBookTest.java @@ -17,25 +17,16 @@ public class AddItemBookTest { * Check that the method addItemBook throws BadEntryException or NotMemberException correctly. * If OK, the method just returns 0. If not OK, displays an error message and returns 1. * - * @param sn - * - the <i>ISocialNetwork</i> - * @param login - * - login of the member - * @param password - * - password of the member - * @param title - * - title of the book - * @param kind - * - kind of the book - * @param author - * - author of the book - * @param nbPages - * - number of pages of the book - * @param expectedException - * - the expected exception class - * @param testId - * - the test ID that will prefix any error message displayed by this method - * @return 0 if the test is OK, 1 if not + * @param sn the <i>ISocialNetwork</i> + * @param login the login of the member + * @param password the password of the member + * @param title the title of the book + * @param kind the kind of the book + * @param author the author of the book + * @param nbPages the page number of the book + * @param expectedException the expected exception class + * @param testId the test ID + * @return 0 if test is OK, 1 if not OK */ private static int checkAddItemBookException(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, Class<?> expectedException, String testId) { try { @@ -57,23 +48,15 @@ public class AddItemBookTest { * Check that the method addItemBook succeeds. * If OK, the method just returns 0. If not OK, displays an error message and returns 1. * - * @param sn - * - the <i>ISocialNetwork</i> - * @param login - * - login of the member - * @param password - * - password of the member - * @param title - * - title of the book - * @param kind - * - kind of the book - * @param author - * - author of the book - * @param nbPages - * - number of pages of the book - * @param testId - * - the test ID that will prefix any error message displayed by this method - * @return 0 if the test is OK, 1 if not + * @param sn the <i>ISocialNetwork</i> + * @param login the login of the member + * @param password the password of the member + * @param title the title of the book + * @param kind the kind of the book + * @param author the author of the book + * @param nbPages the number of pages of the book + * @param testId the test's ID + * @return 0 if test is OK, 1 if not OK */ private static int checkAddItemBookSuccess(ISocialNetwork sn, String login, String password, String title, String kind, String author, int nbPages, String testId) { try { @@ -88,10 +71,8 @@ public class AddItemBookTest { /** * Main test for addItemBook: - * <ul> - * <li>check if the method throws BadEntryException or NotMemberException in non-nominal conditions</li> - * <li>check if the method works correctly in nominal conditions</li> - * </ul> + * Checks if the method throws BadEntryException or NotMemberException in non-nominal conditions + * Checks if the method works correctly in nominal conditions * * @return a summary of the performed tests */ @@ -112,45 +93,45 @@ public class AddItemBookTest { // Non-nominal scenarios - // Scenario 1: Invalid login (empty) + // Test 2.1 : Invalid login (empty) nbTests++; nbErrors += checkAddItemBookException(sn, "", "passwordtest1", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.1"); - // Scenario 2: Invalid password(less than 8 characters) + // Test 2.2 : Invalid password(less than 8 characters) nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "abc", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.2"); - // Scenario 3: Invalid title (empty) + // Test 2.3 : Invalid title (empty) nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.3"); - // Scenario 4: Uninstantiated kind + // Test 2.4 : Uninstantiated kind nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", null, "Juan Perez", 300, BadEntryException.class, "2.4"); - // Scenario 5: Uninstantiated author + // Test 2.5 : Uninstantiated author nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", "Comedy", null, 300, BadEntryException.class, "2.5"); - // Scenario 6: Invalid number of pages (not strictly positive) + // Test 2.6 : Invalid number of pages (not strictly positive) nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "New Book", "Comedy", "Juan Perez", 0, BadEntryException.class, "2.6"); - // Scenario 7: Non-existent member + // Test 2.7 : Non-existent member nbTests++; nbErrors += checkAddItemBookException(sn, "nonExistentUser", "passwordtest1", "New Book", "Comedy", "Juan Perez", 300, NotMemberException.class, "2.7"); - // Scenario 8: Invalid password(null) + // Test 2.8 : Invalid password(null) nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "", "New Book", "Comedy", "Juan Perez", 300, BadEntryException.class, "2.8"); // Nominal scenarios - // Scenario 1: Valid book addition + // Test 2.9 : Valid book addition nbTests++; nbErrors += checkAddItemBookSuccess(sn, "usertest", "passwordtest1", "Effective Java", "Programming", "Joshua Bloch", 416, "2.9"); - // Scenario 2: Adding an existing book + // Test 2.10 : Add an existing book nbTests++; nbErrors += checkAddItemBookException(sn, "usertest", "passwordtest1", "Effective Java", "Programming", "Jose Pablo", 416, ItemBookAlreadyExistsException.class, "2.10"); @@ -159,7 +140,7 @@ public class AddItemBookTest { TestReport tr = new TestReport(nbTests, nbErrors); System.out.println("AddItemBookTest : " + tr); return tr; - } catch (NotTestReportException e) { // This shouldn't happen + } catch (NotTestReportException e) { System.out.println("Unexpected error in AddItemBookTest test code - Can't return valuable test results"); return null; } -- GitLab