diff --git a/out/production/infrastructureLogicielleClient/opinion/Member.class b/out/production/infrastructureLogicielleClient/opinion/Member.class index 824d8034b28d2b0bec55038626bb054a9d641a83..634aa10b77e3bd83e05c4609611987517fba92d4 100644 Binary files a/out/production/infrastructureLogicielleClient/opinion/Member.class and b/out/production/infrastructureLogicielleClient/opinion/Member.class differ diff --git a/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class b/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class index 31b85354a7afa51719a2d6b71378b0a18e67e509..3b11728184a05dad4c2b5ade53362ab5c3de7a85 100644 Binary files a/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class and b/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class differ diff --git a/src/opinion/Book.java b/src/opinion/Book.java index e9e4890405d41c141e97599543e68ad3cd1f0ae9..cb0efbce85eca036046a055a50a6a3772b1cb9ef 100644 --- a/src/opinion/Book.java +++ b/src/opinion/Book.java @@ -1,7 +1,5 @@ package opinion; -import java.util.LinkedList; - /** * This class allows us to create books and to register new books */ @@ -12,14 +10,12 @@ public class Book { private int nbPages; private float mark; - - /** - * Initialization of private class variables by the constructor - * @param title - * @param kind - * @param author - * @param nbPages + * Initialization of private variables with constructor + * @param title The title of the book + * @param kind The type of the book + * @param author The author of the book + * @param nbPages The number of pages of the book */ public Book(String title, String kind, String author, int nbPages) { this.title = title; @@ -28,62 +24,51 @@ public class Book { this.nbPages = nbPages; } - /** - * String function to retrieve the book's title - * @return + * Return the title of the book + * @return book's title */ public String getTitle() { return this.title; } - /** - * String function to retrieve the book's kind - * @return the kind of the book + * Return the kind of the book + * @return book's kind */ public String getKind() { return this.kind; } - /** - * String function to retrieve the book's author - * @return the author + * Retrieve the author of the book + * @return book's author */ public String getAuthor() { return this.author; } - /** - * Integer function to retrieve the book's number of pages - * @return the number of pages + * Return the number of pages of the book + * @return book's page number */ public int getNbPages() { return this.nbPages; } - - /** - * Boolean function to know if the book is already present in the database - * @param title - * @return true or false + * Identify if the current book's title is similar as another book + * @param title2 other's book's title + * @return true if same, false if different */ - public boolean isCalled(String title) { - String titleUpperCase = this.title.toUpperCase().trim(); - if(titleUpperCase.equals(title.toUpperCase().trim())) { - return true; - } - return false; + public boolean compareTitle(String title2) { + String formattedTitle = this.title.toUpperCase().trim(); + String formattedTitle2 = title2.toUpperCase().trim(); + return formattedTitle.equals(formattedTitle2); } - - - /** - * String function to didsplay the values of a book - * @return the values + * String function to display the details of a book + * @return the book's details */ public String toString() { return "Book : "+getTitle()+"\nKind : "+getKind()+"\nAuthor : "+getAuthor()+"\nNumber of pages : "+getNbPages(); diff --git a/src/opinion/SocialNetwork.java b/src/opinion/SocialNetwork.java index bd05cc594e7602bab51e72a3d702c26769f11b5f..c54cf3c156cd385fc6984b6c41a1a248a67ccec2 100644 --- a/src/opinion/SocialNetwork.java +++ b/src/opinion/SocialNetwork.java @@ -32,9 +32,9 @@ public class SocialNetwork implements ISocialNetwork { */ public SocialNetwork() { listMember = new LinkedList<Member>(); - nbmembers=0; - nbbooks=0; - + listBook = new LinkedList<Book>(); + nbmembers = 0; + nbbooks = 0; } /** @@ -49,6 +49,7 @@ public class SocialNetwork implements ISocialNetwork { /** * Returns number of films. + * @return 0 */ @Override public int nbFilms() { @@ -58,54 +59,85 @@ public class SocialNetwork implements ISocialNetwork { /** * Returns number of books. + * @return number of books */ @Override public int nbBooks() { // TODO Auto-generated method stub - return 0; + return nbbooks; } /** * Checks whether a member already exists. - * @param login the login to check + * @param login the login to be checked * @return true if login exists, else return false */ public boolean memberRegister(String login) { - for(Member m : listMember) { - if(m.getLogin().toUpperCase().trim().equals(login.toUpperCase().trim())) { - - if (m.getPassword().toUpperCase().trim().equals(login.toUpperCase().trim())) { - return true; - } + for (Member m : listMember) { + if (m.getLogin().trim().equalsIgnoreCase(login.trim())) { + return true; } - }return false; + } + return false; } + /** + * Validates the format of a user's login and password (not empty or too short) + * @param login the login to be validated + * @param password the password to be validated + * @return true if login and password are valid + * @throws BadEntryException if either login or password is invalid + */ public boolean validateuser(String login, String password) throws BadEntryException{ boolean result = false; - if(login==null || login.length()<=1 ){ + if(login == null || login.length() <= 1){ throw new BadEntryException("login is null or empty"); } - if(password==null || password.length()<=1 ){ + if(password == null || password.length() <= 1 ){ throw new BadEntryException("password is null or empty"); } + return true; + } - - else - return true; + /** + * Authenticates a user's login and password + * @param login the login of the user + * @param password the password of the user + * @return true if password corresponds to registered login, false if wrong password + */ + private boolean authenticateUser(String login, String password) { + for (Member m : listMember) { + if (m.getLogin().trim().equalsIgnoreCase(login.trim()) && + m.getPassword().trim().equals(password.trim())) { + return true; + } + } + return false; } + /** + * Checks if a book title is valid (not empty or too short) and if the book already exists + * @param title the title of the book to be validated + * @return true if book does not exist yet in system, false if already exists + * @throws BadEntryException if title is empty or too short + */ public boolean validatebook(String title) throws BadEntryException { boolean result = false; - if(title==null || title.length()<=1 ){ + 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; +// 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; } } - return result; + return false; } /** @@ -118,48 +150,71 @@ public class SocialNetwork implements ISocialNetwork { */ @Override public void addMember(String login, String password, String profile) throws BadEntryException, MemberAlreadyExistsException{ - if (profile==null){ + if (profile == null){ throw new BadEntryException("profile is null or empty"); } + validateuser(login, password); if (memberRegister(login)) { throw new MemberAlreadyExistsException(); } - if(validateuser(login, password) ){ - member = new Member(login, password, profile); - listMember.add(member); - nbmembers++; - } - - if(validateuser(login, password) ){ - member = new Member(login, password, profile); - listMember.add(member); - nbmembers++; - } - - - - + member = new Member(login.trim(), password.trim(), profile.trim()); + listMember.add(member); + nbmembers++; } + /** + * Adds a new book to the social network. + * @param login login of the member adding the book + * @param password password of the member adding the book + * @param title the new book's title + * @param kind the new book's kind + * @param author the new book's author + * @param nbPages number of pages of the new book's + * @throws BadEntryException if any input is invalid (empty or number pages equal or less than 0) + * @throws NotMemberException if login or password is wrong + * @throws ItemBookAlreadyExistsException if book already exists in system + */ @Override - 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)) { + public void addItemBook(String login, String password, String title, String kind, String author, int nbPages) throws BadEntryException, NotMemberException, ItemBookAlreadyExistsException { + validateuser(login, password); + if (title == null || title.trim().isEmpty()) + throw new BadEntryException("Error: Invalid title."); + if (kind == null || kind.trim().isEmpty()) + throw new BadEntryException("Error: Invalid kind."); + if (author == null || author.trim().isEmpty()) + throw new BadEntryException("Error: Invalid author."); + if (nbPages <= 0) + throw new BadEntryException("Error: Invalid number of pages."); + if (!authenticateUser(login, password)) + throw new NotMemberException("Error: User doesn't exist"); + for (Book b : listBook) { + if (b.compareTitle(title)) { throw new ItemBookAlreadyExistsException(); } - else{ - book = new Book(title, kind, author, nbPages); - listBook.add(book); - } } + Book newBook = new Book(title.trim(), kind.trim(), author.trim(), nbPages); + listBook.add(newBook); + 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 { @@ -172,14 +227,22 @@ public class SocialNetwork implements ISocialNetwork { return 0; } + /** + * Displays a whole list of registered members + * @return list of registered members + */ public String toString(){ - String ret = ""; - for (Member m : listMember) { - ret += m.toString() + "\n"; + if (listMember.isEmpty()) { + return "There are currently no registered members :("; } - return ret; + String ret = "\nRegistered members:\n"; + int counter = 1; + for (Member m : listMember) { + ret += counter + ". " + m.toString() + "\n"; + counter++; + } + return ret.trim(); //return (listMember.toString()); - } @Override diff --git a/src/tests/AddItemBookTest.java b/src/tests/AddItemBookTest.java index 282be8f2f39e5c4b93173fa70c7ce054d1d73162..2b559e2da17214d6966d262b79306e7faa0b8f18 100644 --- a/src/tests/AddItemBookTest.java +++ b/src/tests/AddItemBookTest.java @@ -272,9 +272,9 @@ public class AddItemBookTest { // exception //populate 'sn' with 3 members - nbErrors += addMemberOKTest(sn,"Paul","paul","","1.0a"); - nbErrors += addMemberOKTest(sn,"Antoine","antoine","", "1.0b"); - nbErrors += addMemberOKTest(sn,"Alice", "alice","", "1.0c"); + nbErrors += addMemberOKTest(sn,"Paul","paul","Student","1.0a"); + nbErrors += addMemberOKTest(sn,"Antoine","antoine","Philosopher", "1.0b"); + nbErrors += addMemberOKTest(sn,"Alice", "alice","F1 driver", "1.0c"); nbTests++;