Skip to content
Snippets Groups Projects
Commit f052e54b authored by MASSY FERNANDEZ Neva Aracely's avatar MASSY FERNANDEZ Neva Aracely
Browse files

add: Film Item Book modification ReviewFilm AddItemFilm

parent ec0afe6d
No related branches found
No related tags found
1 merge request!9Merge code for iteration3 with main
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -10,15 +10,15 @@ import java.util.LinkedList; ...@@ -10,15 +10,15 @@ import java.util.LinkedList;
* @version 1.0 * @version 1.0
* @since 1.0 * @since 1.0
*/ */
public class Book { public class Book extends Item{
/** /**
* Declaration of private class variables : book title, kind and author * Declaration of private class variables : book title, kind and author
*/ */
private String title, kind, author, comment; private String author, comment;
private int nbPages; private int nbPages;
private LinkedList<Review> reviews = new LinkedList<>();
/** /**
* <b>Initialization of private variables with constructor</b> * <b>Initialization of private variables with constructor</b>
...@@ -29,9 +29,8 @@ public class Book { ...@@ -29,9 +29,8 @@ public class Book {
* @param nbPages The number of pages of the book * @param nbPages The number of pages of the book
*/ */
public Book(String title, String kind, String author, int nbPages) { public Book(String title, String kind, String author, int nbPages) {
this.title = title; super(title, kind);
this.kind = kind; this.author = author.trim();
this.author = author;
this.nbPages = nbPages; this.nbPages = nbPages;
} }
...@@ -71,36 +70,12 @@ public class Book { ...@@ -71,36 +70,12 @@ public class Book {
return this.nbPages; 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> * <b>String function to display the details of a book</b>
* *
...@@ -109,10 +84,10 @@ public class Book { ...@@ -109,10 +84,10 @@ public class Book {
public String toString() { public String toString() {
String text = "Book : " + getTitle() + "\nKind : " + getKind() + "\nAuthor : " + getAuthor() + "\nNumber of pages : " + getNbPages()+"\nOverall note: "+getAverageMark() +"\nReviews"; String text = "Book : " + getTitle() + "\nKind : " + getKind() + "\nAuthor : " + getAuthor() + "\nNumber of pages : " + getNbPages()+"\nOverall note: "+getAverageMark() +"\nReviews";
int counter = 1; int counter = 1;
for (Review r : reviews) { //for (Review r : reviews) {
text += counter + ". " + r.getReviewerLogin() + "\t" + r.getMark() + "\t" + r.getComment(); // text += counter + ". " + r.getReviewerLogin() + "\t" + r.getMark() + "\t" + r.getComment();
counter++; // counter++;
} //}
return text; return text;
} }
} }
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";
}
}
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();
}
}
...@@ -19,7 +19,8 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -19,7 +19,8 @@ public class SocialNetwork implements ISocialNetwork {
private int nbmembers, nbbooks; private int nbmembers, nbbooks;
private LinkedList<Book> listBook; private LinkedList<Book> listBook;
private Book book; private Book book;
private LinkedList<Film> listFilm;
private int nbfilms;
/** /**
* Constructs new SocialNetwork instance with empty member list. * Constructs new SocialNetwork instance with empty member list.
...@@ -27,8 +28,10 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -27,8 +28,10 @@ public class SocialNetwork implements ISocialNetwork {
public SocialNetwork() { public SocialNetwork() {
listMember = new LinkedList<Member>(); listMember = new LinkedList<Member>();
listBook = new LinkedList<Book>(); listBook = new LinkedList<Book>();
listFilm = new LinkedList<Film>();
nbmembers = 0; nbmembers = 0;
nbbooks = 0; nbbooks = 0;
nbfilms = 0;
} }
/** /**
...@@ -41,16 +44,8 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -41,16 +44,8 @@ public class SocialNetwork implements ISocialNetwork {
return nbmembers; return nbmembers;
} }
/**
* Returns number of films.
*
* @return 0
*/
@Override
public int nbFilms() {
// TODO Auto-generated method stub
return 0;
}
/** /**
* Returns number of books. * Returns number of books.
...@@ -155,9 +150,66 @@ public class SocialNetwork implements ISocialNetwork { ...@@ -155,9 +150,66 @@ 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);
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 { ...@@ -196,12 +248,7 @@ public class SocialNetwork implements ISocialNetwork {
nbbooks++; 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;
}
/** /**
* *
......
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).");
}
}
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).");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment