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 all 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,124 @@
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
public String name;
public int age;
public double weight;
public boolean isIndoor;
public Breed breed;
public ArrayList<String> favoriteFoods;
public boolean isFemale;

// Constructor
public Cat(String name, int age, double weight, Breed breed, boolean isIndoor, boolean isFemale)
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.isFemale = isFemale;
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;
}

public String meow() {
return "Meow!";
}

public String walk() {
return name + " is walking.";
}

public String sleep() {
return name + " is sleeping.";
}

public boolean isKitten() {
return age < 1;
}

public String play() {
return name + " is playing.";
}

public String scratchFurniture() {
return name + " is scratching furniture.";
}

public String useLitterbox() {
return name + " is using the litterbox.";
}

public enum Gender {
MALE,
FEMALE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.codedifferently.lesson16.cutecat;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import org.junit.jupiter.api.Test;

public class CatTest {

@Test
public void testCreateNinjaCat() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
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, false);
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, false);
cat.printfavoriteFoods();
}

@Test
public void testAddFavoriteFood() throws InvalidCatNameException {
Cat cat = new Cat("Ninja", 13, 9.3, Breed.MAINCOON, true, false);
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, false);
assertTrue(cat.isSenior());
}

@Test
public void testCatName() throws InvalidCatNameException {
Cat cat = new Cat("Whiskers", 5, 5.4, Breed.SPHYNX, false, true);
assertEquals("Whiskers", cat.getName());
}

@Test
public void testCatMeow() throws InvalidCatNameException {
Cat cat = new Cat("Mittens", 2, 8.9, Breed.SIAMESE, true, true);
assertEquals("Meow!", cat.meow());
}

@Test
public void testGetName() throws InvalidCatNameException {
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, true);
assertEquals("Shadow", cat.getName());
}

@Test
public void testGetAge() throws InvalidCatNameException {
Cat cat = new Cat("Nine", 2, 13.2, Breed.MAINCOON, true, false);
assertEquals(2, cat.getAge());
}

@Test
public void testMeow() throws InvalidCatNameException {
Cat cat = new Cat("Frisky", 2, 5.4, Breed.UNKNOWN, true, true);
assertEquals("Meow!", cat.meow());
}

@Test
public void testWalk() throws InvalidCatNameException {
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
assertEquals("Shadow is walking.", cat.walk());
}

@Test
public void testSleep() throws InvalidCatNameException {
Cat cat = new Cat("Shadow", 2, 7.4, Breed.BENGAL, true, false);
assertEquals("Shadow is sleeping.", cat.sleep());
}

@Test
public void testisKittenTrue() throws InvalidCatNameException {
Cat cat = new Cat("Tiny", 0, 2.5, Breed.PERSIAN, true, true);
assertTrue(cat.isKitten());
}

@Test
public void testIsKittenFalse() throws InvalidCatNameException {
Cat cat = new Cat("Trinity", 3, 5.6, Breed.UNKNOWN, true, true);
assertFalse(cat.isKitten());
}
}