Skip to content

Commit d589fa1

Browse files
committed
Feat: spotlessApply fix
1 parent 6415e48 commit d589fa1

File tree

2 files changed

+150
-135
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

+150
-135
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Nile'sDog16/com/codedifferently/com/codedifferently/Dog.java

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,82 @@
55

66
public class Dog implements DogTest {
77

8-
// Custom exception
9-
public static class DogAgeException extends Exception {
8+
// Custom exception
9+
public static class DogAgeException extends Exception {
1010

11-
public DogAgeException(String message) {
12-
super(message);
13-
}
11+
public DogAgeException(String message) {
12+
super(message);
1413
}
14+
}
1515

16-
// Enum for dog colors
17-
public enum Colors {
18-
WHITE, BROWN, BLONDE
19-
}
16+
// Enum for dog colors
17+
public enum Colors {
18+
WHITE,
19+
BROWN,
20+
BLONDE
21+
}
2022

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-
}
23+
// Member variables
24+
private int age;
25+
private String breed;
26+
private String gender;
27+
private Colors color;
28+
private boolean isFed;
29+
private List<String> favoriteToys;
4130

42-
// Member functions
43-
public String getDogDescription() {
44-
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
31+
// Constructor
32+
public Dog(int age, String breed, String gender, Colors color, boolean isFed)
33+
throws DogAgeException {
34+
if (age < 0) {
35+
throw new DogAgeException("Age cannot be negative.");
4536
}
37+
this.age = age;
38+
this.breed = breed;
39+
this.gender = gender;
40+
this.color = color;
41+
this.isFed = isFed;
42+
this.favoriteToys = new ArrayList<>();
43+
}
4644

47-
public void addFavoriteToy(String toy) {
48-
favoriteToys.add(toy);
49-
}
45+
// Member functions
46+
public String getDogDescription() {
47+
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
48+
}
5049

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-
}
50+
public void addFavoriteToy(String toy) {
51+
favoriteToys.add(toy);
52+
}
5853

59-
// Getters
60-
public int getAge() {
61-
return age;
54+
public List<String> getFavoriteToys() {
55+
List<String> toys = new ArrayList<>();
56+
for (String toy : favoriteToys) { // Loop
57+
toys.add(toy);
6258
}
59+
return toys;
60+
}
6361

64-
public String getBreed() {
65-
return breed;
66-
}
62+
// Getters
63+
public int getAge() {
64+
return age;
65+
}
6766

68-
public String getGender() {
69-
return gender;
70-
}
67+
public String getBreed() {
68+
return breed;
69+
}
7170

72-
public Colors getColor() {
73-
return color;
74-
}
71+
public String getGender() {
72+
return gender;
73+
}
7574

76-
public boolean isFed() {
77-
return isFed;
78-
}
79-
public String getFavoriteToys() {
80-
return favoriteToy;
81-
}
75+
public Colors getColor() {
76+
return color;
77+
}
78+
79+
public boolean isFed() {
80+
return isFed;
81+
}
82+
83+
public String getFavoriteToys() {
84+
return favoriteToy;
85+
}
8286
}
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,125 @@
1-
package com.codedifferently.lesson16;
21
import static org.assertj.core.api.Assertions.assertThat;
2+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
33

4+
import java.util.ArrayList;
5+
import java.util.List;
46
import org.junit.jupiter.api.Test;
57

6-
78
public class DogTest {
8-
public abstract class EnumToArray {
9-
enum Colors {
9+
10+
// Test the Dog class
11+
public class Dog {
12+
// Custom exception
13+
public static class DogAgeException extends Exception {
14+
public DogAgeException(String message) {
15+
super(message);
16+
}
17+
}
18+
19+
// Enum for dog colors
20+
public enum Colors {
1021
WHITE,
1122
BROWN,
12-
BLONDE;
23+
BLONDE
1324
}
14-
}
15-
16-
1725

18-
public int getAge(int age) {
19-
return age;
20-
}
26+
// Member variables
27+
private int age;
28+
private String breed;
29+
private String gender;
30+
private Colors color;
31+
private boolean isFed;
32+
private List<String> favoriteToys;
33+
34+
// Constructor
35+
public Dog(int age, String breed, String gender, Colors color, boolean isFed)
36+
throws DogAgeException {
37+
if (age < 0) {
38+
throw new DogAgeException("Age cannot be negative.");
39+
}
40+
this.age = age;
41+
this.breed = breed;
42+
this.gender = gender;
43+
this.color = color;
44+
this.isFed = isFed;
45+
this.favoriteToys = new ArrayList<>();
46+
}
2147

22-
@Test
23-
public void testGetAge() {
24-
int getAge = getAge(2);
25-
int myDogsAge = getAge(2);
26-
assertThat(getAge(2)).isNotNull();
27-
}
48+
// Getters
49+
public int getAge() {
50+
return age;
51+
}
2852

29-
public String getbreed(String breed) {
30-
return breed;
31-
}
53+
public String getBreed() {
54+
return breed;
55+
}
3256

33-
@Test
34-
public void testGetBreed() {
35-
String getBreed = getbreed(getbreed("Mutt"));
36-
assertThat(getbreed("Mutt"));
37-
}
57+
public String getGender() {
58+
return gender;
59+
}
60+
61+
public Colors getColor() {
62+
return color;
63+
}
3864

39-
public String getGender(String gender) {
40-
return gender;
65+
public boolean isFed() {
66+
return isFed;
67+
}
68+
69+
public String getFavoriteToys() {
70+
String favoriteToy = "Squeaky Fish";
71+
return favoriteToy;
72+
}
73+
74+
public String addFavoriteToy(String favoriteToy2) {
75+
String favoriteToy = "Squeaky Fish";
76+
return favoriteToy;
77+
}
4178
}
4279

4380
@Test
44-
public String testGetGender() {
45-
String getGender = getGender(getGender("male"));
46-
47-
assertThat(getGender("male"));
48-
return getbreed(getGender);
81+
public void testGetAge() throws DogTest.Dog.DogAgeException {
82+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
83+
assertThat(dog.getAge()).isEqualTo(2);
4984
}
5085

51-
public Enum getColors(Enum Colors) {
52-
return Colors;
86+
@Test
87+
public void testGetBreed() throws DogTest.Dog.DogAgeException {
88+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
89+
assertThat(dog.getBreed()).isEqualTo("Mutt");
5390
}
5491

5592
@Test
56-
public Enum testGetColors(Enum Colors) {
57-
Enum testColor = Colors;
58-
assertThat(getColors(Colors));
59-
return getColors(Colors);
93+
public void testGetGender() throws DogTest.Dog.DogAgeException {
94+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
95+
assertThat(dog.getGender()).isEqualTo("male");
6096
}
6197

6298
@Test
63-
public void testgetColor() {
64-
assertThat(getColors(null));
99+
void DogTesttestGetColor() throws DogTest.Dog.DogAgeException {
100+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
101+
assertThat(dog.getColor()).isEqualTo(Dog.Colors.BROWN);
65102
}
66103

67104
@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;
105+
public void testIsFed() throws DogTest.Dog.DogAgeException {
106+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
107+
assertThat(dog.isFed()).isTrue();
76108
}
77109

78110
@Test
79111
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-
96112
// Act & Assert
97-
assertThatThrownBy(() -> new Dog(negativeAge, breed, gender, Color1, isFed))
98-
.isInstanceOf(DogAgeException.class)
113+
assertThatThrownBy(() -> new Dog(-1, "Mutt", "male", Dog.Colors.BROWN, true))
114+
.isInstanceOf(Dog.DogAgeException.class)
99115
.hasMessage("Age cannot be negative.");
100-
return
101116
}
102117

103118
@Test
104-
public String testaddFavoriteToy(String toy) {
105-
106-
Enum Color1 = null;
107-
119+
public void testAddFavoriteToy() throws Dog.DogAgeException {
120+
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
108121
String favoriteToy = "Squeaky Fish";
109-
110-
assertThat(favoriteToy).isEqualTo("Squeaky Fish");
111-
112-
return toy;
122+
dog.addFavoriteToy(favoriteToy);
123+
assertThat(dog.getFavoriteToys()).contains(favoriteToy);
113124
}
114125
}

0 commit comments

Comments
 (0)