Skip to content

feat: Added Zion's Movie Object and Test to lesson_16 #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.lunch;

public class InvalidCalorieException extends Exception {
public InvalidCalorieException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.codedifferently.lesson16.lunch;

import java.util.ArrayList;

public class Lunch {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably didn't mean to leave this here given the tests.


public enum LunchType {
VEGETARIAN,
NON_VEGETARIAN,
VEGAN,
GLUTEN_FREE
}

// Member variables
private final String mainDish; // String type
private final String sideDish; // String type
private final int calories; // Integer type
private final LunchType lunchType; // Enum type
private final ArrayList<String> drinks; // Collection type (ArrayList)

// Constructor
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType)
throws InvalidCalorieException {
if (calories <= 0) {
throw new InvalidCalorieException("Calories cannot be zero or negative.");
}
this.mainDish = mainDish;
this.sideDish = sideDish;
this.calories = calories;
this.lunchType = lunchType;
this.drinks = new ArrayList<>();
}

// Member function to add a drink
public void addDrink(String drink) {
drinks.add(drink);
}

// Member function to get a healthy message based on calories
public String getHealthMessage() {
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories.";
}

// Member function to display all drinks
public void displayDrinks() {

System.out.println("Available drinks:");
for (String drink : drinks) {
System.out.println("- " + drink);
}
}

// Getters for member variables (optional)
public String getMainDish() {
return mainDish;
}

public String getSideDish() {
return sideDish;
}

public int getCalories() {
return calories;
}

public LunchType getLunchType() {
return lunchType;
}

public ArrayList<String> getDrinks() {
return drinks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.codedifferently.lesson16.movie;

public enum Genres {
DRAMA,
COMEDY,
ACTION,
THRILLER,
HORROR,
SCIENCE_FICTION,
FANTASY,
ROMANCE,
ADVENTURE,
MYSTERY;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.codedifferently.lesson16.movie;

import java.util.ArrayList;

public class Movie {

// member variables
private String title;
private int releaseYear;
private double rating;
private final Genres genres;
private final MovieRating movieRating;
private ArrayList<String> actors;

// constructor
public Movie(
String title, int releaseYear, double rating, MovieRating movieRating, Genres genres) {
this.title = title;
this.releaseYear = releaseYear;
this.rating = rating;
this.movieRating = movieRating;
this.genres = genres;
this.actors = new ArrayList<String>();
}

// getters and setters member functions

public String getTitle() {
return title;
}

public String setTitle() {
return "Creed 2";
}

public int getReleaseYear() {
return releaseYear;
}

public int setReleaseYear(int releaseYear) {
return this.releaseYear = releaseYear;
}

public double getRating() {
return rating;
}

public double setRating(double rating) {
return this.rating = rating;
}

public Genres getGenres() {
return genres;
}

public MovieRating getMovieRating() {
return movieRating;
}

// Conditional
public String goodRating() {
if (rating >= 7.0) {
return "The movie is not good";
} else {
return "The movie is satisfactory";
}
}

public void addActor(String actor) {
actors.add(actor);
}

public String getActors() throws actorNotAvailableException {
if (actors.isEmpty()) {
throw new actorNotAvailableException("actors not available");
}
String names = "";
for (String actor : actors) {
names += actor + ",";
}
return names;
}
}

class actorNotAvailableException extends Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActorNotAvailableException

public actorNotAvailableException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codedifferently.lesson16.movie;

public enum MovieRating {
G,
PG,
PG_13,
R;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.codedifferently.lesson16.movie;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MovieTest {
Movie movie;

@BeforeEach
public void setUp() {
movie = new Movie("Creed", 2015, 7.6, MovieRating.PG_13, Genres.ACTION);
}

@Test
void testGetMovieTitle() {
// act
String title = movie.getTitle();
// assert
assertEquals("Creed", title);
}

@Test
void testSetMovieTitle() {
// arrange
// act
String title = movie.setTitle();
// assert
assertEquals("Creed 2", title);
}

@Test
void testGetReleaseYear() {
int releaseYear = movie.getReleaseYear();
assertEquals(2015, 2015);
}

@Test
void testSetReleaseYear() {
int releaseYear = movie.setReleaseYear(2018);
assertEquals(movie.getReleaseYear(), 2018);
}

@Test
void testGetRating() {
double rating = movie.getRating();
assertEquals(movie.getRating(), 7.6);
}

@Test
void testGetGenres() {
assertEquals(Genres.ACTION, movie.getGenres());
}
}