diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/cutecat/Cat.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/cutecat/Cat.java new file mode 100644 index 000000000..fab4d1d9e --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/cutecat/Cat.java @@ -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 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 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 + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/cutecat/CatTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/cutecat/CatTest.java new file mode 100644 index 000000000..85ad05624 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/cutecat/CatTest.java @@ -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 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 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()); + } +}