Skip to content

feat: adds build for TiktokVideo creator system with custom class, enum, and tests #509

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 5 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.tiktokvideosystem;

public class InvalidViewIncrementException extends RuntimeException {
public InvalidViewIncrementException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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
}
}