Skip to content

Commit 6076094

Browse files
committed
chore: issues on Cat.java and CatTest.java, tests were failing
1 parent 9c75850 commit 6076094

File tree

3 files changed

+154
-72
lines changed
  • lesson_16/objects/objects_app/src

3 files changed

+154
-72
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/cutecat/Cat.java

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,26 @@ enum Breed {
2222

2323
public class Cat {
2424
// Member variables
25-
private String name;
26-
private int age;
27-
private double weight;
28-
private boolean isIndoor;
29-
private Breed breed;
30-
private ArrayList<String> favoriteFoods;
25+
public String name;
26+
public int age;
27+
public double weight;
28+
public boolean isIndoor;
29+
public Breed breed;
30+
public ArrayList<String> favoriteFoods;
31+
public boolean isFemale;
3132

3233
// Constructor
33-
public Cat(String name, int age, double weight, Breed breed, boolean isIndoor)
34-
throws InvalidCatNameException {
35-
if (name == null || name.trim().isEmpty()) {
36-
throw new InvalidCatNameException("Cat name cannot be empty.");
37-
}
38-
this.name = name;
39-
this.age = age;
40-
this.weight = weight;
41-
this.isIndoor = isIndoor;
42-
this.breed = breed;
34+
public Cat(String name, int age, double weight, Breed breed, boolean isIndoor, boolean isFemale)
35+
throws InvalidCatNameException {
36+
if (name == null || name.trim().isEmpty()) {
37+
throw new InvalidCatNameException("Cat name cannot be empty.");
38+
}
39+
this.name = name;
40+
this.age = age;
41+
this.weight = weight;
42+
this.isIndoor = isIndoor;
43+
this.breed = breed;
44+
this.isFemale = isFemale;
4345
this.favoriteFoods =
4446
new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); // Default for Ninja
4547
}
@@ -86,4 +88,37 @@ public Breed getBreed() {
8688
public ArrayList<String> getfavoriteFoods() {
8789
return favoriteFoods;
8890
}
91+
92+
public String meow() {
93+
return "Meow!";
94+
}
95+
96+
public String walk() {
97+
return name + " is walking.";
98+
}
99+
100+
public String sleep() {
101+
return name + " is sleeping.";
102+
}
103+
104+
public boolean isKitten() {
105+
return age < 1;
106+
}
107+
108+
public String play() {
109+
return name + " is playing.";
110+
}
111+
112+
public String scratchFurniture() {
113+
return name + " is scratching furniture.";
114+
}
115+
116+
public String useLitterbox() {
117+
return name + " is using the litterbox.";
118+
}
119+
120+
public enum Gender {
121+
MALE,
122+
FEMALE
123+
}
89124
}

lesson_16/objects/objects_app/src/test/cutecat/CatTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.codedifferently.lesson16.cutecat;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import java.util.ArrayList;
6+
7+
public class CatTest {
8+
9+
@Test
10+
public void testCreateNinjaCat() throws InvalidCatNameException {
11+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
12+
assertEquals("Ninja", cat.getName());
13+
assertEquals(13, cat.getAge());
14+
assertEquals(9.3, cat.getWeight());
15+
assertTrue(cat.getIsIndoor());
16+
assertEquals(Breed.MAINCOON, cat.getBreed());
17+
}
18+
19+
@Test
20+
public void testfavoriteFoodsDefault() throws InvalidCatNameException {
21+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
22+
ArrayList<String> foods = cat.getfavoriteFoods();
23+
assertTrue(foods.contains("Fancy Feast"));
24+
assertTrue(foods.contains("Purina Naturals"));
25+
assertEquals(2, foods.size());
26+
}
27+
28+
@Test
29+
public void testPrintFavoriteFoodsCoverage() throws InvalidCatNameException {
30+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
31+
cat.printfavoriteFoods();
32+
}
33+
34+
@Test
35+
public void testAddFavoriteFood() throws InvalidCatNameException {
36+
Cat cat = new Cat("Ninja", 13,9.3, Breed.MAINCOON, true, false);
37+
cat.addfavoriteFood("Tuna");
38+
ArrayList<String> foods = cat.getfavoriteFoods();
39+
assertTrue(foods.contains("Tuna"));
40+
assertEquals(3, foods.size());
41+
}
42+
43+
@Test
44+
public void testIsSenior() throws InvalidCatNameException {
45+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
46+
assertTrue(cat.isSenior());
47+
}
48+
49+
@Test
50+
public void testCatName() throws InvalidCatNameException {
51+
Cat cat = new Cat("Whiskers",5, 5.4, Breed.SPHYNX, false, true);
52+
assertEquals("Whiskers", cat.getName());
53+
}
54+
55+
@Test
56+
public void testCatMeow() throws InvalidCatNameException {
57+
Cat cat = new Cat("Mittens", 2, 8.9, Breed.SIAMESE, true, true);
58+
assertEquals("Meow!", cat.meow());
59+
}
60+
61+
@Test
62+
public void testGetName() throws InvalidCatNameException {
63+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, true);
64+
assertEquals("Shadow", cat.getName());
65+
}
66+
67+
@Test
68+
public void testGetAge() throws InvalidCatNameException {
69+
Cat cat = new Cat("Nine", 2, 13.2, Breed.MAINCOON, true, false);
70+
assertEquals(2, cat.getAge());
71+
}
72+
73+
@Test
74+
public void testMeow()throws InvalidCatNameException {
75+
Cat cat = new Cat("Frisky", 2, 5.4, Breed.UNKNOWN, true, true);
76+
assertEquals("Meow!", cat.meow());
77+
}
78+
79+
@Test
80+
public void testWalk() throws InvalidCatNameException {
81+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
82+
assertEquals("Shadow is walking.", cat.walk());
83+
}
84+
85+
@Test
86+
public void testSleep() throws InvalidCatNameException {
87+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
88+
assertEquals("Shadow is sleeping.", cat.sleep());
89+
}
90+
91+
@Test
92+
public void testisKittenTrue() throws InvalidCatNameException {
93+
Cat cat = new Cat("Tiny", 0, 2.5, Breed.PERSIAN, true, true);
94+
assertTrue(cat.isKitten());
95+
}
96+
97+
@Test
98+
public void testIsKittenFalse() throws InvalidCatNameException {
99+
Cat cat = new Cat("Trinity", 3, 5.6, Breed.UNKNOWN, true, true);
100+
assertFalse(cat.isKitten());
101+
}
102+
103+
}

0 commit comments

Comments
 (0)