Skip to content

Commit 61847f3

Browse files
feat: adds build for TiktokVideo creator system with custom class, enum, and tests (#509)
* feat: add TikTokVideo class with core features, exceptions, and test file * feat: adds unit test to test file and updates formatting in exception & main files * feat: adds unit test fwhile correcting methods in main * Fix: removing commented out code * Chore: formatting code
1 parent c57b6ad commit 61847f3

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.tiktokvideosystem;
2+
3+
public class InvalidViewIncrementException extends RuntimeException {
4+
public InvalidViewIncrementException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.codedifferently.lesson16.tiktokvideosystem;
2+
3+
import java.util.ArrayList;
4+
5+
public class TiktokVideo {
6+
private String creatorName;
7+
private int viewsCount;
8+
private int likesCount;
9+
private VideoCategory videoCategory;
10+
private ArrayList<String> commentsList;
11+
12+
public enum VideoCategory {
13+
DANCE,
14+
COMEDY,
15+
VLOG,
16+
TUTORIAL
17+
}
18+
19+
public TiktokVideo(
20+
String creatorName, int viewsCount, int likesCount, VideoCategory videoCategory) {
21+
this.creatorName = creatorName;
22+
this.viewsCount = viewsCount;
23+
this.likesCount = likesCount;
24+
this.videoCategory = videoCategory;
25+
this.commentsList = new ArrayList<>();
26+
}
27+
28+
// One function uses a conditional expression to check if the video has more than a specific
29+
// number of views (e.g., 1 million views).
30+
public void increaseViews(int amount) {
31+
// Use a conditional to check if amount is positive
32+
if (amount > 0) {
33+
// If so, add amount to the views
34+
this.viewsCount += amount;
35+
}
36+
// Else, either print a message or throw an exception
37+
else {
38+
throw new InvalidViewIncrementException("View increase amount must be positive.");
39+
}
40+
}
41+
42+
public double likeToViewRatio() {
43+
if (viewsCount == 0) {
44+
return 0.0;
45+
}
46+
47+
int totalLikes = 0;
48+
// used chat because I was not sure how to make a ratio within a for loop
49+
for (int i = 0; i < likesCount; i++) {
50+
totalLikes++;
51+
}
52+
53+
double ratio = (double) totalLikes / viewsCount;
54+
return ratio;
55+
}
56+
57+
public void displayComments(ArrayList<String> userComments) {
58+
if (userComments == null) {
59+
System.out.println("No comments yet. Be the first to comment!");
60+
} else {
61+
for (int i = 0; i < userComments.size(); i++) {
62+
System.out.println(userComments.get(i));
63+
}
64+
}
65+
}
66+
67+
public void addComments(String comment) {
68+
69+
commentsList.add(comment);
70+
}
71+
72+
public ArrayList<String> getCommentsList() {
73+
return commentsList;
74+
}
75+
76+
public String getCreator() {
77+
return this.creatorName;
78+
}
79+
80+
public void setVideoCategory(VideoCategory videoType) {
81+
this.videoCategory = videoType;
82+
}
83+
84+
public VideoCategory getVideoCategory() {
85+
return this.videoCategory;
86+
}
87+
88+
public int getViewsCount() {
89+
return this.viewsCount;
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.codedifferently.lesson16.tiktokvideosystem;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import com.codedifferently.lesson16.tiktokvideosystem.TiktokVideo.VideoCategory;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import java.util.ArrayList;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class TiktokVideoTest {
14+
15+
private TiktokVideo tiktokVideo;
16+
17+
@BeforeEach
18+
void setUp() {
19+
tiktokVideo =
20+
new TiktokVideo("enigivensunday", 1000000, 500000, TiktokVideo.VideoCategory.VLOG);
21+
}
22+
23+
@Test
24+
void testConstructor() {
25+
TiktokVideo video = new TiktokVideo("Jane", 3000, 1000, VideoCategory.DANCE);
26+
}
27+
28+
@Test
29+
void testSetAndGetVideoCategory() {
30+
tiktokVideo.setVideoCategory(VideoCategory.COMEDY);
31+
assertEquals(VideoCategory.COMEDY, tiktokVideo.getVideoCategory());
32+
}
33+
34+
@Test
35+
void getVideoCategory() {
36+
assertEquals(TiktokVideo.VideoCategory.VLOG, tiktokVideo.getVideoCategory());
37+
}
38+
39+
@Test
40+
void testViewsCount() {
41+
assertEquals(1000000, tiktokVideo.getViewsCount());
42+
}
43+
44+
@Test
45+
void testIncreaseViewsPositive() {
46+
tiktokVideo.increaseViews(500);
47+
// got the is matcher idea from chat gpt
48+
assertEquals(1000500, tiktokVideo.getViewsCount());
49+
}
50+
51+
@Test
52+
void testDecreaseViewsNegative() {
53+
InvalidViewIncrementException exception =
54+
assertThrows(
55+
InvalidViewIncrementException.class,
56+
() -> {
57+
tiktokVideo.increaseViews(-1);
58+
});
59+
assertEquals("View increase amount must be positive.", exception.getMessage());
60+
}
61+
62+
@Test
63+
void testDisplayComments() {
64+
// used google & chat for this unit test solution
65+
// Arrange
66+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
67+
PrintStream printStream = new PrintStream(outputStream);
68+
PrintStream originalOut = System.out;
69+
System.setOut(printStream);
70+
71+
ArrayList<String> comments = new ArrayList<>();
72+
comments.add("Love this video!");
73+
comments.add("So funny");
74+
tiktokVideo.displayComments(comments);
75+
System.setOut(originalOut);
76+
String output = outputStream.toString().trim();
77+
String expectedOutput = "Love this video!\nSo funny";
78+
assertEquals(expectedOutput, output);
79+
}
80+
81+
@Test
82+
void testDisplayCommentsWithNullList() {
83+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
84+
PrintStream printStream = new PrintStream(outputStream);
85+
PrintStream originalOut = System.out;
86+
System.setOut(printStream);
87+
88+
tiktokVideo.displayComments(null);
89+
90+
System.setOut(originalOut);
91+
92+
String output = outputStream.toString().trim();
93+
assertEquals("No comments yet. Be the first to comment!", output);
94+
}
95+
96+
@Test
97+
void getCreatorName() {
98+
assertEquals("enigivensunday", tiktokVideo.getCreator());
99+
}
100+
101+
@Test
102+
void testAddComments() {
103+
tiktokVideo.addComments("This is awesome!");
104+
105+
assertEquals("This is awesome!", tiktokVideo.getCommentsList().get(0));
106+
}
107+
108+
@Test
109+
void testLikeToViewRatio() {
110+
// Create the object using the constructor with views and likes
111+
double result = tiktokVideo.likeToViewRatio();
112+
assertEquals(
113+
0.5, result, 0.0001); // got the idea to Use delta for comparing doubles from chatGpt
114+
}
115+
}

0 commit comments

Comments
 (0)