As a first approach to start working on, the UML is based on the following rules:
The Movie Class is designed where an object movie can has different reviews.
Critic create objects for the Review class of a movie object.
The Review class depends on the Critic class and belongs to a movie object.
On this design we can see that the Movie class will have a multiplicity with the Critic class (since a movie can be reviewed by different critics). Also, the Critic class will has a multiplicity relationship wit the review class because a critic object can has multiple reviews of different movies.
classDiagram
class Movie {
-title: String
-reviews: ArrayList Review
}
class Critic {
-name: String
}
class Review {
-movie: Movie
-critic: Critic
-rating: int
-comment: String
}
Movie *-- Review
Review o-- Critic
5Creating classes
Proceeding with the tasks given, now it’s time to create the classes as per the UML designed.
The first class to be created is the Movie class. Lombok is used to create two constructors, one will al arguments, and another with no arguments (will allow to create empty objects). A third constructor is created. This constructor only needs a String whit the movie name.
Movie.java
@Data@AllArgsConstructor@NoArgsConstructorpublicclass Movie {// first approach for Movie classprivateString movieName;privateArrayList<Review> reviews;private Critic critic;publicMovie(String movieName){this.movieName= movieName;}}
Critic class is the same as the Movie class. Lombok creates two constructors and a third one that will allow creating Critic objects only with the criticName.
The Review class works only with two constructors, with all the arguments or without any. This way Reviews can only be created if i have all the parameters or empty, so it can be setted later in case that all the parameters doesn’t exist yet.
Review.java
@Data@AllArgsConstructor@NoArgsConstructorpublicclass Review {// First approach for Review classprivate Movie movie;private Critic critic;privateint rating;privateString comment;}
6First tests
With the classes already created it’s time to run the first test.
This first test consists on manually creating a Critic object, sets the name, prints it on screen and using assertEquals and assertNotEquals to compare both results, true and false.
CriticTest.java
publicclass CriticTest {@TestpublicvoidcreateCriticObjectTest(){// Manually creating a Critic object. Critic critic =newCritic(); critic.setCriticName("Victor");System.out.println(critic.getCriticName());// assertEquals and assertNotEquals testingassertEquals("Victor", critic.getCriticName());assertNotEquals("Paco", critic.getCriticName());}}
This second test uses the Faker library. First, a Movie object is created passing the movie name as a parameter when creating. Then, a faker object is created and will be used so declare a String with fake data. Then, an assertEquals and assertNotEquals compares are called.
MovieTest.java
publicclass MovieTests {@TestpublicvoidcreateMovieObjectTest(){// Manually creating a movie object. Movie alien =newMovie("Alien"); Faker faker =newFaker();String name = faker.twinPeaks().character();// assertEquals and assertNotEquals testingassertEquals("Alien", alien.getMovieName());assertNotEquals("Alien", name);}}
The ReviewTest class consist on manually creating a Movie object, a Critic object. When creating a Review object, all parameters are given on the creation of the object. Again, assertEquals and assertNotEquals compares the expected output with the actual output.
ReviewTest.java
publicclass ReviewTest {@TestpublicvoidcreateReviewObjectTest(){//Creating a review and set comment manually. Movie alien =newMovie("Alien"); Critic victor =newCritic("Victor"); Review alienReview =newReview(alien, victor,5,"Must see");// assertEquals and assertNotEquals testingassertEquals("Must see", alienReview.getComment());assertNotEquals("Predator", alienReview.getMovie().getMovieName());}}
This last test is made on the AppTest clases, to test the workflow of our program. First, empty objects are created. The next block of code fakes parameters. The third block of code sets all the faked parameters created. Note that on the setting parameters for the movie object, first we add a Review object to an ArrayList, and then is setted as a parameter.
And lastly, the expected output and the actual output is tested with assertEquals and assertNotEquals testing.
Now that we have our core classes ready and tested, it’s time to add new features. This will change the first UML that worked as some sort of a draft.
classDiagram
class Movie {
- title: String
- reviews: ArrayList Review
- awards: ArrayList Awards
}
class Review {
- movie: Movie
- critic: Critic
- rating: int
- comment: String
}
class Person{
- name: String
}
class Director extends Person{
- movies: ArrayList Movie
}
class Critic extends Person{
- reviews: ArrayList Review
}
class Awards{
- oscars: Set Oscar
}
class Oscar extends Awards{
- Boolean won
}
Movie *-- Review : Composition
Review o-- Critic extends Person : Aggregation
Movie *-- Awards : Composition
Awards <|-- Oscar extends Awards : Inheritance
Person <|-- Director extends Person : Inheritance
Person <|-- Critic extends Person : Inheritance