Skip to content
Snippets Groups Projects
Commit 391ea6ea authored by BINTI TENGKU MASRULHISHAM Tengku Maryam Naqeesha's avatar BINTI TENGKU MASRULHISHAM Tengku Maryam Naqeesha
Browse files

Clean code before crossed test

parent 657b3547
No related branches found
No related tags found
1 merge request!17Clean code before crossed test
Showing
with 64 additions and 46 deletions
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
...@@ -26,7 +26,8 @@ public class Film extends Item { ...@@ -26,7 +26,8 @@ public class Film extends Item {
@Override @Override
public String toString() { public String toString() {
return "Film Title: " + title + "\nKind: " + kind + "\nDirector: " + director + return "Film Title: " + getTitle() + "\nKind: " + getKind() + "\nDirector: " + getDirector() +
"\nScenarist: " + scenarist + "\nDuration: " + duration + " minutes"; "\nScenarist: " + getScenarist() + "\nDuration: " + getDuration() + " minutes";
} }
} }
...@@ -34,10 +34,13 @@ public abstract class Item { ...@@ -34,10 +34,13 @@ public abstract class Item {
public boolean compareTitle(String title2) { public boolean compareTitle(String title2) {
if (this.title == null || title2 == null) return false; if (this.title == null || title2 == null) return false;
String formattedTitle = this.title.trim().toUpperCase(); return normalize(this.title).equals(normalize(title2));
String formattedTitle2 = title2.trim().toUpperCase();
return formattedTitle.equals(formattedTitle2);
} }
private String normalize(String s) {
return s.trim().replaceAll("\\s+", "").toLowerCase();
}
public void addOrUpdateReview(String login, float mark, String comment) { public void addOrUpdateReview(String login, float mark, String comment) {
for (Review r : reviews) { for (Review r : reviews) {
if (r.getReviewerLogin().equalsIgnoreCase(login)) { if (r.getReviewerLogin().equalsIgnoreCase(login)) {
......
...@@ -34,6 +34,14 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -34,6 +34,14 @@ public class SocialNetwork implements ISocialNetwork {
nbfilms = 0; nbfilms = 0;
} }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/** /**
* Returns number of registered members. * Returns number of registered members.
* *
...@@ -44,9 +52,6 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -44,9 +52,6 @@ public class SocialNetwork implements ISocialNetwork {
return nbmembers; return nbmembers;
} }
/** /**
* Returns number of books. * Returns number of books.
* *
...@@ -75,7 +80,7 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -75,7 +80,7 @@ public class SocialNetwork implements ISocialNetwork {
/** /**
* Validates the format of a user's login and password (not empty or too short) * Validates the format of a user's login and password (not empty or too short)
* *
* @param login the login to be validated * @param login the login to be validated
* @param password the password to be validated * @param password the password to be validated
* @return true if login and password are valid * @return true if login and password are valid
* @throws BadEntryException if either login or password is invalid * @throws BadEntryException if either login or password is invalid
...@@ -94,7 +99,7 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -94,7 +99,7 @@ public class SocialNetwork implements ISocialNetwork {
/** /**
* Authenticates a user's login and password * Authenticates a user's login and password
* *
* @param login the login of the user * @param login the login of the user
* @param password the password of the user * @param password the password of the user
* @return true if password corresponds to registered login, false if wrong password * @return true if password corresponds to registered login, false if wrong password
*/ */
...@@ -130,10 +135,10 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -130,10 +135,10 @@ public class SocialNetwork implements ISocialNetwork {
/** /**
* Adds a new member to the social network. * Adds a new member to the social network.
* *
* @param login the new member's login * @param login the new member's login
* @param password the new member's password * @param password the new member's password
* @param profile a free String describing the new member's profile * @param profile a free String describing the new member's profile
* @throws BadEntryException if parameter is null or too short * @throws BadEntryException if parameter is null or too short
* @throws MemberAlreadyExistsException if login already exists * @throws MemberAlreadyExistsException if login already exists
*/ */
@Override @Override
...@@ -150,13 +155,16 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -150,13 +155,16 @@ public class SocialNetwork implements ISocialNetwork {
nbmembers++; nbmembers++;
} }
@Override @Override
public void addItemFilm(String login, String password, String title, String kind, String director, String scriptwriter, int duration) public void addItemFilm(String login, String password, String title, String kind, String director, String scriptwriter, int duration)
throws BadEntryException, NotMemberException, ItemFilmAlreadyExistsException { throws BadEntryException, NotMemberException, ItemFilmAlreadyExistsException {
validateUser(login, password); validateUser(login, password);
if (login == null || login.trim().isEmpty())
throw new BadEntryException("Error: Login is null or empty");
if (password == null || password.trim().length() < 4)
throw new BadEntryException("Error: Password is null or too short");
if (title == null || title.trim().isEmpty()) if (title == null || title.trim().isEmpty())
throw new BadEntryException("Error: Title is null or empty"); throw new BadEntryException("Error: Title is null or empty");
if (kind == null || kind.trim().isEmpty()) if (kind == null || kind.trim().isEmpty())
...@@ -180,6 +188,7 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -180,6 +188,7 @@ public class SocialNetwork implements ISocialNetwork {
listFilm.add(film); listFilm.add(film);
nbfilms++; nbfilms++;
} }
/** /**
* Returns number of films. * Returns number of films.
* *
...@@ -211,23 +220,27 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -211,23 +220,27 @@ public class SocialNetwork implements ISocialNetwork {
throw new NotItemException("Error: Film not found"); throw new NotItemException("Error: Film not found");
} }
/** /**
* Adds a new book to the social network. * Adds a new book to the social network.
* *
* @param login login of the member adding the book * @param login login of the member adding the book
* @param password password of the member adding the book * @param password password of the member adding the book
* @param title the new book's title * @param title the new book's title
* @param kind the new book's kind * @param kind the new book's kind
* @param author the new book's author * @param author the new book's author
* @param nbPages number of pages of the new book's * @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 BadEntryException if any input is invalid (empty or number pages equal or less than 0)
* @throws NotMemberException if login or password is wrong * @throws NotMemberException if login or password is wrong
* @throws ItemBookAlreadyExistsException if book already exists in system * @throws ItemBookAlreadyExistsException if book already exists in system
*/ */
@Override @Override
public void addItemBook(String login, String password, String title, String kind, String author, int nbPages) throws BadEntryException, NotMemberException, ItemBookAlreadyExistsException { public void addItemBook(String login, String password, String title, String kind, String author, int nbPages) throws BadEntryException, NotMemberException, ItemBookAlreadyExistsException {
validateUser(login, password); // Validación de entrada
if (login == null || login.trim().length() < 1)
throw new BadEntryException("Error: login is null or empty");
if (password == null || password.trim().length() < 4)
throw new BadEntryException("Error: password is null or too short");
if (title == null || title.trim().isEmpty()) if (title == null || title.trim().isEmpty())
throw new BadEntryException("Error: Title is null or empty"); throw new BadEntryException("Error: Title is null or empty");
if (kind == null || kind.trim().isEmpty()) if (kind == null || kind.trim().isEmpty())
...@@ -235,32 +248,42 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -235,32 +248,42 @@ public class SocialNetwork implements ISocialNetwork {
if (author == null || author.trim().isEmpty()) if (author == null || author.trim().isEmpty())
throw new BadEntryException("Error: Author is null or empty"); throw new BadEntryException("Error: Author is null or empty");
if (nbPages <= 0) if (nbPages <= 0)
throw new BadEntryException("Error: Page number is null or less"); throw new BadEntryException("Error: Page number must be positive");
if (!authenticateUser(login, password))
// Normalización
String trimmedLogin = login.trim();
String trimmedPassword = password.trim();
String trimmedTitle = title.trim();
String trimmedAuthor = author.trim();
// Verificación de miembro
if (!authenticateUser(trimmedLogin, trimmedPassword))
throw new NotMemberException("Error: Invalid login or password"); throw new NotMemberException("Error: Invalid login or password");
// Verificación de duplicado de libro (título + autor)
for (Book b : listBook) { for (Book b : listBook) {
if (b.compareTitle(title)) { if (b.compareTitle(trimmedTitle) &&
b.getAuthor().trim().equalsIgnoreCase(trimmedAuthor)) {
throw new ItemBookAlreadyExistsException(); throw new ItemBookAlreadyExistsException();
} }
} }
book = new Book(title.trim(), kind.trim(), author.trim(), nbPages);
listBook.add(book); // Agregar libro nuevo
Book newBook = new Book(trimmedTitle, kind.trim(), trimmedAuthor, nbPages);
listBook.add(newBook);
nbbooks++; nbbooks++;
} }
/** /**
* * @param login login of the member adding the review
* @param login login of the member adding the review
* @param password password of the member adding the review * @param password password of the member adding the review
* @param title the reviewed book's title * @param title the reviewed book's title
* @param mark the mark given by the member for this book * @param mark the mark given by the member for this book
* @param comment the comment given by the member for this book * @param comment the comment given by the member for this book
* @return average mark of current book * @return average mark of current book
* @throws BadEntryException if any input is invalid (mark is out of range) * @throws BadEntryException if any input is invalid (mark is out of range)
* @throws NotMemberException if login or password is wrong * @throws NotMemberException if login or password is wrong
* @throws NotItemException if book doesn't exist * @throws NotItemException if book doesn't exist
*/ */
@Override @Override
public float reviewItemBook(String login, String password, String title, float mark, String comment) public float reviewItemBook(String login, String password, String title, float mark, String comment)
...@@ -294,7 +317,6 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -294,7 +317,6 @@ public class SocialNetwork implements ISocialNetwork {
} }
/** /**
*
* @param title title of searched item(s) * @param title title of searched item(s)
* @return the search result of book or film added * @return the search result of book or film added
* @throws BadEntryException if any input is invalid (title is empty or too short) * @throws BadEntryException if any input is invalid (title is empty or too short)
...@@ -342,12 +364,4 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -342,12 +364,4 @@ public class SocialNetwork implements ISocialNetwork {
} }
return ret.trim(); return ret.trim();
} }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment