Skip to content

Commit 6415e48

Browse files
committed
Feat:Fix to Object/Test.java files
1 parent 2d72e1f commit 6415e48

File tree

2 files changed

+196
-0
lines changed
  • lesson_16/objects/objects_app/src
    • main/java/com/codedifferently/lesson16/Nile'sDog16/com/codedifferently/com/codedifferently
    • test/java/com/codedifferently/lesson16/Nile'sDogTest16/com/codedifferently/lesson_16/Dog/com/codedifferently/lesson_16/Dog/com/codedifferently/lesson16

2 files changed

+196
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.codedifferently.lesson16.dog;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Dog implements DogTest {
7+
8+
// Custom exception
9+
public static class DogAgeException extends Exception {
10+
11+
public DogAgeException(String message) {
12+
super(message);
13+
}
14+
}
15+
16+
// Enum for dog colors
17+
public enum Colors {
18+
WHITE, BROWN, BLONDE
19+
}
20+
21+
// Member variables
22+
private int age;
23+
private String breed;
24+
private String gender;
25+
private Colors color;
26+
private boolean isFed;
27+
private List<String> favoriteToys;
28+
29+
// Constructor
30+
public Dog(int age, String breed, String gender, Colors color, boolean isFed) throws DogAgeException {
31+
if (age < 0) {
32+
throw new DogAgeException("Age cannot be negative.");
33+
}
34+
this.age = age;
35+
this.breed = breed;
36+
this.gender = gender;
37+
this.color = color;
38+
this.isFed = isFed;
39+
this.favoriteToys = new ArrayList<>();
40+
}
41+
42+
// Member functions
43+
public String getDogDescription() {
44+
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
45+
}
46+
47+
public void addFavoriteToy(String toy) {
48+
favoriteToys.add(toy);
49+
}
50+
51+
public List<String> getFavoriteToys() {
52+
List<String> toys = new ArrayList<>();
53+
for (String toy : favoriteToys) { // Loop
54+
toys.add(toy);
55+
}
56+
return toys;
57+
}
58+
59+
// Getters
60+
public int getAge() {
61+
return age;
62+
}
63+
64+
public String getBreed() {
65+
return breed;
66+
}
67+
68+
public String getGender() {
69+
return gender;
70+
}
71+
72+
public Colors getColor() {
73+
return color;
74+
}
75+
76+
public boolean isFed() {
77+
return isFed;
78+
}
79+
public String getFavoriteToys() {
80+
return favoriteToy;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.codedifferently.lesson16;
2+
import static org.assertj.core.api.Assertions.assertThat;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
7+
public class DogTest {
8+
public abstract class EnumToArray {
9+
enum Colors {
10+
WHITE,
11+
BROWN,
12+
BLONDE;
13+
}
14+
}
15+
16+
17+
18+
public int getAge(int age) {
19+
return age;
20+
}
21+
22+
@Test
23+
public void testGetAge() {
24+
int getAge = getAge(2);
25+
int myDogsAge = getAge(2);
26+
assertThat(getAge(2)).isNotNull();
27+
}
28+
29+
public String getbreed(String breed) {
30+
return breed;
31+
}
32+
33+
@Test
34+
public void testGetBreed() {
35+
String getBreed = getbreed(getbreed("Mutt"));
36+
assertThat(getbreed("Mutt"));
37+
}
38+
39+
public String getGender(String gender) {
40+
return gender;
41+
}
42+
43+
@Test
44+
public String testGetGender() {
45+
String getGender = getGender(getGender("male"));
46+
47+
assertThat(getGender("male"));
48+
return getbreed(getGender);
49+
}
50+
51+
public Enum getColors(Enum Colors) {
52+
return Colors;
53+
}
54+
55+
@Test
56+
public Enum testGetColors(Enum Colors) {
57+
Enum testColor = Colors;
58+
assertThat(getColors(Colors));
59+
return getColors(Colors);
60+
}
61+
62+
@Test
63+
public void testgetColor() {
64+
assertThat(getColors(null));
65+
}
66+
67+
@Test
68+
Boolean isFed(Boolean Fed) {
69+
var isNotFed = false;
70+
var isFed = true;
71+
var getFedStatus = Fed || isNotFed;
72+
assertThat(getFedStatus).isTrue();
73+
assertThat(Fed).isFalse();
74+
assertThat(getFedStatus).isEqualTo(isFed);
75+
return getFedStatus == Fed;
76+
}
77+
78+
@Test
79+
public void testDogConstructorThrowsExceptionForNegativeAge() {
80+
public class newDog extends Dog {
81+
82+
83+
// Arrange
84+
Enum colorList = getColors(null);
85+
int negativeAge = -1;
86+
String breed = "Mutt";
87+
String gender = "male";
88+
boolean isFed = true;
89+
Enum Color1 = null;
90+
91+
Dog dog = new Dog(2, "Mutt", "male", Color1, true);
92+
93+
//None of this is going to work without the correct import configuration
94+
95+
96+
// Act & Assert
97+
assertThatThrownBy(() -> new Dog(negativeAge, breed, gender, Color1, isFed))
98+
.isInstanceOf(DogAgeException.class)
99+
.hasMessage("Age cannot be negative.");
100+
return
101+
}
102+
103+
@Test
104+
public String testaddFavoriteToy(String toy) {
105+
106+
Enum Color1 = null;
107+
108+
String favoriteToy = "Squeaky Fish";
109+
110+
assertThat(favoriteToy).isEqualTo("Squeaky Fish");
111+
112+
return toy;
113+
}
114+
}

0 commit comments

Comments
 (0)