Skip to content

Commit 76707f4

Browse files
committed
feat: adds unit test to test file and updates formatting in exception & main files
1 parent 3448743 commit 76707f4

File tree

3 files changed

+107
-70
lines changed

3 files changed

+107
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.codedifferently.lesson16.tiktokvideosystem;
22

3-
public class InvalidViewIncrementException extends RuntimeException {
4-
public InvalidViewIncrementException(String message) {
5-
super(message);
6-
7-
}
3+
public class InvalidViewIncrementException extends RuntimeException {
4+
public InvalidViewIncrementException(String message) {
5+
super(message);
6+
}
87
}

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/tiktokvideosystem/TiktokVideo.java

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,86 @@
22

33
import java.util.ArrayList;
44

5-
6-
75
public class TiktokVideo {
8-
private String creatorName;
9-
private int viewsCount;
10-
private int likesCount;
11-
private VideoCategory videoCategory;
12-
private ArrayList<String> commentsList;
6+
private String creatorName;
7+
private int viewsCount;
8+
private int likesCount;
9+
private VideoCategory videoCategory;
10+
private ArrayList<String> commentsList;
1311

14-
public enum VideoCategory {
12+
public enum VideoCategory {
1513
DANCE,
1614
COMEDY,
1715
VLOG,
1816
TUTORIAL
19-
}
20-
21-
22-
public TiktokVideo(String creatorName, int viewsCount, int likesCount, VideoCategory videoCategory ){
23-
this.creatorName = creatorName;
24-
this.viewsCount = viewsCount;
25-
this.likesCount = likesCount;
26-
this.videoCategory = videoCategory;
27-
this.commentsList = new ArrayList<>();
28-
}
29-
30-
31-
//One function uses a conditional expression to check if the video has more than a specific number of views (e.g., 1 million views).
32-
public void increaseViews(int amount){
33-
// Use a conditional to check if amount is positive
34-
if (amount > 0){
35-
// If so, add amount to the views
36-
this.viewsCount += amount;
37-
}
38-
// Else, either print a message or throw an exception
39-
else{
40-
throw new InvalidViewIncrementException("View increase amount must be positive.");
41-
}
42-
}
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+
}
4341

44-
public double likeToViewRatio() {
42+
public double likeToViewRatio() {
4543
if (viewsCount == 0) {
46-
return 0.0;
44+
return 0.0;
4745
}
4846

4947
int totalLikes = 0;
50-
//used chat because I was not sure how to make a ratio within a for loop
48+
// used chat because I was not sure how to make a ratio within a for loop
5149
for (int i = 0; i < likesCount; i++) {
52-
totalLikes++;
50+
totalLikes++;
5351
}
5452

5553
double ratio = (double) totalLikes / viewsCount;
5654
return ratio;
57-
}
58-
public void displayComments(ArrayList<String> userComments){
59-
if(userComments == null){
60-
System.out.println("No comments yet. Be the first to comment!");
61-
} else {
62-
for(int i = 0; i < userComments.size(); i++){
63-
System.out.println(userComments.get(i));
55+
}
6456

65-
}
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+
}
6664
}
67-
}
68-
public void addComments(String comment){
69-
70-
commentsList.add(comment);
71-
}
72-
public String getCreator(){
73-
return this.creatorName;
74-
}
75-
public void setVideoCategory(VideoCategory videoType){
76-
this.videoCategory = videoType;
77-
}
78-
public VideoCategory getVideoCategory (){
79-
return this.videoCategory;
65+
}
8066

81-
}
82-
}
83-
67+
public void addComments(String comment) {
8468

69+
commentsList.add(comment);
70+
}
8571

72+
public String getCreator() {
73+
return this.creatorName;
74+
}
8675

76+
public void setVideoCategory(VideoCategory videoType) {
77+
this.videoCategory = videoType;
78+
}
8779

80+
public VideoCategory getVideoCategory() {
81+
return this.videoCategory;
82+
}
8883

84+
public int getViewsCount() {
85+
return this.viewsCount;
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
package com.codedifferently.lesson16.tiktokvideosystem;
2-
import static com.codedifferently.lesson16.tiktokvideosystem.InvalidViewIncrementException;
3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.hamcrest.MatcherAssert.assertThat.assertEquals;
4+
import org.hamcrest.Matchers.is;
5+
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67

78
public class TiktokVideoTest {
8-
@Test
9-
9+
10+
private TiktokVideo tiktokVideo;
11+
12+
@BeforeEach
13+
void setUp() {
14+
tiktokVideo =
15+
new TiktokVideo("enigivensunday", 1000000, 500000, TiktokVideo.VideoCategory.VLOG);
16+
}
17+
18+
@Test
19+
void testConstructor() {
20+
TiktokVideo video = new TiktokVideo("Jane", 3000, 1000, VideoCategory.DANCE);
21+
}
22+
23+
@Test
24+
void getVideoCategory() {
25+
assertEquals(TiktokVideo.VideoCategory.VLOG, tiktokVideo.getVideoCategory());
26+
}
27+
28+
@Test
29+
void testIncreaseViewsPositive() {
30+
tiktokVideo.increaseViews(500);
31+
// got the is matcher idea from chat gpt
32+
assertThat(tiktokVideo.getViewsCount(), is(1500));
33+
}
34+
35+
@Test
36+
void testDecreaseViewsNegative() {
37+
tiktokVideo.InvalidViewIncrementException(-78);
38+
assertThrows(InvalidViewIncrementException.class, () -> tiktokVideo);
39+
}
40+
41+
@Test
42+
String showCommennts() {}
43+
44+
@Test
45+
void getCreatorName() {
46+
47+
assertEquals(TiktokVideo.String, tiktokVideo.getCreator());
48+
}
1049
}

0 commit comments

Comments
 (0)