Skip to content

feat: complete Lesson 16 Cat class with custom exception, enum and tests #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.codedifferently.lesson16.cutecat;

import java.util.ArrayList;
import java.util.Arrays;

// Exception
class InvalidCatNameException extends Exception {
public InvalidCatNameException(String message) {
super(message);
}
}

// Enum for Cat Breed
enum Breed {
SIAMESE,
PERSIAN,
MAINCOON,
SPHYNX,
BENGAL,
UNKNOWN
}

public class Cat {
// Member variables
private String name;
private int age;
private double weight;
private boolean isIndoor;
private Breed breed;
private ArrayList<String> favoriteFoods;

// Constructor
public Cat(String name, int age, double weight, Breed breed, boolean isIndoor)
throws InvalidCatNameException {
if (name == null || name.trim().isEmpty()) {
throw new InvalidCatNameException("Cat name cannot be empty.");
}
this.name = name;
this.age = age;
this.weight = weight;
this.isIndoor = isIndoor;
this.breed = breed;
this.favoriteFoods =
new ArrayList<>(Arrays.asList("Fancy Feast", "Purina Naturals")); // Default for Ninja
}

// Function 1: Add Favorite Food (uses collection)
public void addfavoriteFood(String food) {
favoriteFoods.add(food);
}

// Function 2: Print Favorite Foods (uses loop)
public void printfavoriteFoods() {
System.out.println(name + "'s favorite foods:");
for (String v : favoriteFoods) {
System.out.println("- " + v);
}
}

// Function 3: Is the cat a senior? (uses conditional)
public boolean isSenior() {
return age >= 10;
}

// Getters for testing
public String getName() {
return name;
}

public int getAge() {
return age;
}

public double getWeight() {
return weight;
}

public boolean getIsIndoor() {
return isIndoor;
}

public Breed getBreed() {
return breed;
}

public ArrayList<String> getfavoriteFoods() {
return favoriteFoods;
}
}
56 changes: 56 additions & 0 deletions lesson_16/objects/objects_app/src/test/cutecat/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.codedifferently.lesson16.cutecat;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;

public class CatTest {

@Test
public void testCreateNinjaCat() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true);
assertEquals("Ninja", cat.getName());
assertEquals(13, cat.getAge());
assertEquals(9.3, cat.getWeight());
assertTrue(cat.getIsIndoor());
assertEquals(Breed.MAINCOON, cat.getBreed());
}

@Test
public void testfavoriteFoodsDefault() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true);
ArrayList<String> foods = cat.getfavoriteFoods();
assertTrue(foods.contains("Fancy Feast"));
assertTrue(foods.contains("Purina Naturals"));
assertEquals(2, foods.size());
}

@Test
public void testPrintFavoriteFoodsCoverage() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true);
cat.printfavoriteFoods();
}

@Test
public void testAddFavoriteFood() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13,9.3, Breed.MAINCOON, true);
cat.addfavoriteFood("Tuna");
ArrayList<String> foods = cat.getfavoriteFoods();
assertTrue(foods.contains("Tuna"));
assertEquals(3, foods.size());
}

@Test
public void testIsSenior() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true);
assertTrue(cat.isSenior());
}

@Test
public void testInvalidNameThrowsException() {
assertThrows(InvalidCatNameException.class, () -> {
new Cat("", 13, 9.3, Breed.MAINCOON, true);
});
}

}
Loading