Skip to content

Commit 06eb376

Browse files
committed
feat: complete Lesson 16 Cat class with custom exception,enum and tests
1 parent dbd5296 commit 06eb376

File tree

2 files changed

+122
-0
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+122
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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, PERSIAN, MAINCOON, SPHYNX, BENGAL, UNKNOWN
16+
}
17+
18+
public class Cat {
19+
// Member variables
20+
private String name;
21+
private int age;
22+
private double weight;
23+
private boolean isIndoor;
24+
private Breed breed;
25+
private ArrayList<String> favoriteFoods;
26+
27+
// Constructor
28+
public Cat(String name, int age, double weight, Breed breed, boolean isIndoor) throws InvalidCatNameException {
29+
if (name == null || name.trim().isEmpty()) {
30+
throw new InvalidCatNameException("Cat name cannot be empty.");
31+
}
32+
this.name = name;
33+
this.age = age;
34+
this.weight = weight;
35+
this.isIndoor = isIndoor;
36+
this.breed = breed;
37+
this.favoriteFoods = new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); //Default for Ninja
38+
}
39+
40+
// Function 1: Add Favorite Food (uses collection)
41+
public void addfavoriteFood(String food) {
42+
favoriteFoods.add(food);
43+
}
44+
45+
//Function 2: Print Favorite Foods (uses loop)
46+
public void printfavoriteFoods() {
47+
System.out.println(name + "'s favorite foods:");
48+
for (String v : favoriteFoods) {
49+
System.out.println("- " + v);
50+
}
51+
}
52+
53+
// Function 3: Is the cat a senior? (uses conditional)
54+
public boolean isSenior() {
55+
return age >= 10;
56+
}
57+
58+
// Getters for testing
59+
public String getName() { return name; }
60+
public int getAge() { return age; }
61+
public double getWeight() { return weight; }
62+
public boolean getIsIndoor() { return isIndoor; }
63+
public Breed getBreed() { return breed; }
64+
public ArrayList<String> getfavoriteFoods() {return favoriteFoods; }
65+
}
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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);
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);
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);
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);
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);
46+
assertTrue(cat.isSenior());
47+
}
48+
49+
@Test
50+
public void testInvalidNameThrowsException() {
51+
assertThrows(InvalidCatNameException.class, () -> {
52+
new Cat("", 13, 9.3, Breed.MAINCOON, true);
53+
});
54+
}
55+
56+
}

0 commit comments

Comments
 (0)