diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/InvalidViewIncrementException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/InvalidViewIncrementException.java new file mode 100644 index 000000000..ad6c8e5e4 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/InvalidViewIncrementException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.tiktokvideosystem; + +public class InvalidViewIncrementException extends RuntimeException { + public InvalidViewIncrementException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideo.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideo.java new file mode 100644 index 000000000..0215912b6 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideo.java @@ -0,0 +1,91 @@ +package com.codedifferently.lesson16.tiktokvideosystem; + +import java.util.ArrayList; + +public class TiktokVideo { + private String creatorName; + private int viewsCount; + private int likesCount; + private VideoCategory videoCategory; + private ArrayList commentsList; + + public enum VideoCategory { + DANCE, + COMEDY, + VLOG, + TUTORIAL + } + + public TiktokVideo( + String creatorName, int viewsCount, int likesCount, VideoCategory videoCategory) { + this.creatorName = creatorName; + this.viewsCount = viewsCount; + this.likesCount = likesCount; + this.videoCategory = videoCategory; + this.commentsList = new ArrayList<>(); + } + + // One function uses a conditional expression to check if the video has more than a specific + // number of views (e.g., 1 million views). + public void increaseViews(int amount) { + // Use a conditional to check if amount is positive + if (amount > 0) { + // If so, add amount to the views + this.viewsCount += amount; + } + // Else, either print a message or throw an exception + else { + throw new InvalidViewIncrementException("View increase amount must be positive."); + } + } + + public double likeToViewRatio() { + if (viewsCount == 0) { + return 0.0; + } + + int totalLikes = 0; + // used chat because I was not sure how to make a ratio within a for loop + for (int i = 0; i < likesCount; i++) { + totalLikes++; + } + + double ratio = (double) totalLikes / viewsCount; + return ratio; + } + + public void displayComments(ArrayList userComments) { + if (userComments == null) { + System.out.println("No comments yet. Be the first to comment!"); + } else { + for (int i = 0; i < userComments.size(); i++) { + System.out.println(userComments.get(i)); + } + } + } + + public void addComments(String comment) { + + commentsList.add(comment); + } + + public ArrayList getCommentsList() { + return commentsList; + } + + public String getCreator() { + return this.creatorName; + } + + public void setVideoCategory(VideoCategory videoType) { + this.videoCategory = videoType; + } + + public VideoCategory getVideoCategory() { + return this.videoCategory; + } + + public int getViewsCount() { + return this.viewsCount; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideoTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideoTest.java new file mode 100644 index 000000000..9d89a2b48 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideoTest.java @@ -0,0 +1,115 @@ +package com.codedifferently.lesson16.tiktokvideosystem; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.codedifferently.lesson16.tiktokvideosystem.TiktokVideo.VideoCategory; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class TiktokVideoTest { + + private TiktokVideo tiktokVideo; + + @BeforeEach + void setUp() { + tiktokVideo = + new TiktokVideo("enigivensunday", 1000000, 500000, TiktokVideo.VideoCategory.VLOG); + } + + @Test + void testConstructor() { + TiktokVideo video = new TiktokVideo("Jane", 3000, 1000, VideoCategory.DANCE); + } + + @Test + void testSetAndGetVideoCategory() { + tiktokVideo.setVideoCategory(VideoCategory.COMEDY); + assertEquals(VideoCategory.COMEDY, tiktokVideo.getVideoCategory()); + } + + @Test + void getVideoCategory() { + assertEquals(TiktokVideo.VideoCategory.VLOG, tiktokVideo.getVideoCategory()); + } + + @Test + void testViewsCount() { + assertEquals(1000000, tiktokVideo.getViewsCount()); + } + + @Test + void testIncreaseViewsPositive() { + tiktokVideo.increaseViews(500); + // got the is matcher idea from chat gpt + assertEquals(1000500, tiktokVideo.getViewsCount()); + } + + @Test + void testDecreaseViewsNegative() { + InvalidViewIncrementException exception = + assertThrows( + InvalidViewIncrementException.class, + () -> { + tiktokVideo.increaseViews(-1); + }); + assertEquals("View increase amount must be positive.", exception.getMessage()); + } + + @Test + void testDisplayComments() { + // used google & chat for this unit test solution + // Arrange + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream printStream = new PrintStream(outputStream); + PrintStream originalOut = System.out; + System.setOut(printStream); + + ArrayList comments = new ArrayList<>(); + comments.add("Love this video!"); + comments.add("So funny"); + tiktokVideo.displayComments(comments); + System.setOut(originalOut); + String output = outputStream.toString().trim(); + String expectedOutput = "Love this video!\nSo funny"; + assertEquals(expectedOutput, output); + } + + @Test + void testDisplayCommentsWithNullList() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream printStream = new PrintStream(outputStream); + PrintStream originalOut = System.out; + System.setOut(printStream); + + tiktokVideo.displayComments(null); + + System.setOut(originalOut); + + String output = outputStream.toString().trim(); + assertEquals("No comments yet. Be the first to comment!", output); + } + + @Test + void getCreatorName() { + assertEquals("enigivensunday", tiktokVideo.getCreator()); + } + + @Test + void testAddComments() { + tiktokVideo.addComments("This is awesome!"); + + assertEquals("This is awesome!", tiktokVideo.getCommentsList().get(0)); + } + + @Test + void testLikeToViewRatio() { + // Create the object using the constructor with views and likes + double result = tiktokVideo.likeToViewRatio(); + assertEquals( + 0.5, result, 0.0001); // got the idea to Use delta for comparing doubles from chatGpt + } +}