Skip to content
Open
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
85 changes: 85 additions & 0 deletions src/main/java/masonbrown/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package masonbrown;

public class Animal {

private String sound;
private int age;
private String name;
private double maxSpeed;
private String color;

// Constructor
public Animal(String sound, int age, String name, double maxSpeed, String color) {

if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}

this.sound = sound;
this.age = age;
this.name = name;
this.maxSpeed = maxSpeed;
this.color = color;
}

// Getters
public String getSound() {
return sound;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}

public double getMaxSpeed() {
return maxSpeed;
}

public String getColor() {
return color;
}

// Setters
public void setSound(String sound) {
this.sound = sound;
}

public void setAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
this.age = age;
}

public void setName(String name) {
this.name = name;
}

public void setMaxSpeed(double maxSpeed) {
this.maxSpeed = maxSpeed;
}

public void setColor(String color) {
this.color = color;
}

// Custom Method 1
public String makeSound() {
return sound;
}

// Custom Method 2
public boolean isFast() {
return maxSpeed > 10;
}

@Override
public String toString() {
return name + " is a " + color + " animal that makes the sound " + sound;
}
}

123 changes: 123 additions & 0 deletions src/test/java/masonbrowntest/AnimalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package masonbrowntest;

import masonbrown.Animal;
import org.junit.jupiter.api.Test;

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

class AnimalTest {

@Test
public void constructorShouldCreateAnimalWithValidData() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act + Assert
assertEquals("Meow", animal.getSound());
assertEquals(3, animal.getAge());
assertEquals("Marco", animal.getName());
assertEquals(15.5, animal.getMaxSpeed());
assertEquals("Coal", animal.getColor());
}

@Test
public void constructorWithNegativeAgeShouldThrowException() {

assertThrows(
IllegalArgumentException.class,
() -> new Animal("Meow", -1, "Marco", 15.5, "Coal")
);
}

@Test
public void setSoundShouldUpdateSound() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act
animal.setSound("Roar");

// Assert
assertEquals("Roar", animal.getSound());
}

@Test
public void setAgeShouldUpdateAge() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act
animal.setAge(5);

// Assert
assertEquals(5, animal.getAge());
}

@Test
public void setAgeWithNegativeValueShouldThrowException() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act + Assert
assertThrows(
IllegalArgumentException.class,
() -> animal.setAge(-1)
);
}

@Test
public void setColorShouldUpdateColor() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act
animal.setColor("Brown");

// Assert
assertEquals("Brown", animal.getColor());
}

@Test
public void makeSoundShouldReturnSound() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act
String result = animal.makeSound();

// Assert
assertEquals("Meow", result);
}

@Test
public void isFastShouldReturnTrueWhenSpeedGreaterThan10() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 15.5, "Coal");

// Act
boolean result = animal.isFast();

// Assert
assertTrue(result);
}

@Test
public void isFastShouldReturnFalseWhenSpeedLessThanOrEqualTo10() {

// Arrange
Animal animal = new Animal("Meow", 3, "Marco", 5, "Coal");

// Act
boolean result = animal.isFast();

// Assert
assertFalse(result);
}
}