diff --git a/out/production/infrastructureLogicielleClient/opinion/Book.class b/out/production/infrastructureLogicielleClient/opinion/Book.class index dbdc2cd776a1d7fb9146f3653746c2aa23291368..d090638588fb800270805a9a52c290a1398613ea 100644 Binary files a/out/production/infrastructureLogicielleClient/opinion/Book.class and b/out/production/infrastructureLogicielleClient/opinion/Book.class differ diff --git a/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class b/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class index f72caf53502252cc68a317e97eb5f24e42b6d421..c1aea344efaad2d15b7b062784fd51cef66130cb 100644 Binary files a/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class and b/out/production/infrastructureLogicielleClient/opinion/SocialNetwork.class differ diff --git a/out/production/infrastructureLogicielleClient/tests/AddMemberTest.class b/out/production/infrastructureLogicielleClient/tests/AddMemberTest.class index 428db0695063dcca3c3a0991857b6b6341ad027e..b1d6375ffa627f698c13550ef3e0df492265dab6 100644 Binary files a/out/production/infrastructureLogicielleClient/tests/AddMemberTest.class and b/out/production/infrastructureLogicielleClient/tests/AddMemberTest.class differ diff --git a/src/opinion/Book.java b/src/opinion/Book.java index bc3c4a6902bae07ca41408087c16e64934753158..7f6ebe0881bf1faae2ea54b85bc147b23c7e6bbd 100644 --- a/src/opinion/Book.java +++ b/src/opinion/Book.java @@ -10,15 +10,15 @@ import java.util.LinkedList; * @version 1.0 * @since 1.0 */ -public class Book { +public class Book extends Item{ /** * Declaration of private class variables : book title, kind and author */ - private String title, kind, author, comment; + private String author, comment; private int nbPages; - private LinkedList<Review> reviews = new LinkedList<>(); + /** * <b>Initialization of private variables with constructor</b> @@ -29,9 +29,8 @@ public class Book { * @param nbPages The number of pages of the book */ public Book(String title, String kind, String author, int nbPages) { - this.title = title; - this.kind = kind; - this.author = author; + super(title, kind); + this.author = author.trim(); this.nbPages = nbPages; } @@ -71,36 +70,12 @@ public class Book { return this.nbPages; } - /** - * <b>Identify if the current book's <i>title</i> is similar as another book</b> - * - * @param title2 other's book's title - * @return true if same, false if different - */ - public boolean compareTitle(String title2) { - String formattedTitle = this.title.toUpperCase().trim(); - String formattedTitle2 = title2.toUpperCase().trim(); - return formattedTitle.equals(formattedTitle2); - } - public void addOrUpdateReview(String login, float mark, String comment) { - for (Review r : reviews) { - if (r.getReviewerLogin().equalsIgnoreCase(login)) { - r.setMark(mark); - r.setComment(comment); - return; - } - } - reviews.add(new Review(login, mark, comment)); - } - public float getAverageMark() { - if (reviews.isEmpty()) return 0f; - float total = 0f; - for (Review r : reviews) { - total += r.getMark(); - } - return total / reviews.size(); - } + + + + + /** * <b>String function to display the details of a book</b> * @@ -109,10 +84,10 @@ public class Book { public String toString() { String text = "Book : " + getTitle() + "\nKind : " + getKind() + "\nAuthor : " + getAuthor() + "\nNumber of pages : " + getNbPages()+"\nOverall note: "+getAverageMark() +"\nReviews"; int counter = 1; - for (Review r : reviews) { - text += counter + ". " + r.getReviewerLogin() + "\t" + r.getMark() + "\t" + r.getComment(); - counter++; - } + //for (Review r : reviews) { + // text += counter + ". " + r.getReviewerLogin() + "\t" + r.getMark() + "\t" + r.getComment(); + // counter++; + //} return text; } } diff --git a/src/opinion/Film.java b/src/opinion/Film.java new file mode 100644 index 0000000000000000000000000000000000000000..d0cc09cdf64cf4c8f9a9f94e4577daba1cb2e877 --- /dev/null +++ b/src/opinion/Film.java @@ -0,0 +1,37 @@ +package opinion; + +import java.util.LinkedList; + +public class Film extends Item { + private String director; + private String scenarist; + private int duration; + + + + public Film(String title, String kind, String director, String scenarist, int duration) { + super(title, kind); + this.director = director.trim(); + this.scenarist = scenarist.trim(); + this.duration = duration; + + } + + public String getDirector() { + return director; + } + + public String getScenarist() { + return scenarist; + } + + public int getDuration() { + return duration; + } + + @Override + public String toString() { + return "Film Title: " + title + "\nKind: " + kind + "\nDirector: " + director + + "\nScenarist: " + scenarist + "\nDuration: " + duration + " minutes"; + } +} diff --git a/src/opinion/Item.java b/src/opinion/Item.java new file mode 100644 index 0000000000000000000000000000000000000000..ceaeba516f2027dffdf9ce3c9f84e37066ed8273 --- /dev/null +++ b/src/opinion/Item.java @@ -0,0 +1,60 @@ +package opinion; + +import java.util.LinkedList; + +public abstract class Item { + protected String title; + protected String kind; + + private LinkedList<Review> reviews = new LinkedList<>(); + + public Item(String title, String kind) { + this.title = title.trim(); + this.kind = kind.trim(); + this.reviews = new LinkedList<>(); + } + + public String getTitle() { + return title; + } + + public String getKind() { + return kind; + } + + public LinkedList<Review> getReviews() { + return reviews; + } + + public void addReview(Review review) { + reviews.add(review); + } + + public abstract String toString(); + + public boolean compareTitle(String title2) { + if (this.title == null || title2 == null) return false; + String formattedTitle = this.title.trim().toUpperCase(); + String formattedTitle2 = title2.trim().toUpperCase(); + return formattedTitle.equals(formattedTitle2); + } + public void addOrUpdateReview(String login, float mark, String comment) { + for (Review r : reviews) { + if (r.getReviewerLogin().equalsIgnoreCase(login)) { + r.setMark(mark); + r.setComment(comment); + return; + } + } + reviews.add(new Review(login, mark, comment)); + } + public float getAverageMark() { + if (reviews.isEmpty()) return 0f; + float total = 0f; + for (Review r : reviews) { + total += r.getMark(); + } + return total / reviews.size(); + } +} + diff --git a/src/opinion/SocialNetwork.java b/src/opinion/SocialNetwork.java index 7a5ba3fce88bd6c3c704944a38e57b1f5e2895fb..6c150410266e4f0e98f5ba53ca571d81e247f964 100644 --- a/src/opinion/SocialNetwork.java +++ b/src/opinion/SocialNetwork.java @@ -19,7 +19,8 @@ public class SocialNetwork implements ISocialNetwork { private int nbmembers, nbbooks; private LinkedList<Book> listBook; private Book book; - + private LinkedList<Film> listFilm; + private int nbfilms; /** * Constructs new SocialNetwork instance with empty member list. @@ -27,8 +28,10 @@ public class SocialNetwork implements ISocialNetwork { public SocialNetwork() { listMember = new LinkedList<Member>(); listBook = new LinkedList<Book>(); + listFilm = new LinkedList<Film>(); nbmembers = 0; nbbooks = 0; + nbfilms = 0; } /** @@ -41,16 +44,8 @@ public class SocialNetwork implements ISocialNetwork { return nbmembers; } - /** - * Returns number of films. - * - * @return 0 - */ - @Override - public int nbFilms() { - // TODO Auto-generated method stub - return 0; - } + + /** * Returns number of books. @@ -155,9 +150,66 @@ public class SocialNetwork implements ISocialNetwork { nbmembers++; } + @Override public void addItemFilm(String login, String password, String title, String kind, String director, String scriptwriter, int duration) throws BadEntryException, NotMemberException, ItemFilmAlreadyExistsException { + + validateUser(login, password); + + if (title == null || title.trim().isEmpty()) + throw new BadEntryException("Error: Title is null or empty"); + if (kind == null || kind.trim().isEmpty()) + throw new BadEntryException("Error: Kind is null or empty"); + if (director == null || director.trim().isEmpty()) + throw new BadEntryException("Error: Director is null or empty"); + if (scriptwriter == null || scriptwriter.trim().isEmpty()) + throw new BadEntryException("Error: Scriptwriter is null or empty"); + if (duration <= 0) + throw new BadEntryException("Error: Duration must be > 0"); + if (!authenticateUser(login, password)) + throw new NotMemberException("Error: Invalid login or password"); + + for (Film f : listFilm) { + if (f.compareTitle(title)) { + throw new ItemFilmAlreadyExistsException(); + } + } + + Film film = new Film(title.trim(), kind.trim(), director.trim(), scriptwriter.trim(), duration); + listFilm.add(film); + nbfilms++; + } + /** + * Returns number of films. + * + * @return 0 + */ + @Override + public int nbFilms() { + return nbfilms; + } + + @Override + public float reviewItemFilm(String login, String password, String title, float mark, String comment) + throws BadEntryException, NotMemberException, NotItemException { + + validateUser(login, password); + if (comment == null) + throw new BadEntryException("Error: Comment is null"); + if (mark < 0.0f || mark > 5.0f) + throw new BadEntryException("Error: Mark must be between 0.0 and 5.0"); + if (!authenticateUser(login, password)) + throw new NotMemberException("Error: Invalid login or password"); + + for (Film f : listFilm) { + if (f.compareTitle(title)) { + f.addOrUpdateReview(login.trim(), mark, comment); + return f.getAverageMark(); + } + } + + throw new NotItemException("Error: Film not found"); } /** @@ -196,12 +248,7 @@ public class SocialNetwork implements ISocialNetwork { nbbooks++; } - @Override - public float reviewItemFilm(String login, String password, String title, float mark, String comment) - throws BadEntryException, NotMemberException, NotItemException { - // TODO Auto-generated method stub - return 0; - } + /** * diff --git a/src/tests/AddItemFilmTest.java b/src/tests/AddItemFilmTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bdd811f5cb1f6b51a3780e111e43941ebf13e0aa --- /dev/null +++ b/src/tests/AddItemFilmTest.java @@ -0,0 +1,62 @@ +package tests; + +import opinion.ISocialNetwork; +import opinion.SocialNetwork; +import exceptions.BadEntryException; +import exceptions.MemberAlreadyExistsException; +import exceptions.NotMemberException; +import exceptions.ItemFilmAlreadyExistsException; + +/** + * Tests for the SocialNetwork.addItemFilm() method. + */ +public class AddItemFilmTest { + + private static int addItemFilmCheck(ISocialNetwork sn, String login, String password, String title, + String kind, String director, String scenarist, int duration, + String testId, String errorMessage) { + try { + sn.addItemFilm(login, password, title, kind, director, scenarist, duration); + System.out.println("Test " + testId + " passed."); + return 0; + } catch (BadEntryException | NotMemberException | ItemFilmAlreadyExistsException e) { + System.out.println("Test " + testId + " threw exception: " + e.getMessage()); + return 0; + } catch (Exception e) { + System.out.println("Test " + testId + " failed: " + errorMessage + " Unexpected exception: " + e); + return 1; + } + } + + public static void main(String[] args) { + ISocialNetwork sn = new SocialNetwork(); + int nbTests = 0; + int nbErrors = 0; + + // Primero registramos un miembro + try { + sn.addMember("john", "password123", "film critic"); + } catch (Exception e) { + System.out.println("Error setting up test member: " + e); + return; + } + + // Test válido + nbTests++; + nbErrors += addItemFilmCheck(sn, "john", "password123", "Inception", "Sci-Fi", "Christopher Nolan", "Christopher Nolan", 148, + "1.1", "Failed to add valid film"); + + // Test con duración inválida + nbTests++; + nbErrors += addItemFilmCheck(sn, "john", "password123", "Matrix", "Action", "Wachowskis", "Wachowskis", -120, + "1.2", "Negative duration accepted"); + + // Test con login incorrecto + nbTests++; + nbErrors += addItemFilmCheck(sn, "unknown", "password123", "Avatar", "Fantasy", "James Cameron", "James Cameron", 162, + "1.3", "Unregistered member accepted"); + + // Resumen + System.out.println("\nAddItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s)."); + } +} diff --git a/src/tests/ReviewItemFilmTest.java b/src/tests/ReviewItemFilmTest.java new file mode 100644 index 0000000000000000000000000000000000000000..43c37114e8296e70654b0493c1bfdc8a6ba2ce06 --- /dev/null +++ b/src/tests/ReviewItemFilmTest.java @@ -0,0 +1,65 @@ +package tests; + +import opinion.ISocialNetwork; +import opinion.SocialNetwork; +import exceptions.BadEntryException; +import exceptions.NotMemberException; +import exceptions.NotItemException; + +/** + * Tests for the SocialNetwork.reviewItemFilm() method. + */ +public class ReviewItemFilmTest { + + private static int reviewItemFilmCheck(ISocialNetwork sn, String login, String password, String title, + float mark, String comment, String testId, String errorMessage) { + try { + float avg = sn.reviewItemFilm(login, password, title, mark, comment); + System.out.println("Test " + testId + " passed. Avg mark: " + avg); + return 0; + } catch (BadEntryException | NotMemberException | NotItemException e) { + System.out.println("Test " + testId + " threw expected exception: " + e.getMessage()); + return 0; + } catch (Exception e) { + System.out.println("Test " + testId + " failed: " + errorMessage + " Unexpected exception: " + e); + return 1; + } + } + + public static void main(String[] args) { + ISocialNetwork sn = new SocialNetwork(); + int nbTests = 0; + int nbErrors = 0; + + try { + sn.addMember("alice", "strongpass", "cinema lover"); + sn.addItemFilm("alice", "strongpass", "The Matrix", "Sci-Fi", "Wachowski", "Wachowski", 136); + } catch (Exception e) { + System.out.println("Setup failed: " + e); + return; + } + + // Valid test + nbTests++; + nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 4.5f, "Amazing film!", + "4.1", "Valid review rejected"); + + // Test out of range + nbTests++; + nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "The Matrix", 6.0f, "Too high!", + "4.2", "Accepted invalid mark > 5"); + + // Test with invalid title + nbTests++; + nbErrors += reviewItemFilmCheck(sn, "alice", "strongpass", "Unknown Film", 3.0f, "Not found", + "4.3", "Accepted review for nonexistent film"); + + // Test with no registered user + nbTests++; + nbErrors += reviewItemFilmCheck(sn, "bob", "1234", "The Matrix", 3.0f, "Hacked!", + "4.4", "Accepted review from non-member"); + + // Resumen + System.out.println("\nReviewItemFilmTest summary: " + nbErrors + " error(s) out of " + nbTests + " test(s)."); + } +}