Skip to content

Commit 0191faa

Browse files
authored
feat: complete Lesson 16 Cat class with custom exception, enum and tests (#497)
* feat: complete Lesson 16 Cat class with custom exception,enum and tests * Fix: formatting issue in Cat.java * chore: issues on Cat.java and CatTest.java, tests were failing * chore: fix formatting issues in Cat.java and CatTest.java
1 parent 09ab407 commit 0191faa

File tree

2 files changed

+227
-0
lines changed
  • lesson_16/objects/objects_app/src
    • main/java/com/codedifferently/lesson16/cutecat
    • test/java/com/codedifferently/lesson16/cutecat

2 files changed

+227
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.codedifferently.lesson16.cutecat;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
// Exception
7+
class InvalidCatNameException extends Exception {
8+
public InvalidCatNameException(String message) {
9+
super(message);
10+
}
11+
}
12+
13+
// Enum for Cat Breed
14+
enum Breed {
15+
SIAMESE,
16+
PERSIAN,
17+
MAINCOON,
18+
SPHYNX,
19+
BENGAL,
20+
UNKNOWN
21+
}
22+
23+
public class Cat {
24+
// Member variables
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;
32+
33+
// Constructor
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;
45+
this.favoriteFoods =
46+
new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); // Default for Ninja
47+
}
48+
49+
// Function 1: Add Favorite Food (uses collection)
50+
public void addfavoriteFood(String food) {
51+
favoriteFoods.add(food);
52+
}
53+
54+
// Function 2: Print Favorite Foods (uses loop)
55+
public void printfavoriteFoods() {
56+
System.out.println(name + "'s favorite foods:");
57+
for (String v : favoriteFoods) {
58+
System.out.println("- " + v);
59+
}
60+
}
61+
62+
// Function 3: Is the cat a senior? (uses conditional)
63+
public boolean isSenior() {
64+
return age >= 10;
65+
}
66+
67+
// Getters for testing
68+
public String getName() {
69+
return name;
70+
}
71+
72+
public int getAge() {
73+
return age;
74+
}
75+
76+
public double getWeight() {
77+
return weight;
78+
}
79+
80+
public boolean getIsIndoor() {
81+
return isIndoor;
82+
}
83+
84+
public Breed getBreed() {
85+
return breed;
86+
}
87+
88+
public ArrayList<String> getfavoriteFoods() {
89+
return favoriteFoods;
90+
}
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+
}
124+
}
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 static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.ArrayList;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class CatTest {
9+
10+
@Test
11+
public void testCreateNinjaCat() throws InvalidCatNameException {
12+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
13+
assertEquals("Ninja", cat.getName());
14+
assertEquals(13, cat.getAge());
15+
assertEquals(9.3, cat.getWeight());
16+
assertTrue(cat.getIsIndoor());
17+
assertEquals(Breed.MAINCOON, cat.getBreed());
18+
}
19+
20+
@Test
21+
public void testfavoriteFoodsDefault() throws InvalidCatNameException {
22+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
23+
ArrayList<String> foods = cat.getfavoriteFoods();
24+
assertTrue(foods.contains("Fancy Feast"));
25+
assertTrue(foods.contains("Purina Naturals"));
26+
assertEquals(2, foods.size());
27+
}
28+
29+
@Test
30+
public void testPrintFavoriteFoodsCoverage() throws InvalidCatNameException {
31+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
32+
cat.printfavoriteFoods();
33+
}
34+
35+
@Test
36+
public void testAddFavoriteFood() throws InvalidCatNameException {
37+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
38+
cat.addfavoriteFood("Tuna");
39+
ArrayList<String> foods = cat.getfavoriteFoods();
40+
assertTrue(foods.contains("Tuna"));
41+
assertEquals(3, foods.size());
42+
}
43+
44+
@Test
45+
public void testIsSenior() throws InvalidCatNameException {
46+
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
47+
assertTrue(cat.isSenior());
48+
}
49+
50+
@Test
51+
public void testCatName() throws InvalidCatNameException {
52+
Cat cat = new Cat("Whiskers", 5, 5.4, Breed.SPHYNX, false, true);
53+
assertEquals("Whiskers", cat.getName());
54+
}
55+
56+
@Test
57+
public void testCatMeow() throws InvalidCatNameException {
58+
Cat cat = new Cat("Mittens", 2, 8.9, Breed.SIAMESE, true, true);
59+
assertEquals("Meow!", cat.meow());
60+
}
61+
62+
@Test
63+
public void testGetName() throws InvalidCatNameException {
64+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, true);
65+
assertEquals("Shadow", cat.getName());
66+
}
67+
68+
@Test
69+
public void testGetAge() throws InvalidCatNameException {
70+
Cat cat = new Cat("Nine", 2, 13.2, Breed.MAINCOON, true, false);
71+
assertEquals(2, cat.getAge());
72+
}
73+
74+
@Test
75+
public void testMeow() throws InvalidCatNameException {
76+
Cat cat = new Cat("Frisky", 2, 5.4, Breed.UNKNOWN, true, true);
77+
assertEquals("Meow!", cat.meow());
78+
}
79+
80+
@Test
81+
public void testWalk() throws InvalidCatNameException {
82+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
83+
assertEquals("Shadow is walking.", cat.walk());
84+
}
85+
86+
@Test
87+
public void testSleep() throws InvalidCatNameException {
88+
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
89+
assertEquals("Shadow is sleeping.", cat.sleep());
90+
}
91+
92+
@Test
93+
public void testisKittenTrue() throws InvalidCatNameException {
94+
Cat cat = new Cat("Tiny", 0, 2.5, Breed.PERSIAN, true, true);
95+
assertTrue(cat.isKitten());
96+
}
97+
98+
@Test
99+
public void testIsKittenFalse() throws InvalidCatNameException {
100+
Cat cat = new Cat("Trinity", 3, 5.6, Breed.UNKNOWN, true, true);
101+
assertFalse(cat.isKitten());
102+
}
103+
}

0 commit comments

Comments
 (0)