Skip to content

Commit eae01a8

Browse files
authored
feat: Added Zion's Movie Object and Test to lesson_16 (#528)
* feat: Added Zion's Bedroom Object and Test to lesson_16 * fix: naming errors * feat: added Zion's Movie Object and Test to lesson_16 hw * feix: Mresolved changes * feat: made changes to Zions Movie Folder for lesson_16
1 parent 725c15b commit eae01a8

File tree

6 files changed

+246
-0
lines changed

6 files changed

+246
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.lunch;
2+
3+
public class InvalidCalorieException extends Exception {
4+
public InvalidCalorieException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.codedifferently.lesson16.lunch;
2+
3+
import java.util.ArrayList;
4+
5+
public class Lunch {
6+
7+
public enum LunchType {
8+
VEGETARIAN,
9+
NON_VEGETARIAN,
10+
VEGAN,
11+
GLUTEN_FREE
12+
}
13+
14+
// Member variables
15+
private final String mainDish; // String type
16+
private final String sideDish; // String type
17+
private final int calories; // Integer type
18+
private final LunchType lunchType; // Enum type
19+
private final ArrayList<String> drinks; // Collection type (ArrayList)
20+
21+
// Constructor
22+
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType)
23+
throws InvalidCalorieException {
24+
if (calories <= 0) {
25+
throw new InvalidCalorieException("Calories cannot be zero or negative.");
26+
}
27+
this.mainDish = mainDish;
28+
this.sideDish = sideDish;
29+
this.calories = calories;
30+
this.lunchType = lunchType;
31+
this.drinks = new ArrayList<>();
32+
}
33+
34+
// Member function to add a drink
35+
public void addDrink(String drink) {
36+
drinks.add(drink);
37+
}
38+
39+
// Member function to get a healthy message based on calories
40+
public String getHealthMessage() {
41+
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories.";
42+
}
43+
44+
// Member function to display all drinks
45+
public void displayDrinks() {
46+
47+
System.out.println("Available drinks:");
48+
for (String drink : drinks) {
49+
System.out.println("- " + drink);
50+
}
51+
}
52+
53+
// Getters for member variables (optional)
54+
public String getMainDish() {
55+
return mainDish;
56+
}
57+
58+
public String getSideDish() {
59+
return sideDish;
60+
}
61+
62+
public int getCalories() {
63+
return calories;
64+
}
65+
66+
public LunchType getLunchType() {
67+
return lunchType;
68+
}
69+
70+
public ArrayList<String> getDrinks() {
71+
return drinks;
72+
}
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codedifferently.lesson16.movie;
2+
3+
public enum Genres {
4+
DRAMA,
5+
COMEDY,
6+
ACTION,
7+
THRILLER,
8+
HORROR,
9+
SCIENCE_FICTION,
10+
FANTASY,
11+
ROMANCE,
12+
ADVENTURE,
13+
MYSTERY;
14+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.codedifferently.lesson16.movie;
2+
3+
import java.util.ArrayList;
4+
5+
public class Movie {
6+
7+
// member variables
8+
private String title;
9+
private int releaseYear;
10+
private double rating;
11+
private final Genres genres;
12+
private final MovieRating movieRating;
13+
private ArrayList<String> actors;
14+
15+
// constructor
16+
public Movie(
17+
String title, int releaseYear, double rating, MovieRating movieRating, Genres genres) {
18+
this.title = title;
19+
this.releaseYear = releaseYear;
20+
this.rating = rating;
21+
this.movieRating = movieRating;
22+
this.genres = genres;
23+
this.actors = new ArrayList<String>();
24+
}
25+
26+
// getters and setters member functions
27+
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
public String setTitle() {
33+
return "Creed 2";
34+
}
35+
36+
public int getReleaseYear() {
37+
return releaseYear;
38+
}
39+
40+
public int setReleaseYear(int releaseYear) {
41+
return this.releaseYear = releaseYear;
42+
}
43+
44+
public double getRating() {
45+
return rating;
46+
}
47+
48+
public double setRating(double rating) {
49+
return this.rating = rating;
50+
}
51+
52+
public Genres getGenres() {
53+
return genres;
54+
}
55+
56+
public MovieRating getMovieRating() {
57+
return movieRating;
58+
}
59+
60+
// Conditional
61+
public String goodRating() {
62+
if (rating >= 7.0) {
63+
return "The movie is not good";
64+
} else {
65+
return "The movie is satisfactory";
66+
}
67+
}
68+
69+
public void addActor(String actor) {
70+
actors.add(actor);
71+
}
72+
73+
public String getActors() throws actorNotAvailableException {
74+
if (actors.isEmpty()) {
75+
throw new actorNotAvailableException("actors not available");
76+
}
77+
String names = "";
78+
for (String actor : actors) {
79+
names += actor + ",";
80+
}
81+
return names;
82+
}
83+
}
84+
85+
class actorNotAvailableException extends Exception {
86+
public actorNotAvailableException(String message) {
87+
super(message);
88+
}
89+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.movie;
2+
3+
public enum MovieRating {
4+
G,
5+
PG,
6+
PG_13,
7+
R;
8+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codedifferently.lesson16.movie;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class MovieTest {
9+
Movie movie;
10+
11+
@BeforeEach
12+
public void setUp() {
13+
movie = new Movie("Creed", 2015, 7.6, MovieRating.PG_13, Genres.ACTION);
14+
}
15+
16+
@Test
17+
void testGetMovieTitle() {
18+
// act
19+
String title = movie.getTitle();
20+
// assert
21+
assertEquals("Creed", title);
22+
}
23+
24+
@Test
25+
void testSetMovieTitle() {
26+
// arrange
27+
// act
28+
String title = movie.setTitle();
29+
// assert
30+
assertEquals("Creed 2", title);
31+
}
32+
33+
@Test
34+
void testGetReleaseYear() {
35+
int releaseYear = movie.getReleaseYear();
36+
assertEquals(2015, 2015);
37+
}
38+
39+
@Test
40+
void testSetReleaseYear() {
41+
int releaseYear = movie.setReleaseYear(2018);
42+
assertEquals(movie.getReleaseYear(), 2018);
43+
}
44+
45+
@Test
46+
void testGetRating() {
47+
double rating = movie.getRating();
48+
assertEquals(movie.getRating(), 7.6);
49+
}
50+
51+
@Test
52+
void testGetGenres() {
53+
assertEquals(Genres.ACTION, movie.getGenres());
54+
}
55+
}

0 commit comments

Comments
 (0)