Skip to content

Feat: Adds Nile Jackson's .java files for lesson16 #524

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

Closed
wants to merge 12 commits into from
80 changes: 80 additions & 0 deletions lesson_16/objects/objects_app/src/main/java/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.ArrayList;
import java.util.List;

public class Dog implements DogTest {

// Custom exception
public static class DogAgeException extends Exception {

public DogAgeException(String message) {
super(message);
}
}

// Enum for dog colors
public enum Colors {
WHITE,
BROWN,
BLONDE
}

// Member variables
private int age;
private String breed;
private String gender;
private Colors color;
private boolean isFed;
private List<String> favoriteToys;

// Constructor
public Dog(int age, String breed, String gender, Colors color, boolean isFed)
throws DogAgeException {
if (age < 0) {
throw new DogAgeException("Age cannot be negative.");
}
this.age = age;
this.breed = breed;
this.gender = gender;
this.color = color;
this.isFed = isFed;
this.favoriteToys = new ArrayList<>();
}

// Member functions
public String getDogDescription() {
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
}

public void addFavoriteToy(String toy) {
favoriteToys.add(toy);
}

public List<String> getFavoriteToys() {
List<String> toys = new ArrayList<>();
for (String toy : favoriteToys) { // Loop
toys.add(toy);
}
return toys;
}

// Getters
public int getAge() {
return age;
}

public String getBreed() {
return breed;
}

public String getGender() {
return gender;
}

public Colors getColor() {
return color;
}

public boolean isFed() {
return isFed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.ArrayList;
import java.util.List;

public class Dog {
public abstract class EnumToArray {
enum Colors {
WHITE,
BROWN,
BLONDE;
}

//
public static void main(Enum[] args) {

List<Colors> colorList = new ArrayList<>();
for (Colors color : Colors.values()) {}
}

// Attributes
private int age;
private String breed;
private String gender;
private Boolean isFed, isNotFed;
private Colors color;

// Constructor
public void Dog(int age, String breed, String gender, Enum Colors, boolean isFed) {
this.age = age;
this.breed = breed;
this.gender = gender;
this.color = color;
this.isFed = isFed | isNotFed;
}

public static int myDogsAge(int Age) {
return 2;
}

public static String getbreed(String breed) {
return "mutt";
}

public static String getGender(String Gender) {
return "male";
}

public static Enum getColors(Enum Colors) {
return Colors;
}

public Boolean isFed(Boolean Fed) {

Boolean getFedStatus = isFed, isNotFed;

return getFedStatus == Fed;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.codedifferently.lesson16.dog;

import java.util.ArrayList;
import java.util.List;

public class Dog implements DogTest {

// Custom exception
public static class DogAgeException extends Exception {

public DogAgeException(String message) {
super(message);
}
}

// Enum for dog colors
public enum Colors {
WHITE,
BROWN,
BLONDE
}

// Member variables
private int age;
private String breed;
private String gender;
private Colors color;
private boolean isFed;
private List<String> favoriteToys;

// Constructor
public Dog(int age, String breed, String gender, Colors color, boolean isFed)
throws DogAgeException {
if (age < 0) {
throw new DogAgeException("Age cannot be negative.");
}
this.age = age;
this.breed = breed;
this.gender = gender;
this.color = color;
this.isFed = isFed;
this.favoriteToys = new ArrayList<>();
}

// Member functions
public String getDogDescription() {
return age > 5 ? "Senior Dog" : "Young Dog"; // Conditional expression
}

public void addFavoriteToy(String toy) {
favoriteToys.add(toy);
}

public List<String> getFavoriteToys() {
List<String> toys = new ArrayList<>();
for (String toy : favoriteToys) { // Loop
toys.add(toy);
}
return toys;
}

// Getters
public int getAge() {
return age;
}

public String getBreed() {
return breed;
}

public String getGender() {
return gender;
}

public Colors getColor() {
return color;
}

public boolean isFed() {
return isFed;
}

public String getFavoriteToys() {
return favoriteToy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.codedifferently.lesson16.Dog.Dog;
import org.junit.jupiter.api.Test;

public class DogTest {
public abstract class EnumToArray {
enum Colors {
WHITE,
BROWN,
BLONDE;
}
}

public int getAge(int age) {
return age;
}

@Test
public void testGetAge() {
int getAge = getAge(2);
int myDogsAge = getAge(2);
assertThat(getAge(2)).isNotNull();
}

public String getbreed(String breed) {
return breed;
}

@Test
public void testGetBreed() {
String getBreed = getbreed(getbreed("Mutt"));
assertThat(getbreed("Mutt"));
}

public String getGender(String gender) {
return gender;
}

@Test
public String testGetGender() {
String getGender = getGender(getGender("male"));

assertThat(getGender("male"));
return getbreed(getGender);
}

public Colors getColors(Colors Colors) {
return Colors;
}

@Test
public Colors testGetColors(Colors Colors) {
Colors testColor = Colors.BROWN;
assertThat(getColors(Colors));
return getColors(Colors);
}

@Test
public void testgetColor() {
assertThat(getColors(null));
}

@Test
Boolean isFed(Boolean Fed) {
var isNotFed = false;
var isFed = true;
var getFedStatus = Fed || isNotFed;
assertThat(getFedStatus).isTrue();
assertThat(Fed).isFalse();
assertThat(getFedStatus).isEqualTo(isFed);
return getFedStatus == Fed;
}

@Test
public void testDogConstructorThrowsExceptionForNegativeAge() {
// Arrange
Colors colorList = Colors.getBROWN();
int negativeAge = -1;
String breed = "Mutt";
String gender = "male";
boolean isFed = true;
Enum Color1 = null;

// Act & Assert
assertThatThrownBy(() -> new Dog(negativeAge, breed, gender, Color1, isFed))
.isInstanceOf(DogAgeException.class)
.hasMessage("Age cannot be negative.");
}

@Test
public String testaddFavoriteToy(String toy) {

Enum Color1 = null;

Dog dog = new Dog(2, "Mutt", "male", Color1, true);

String favoriteToy = "Squeaky Fish";

assertThat(favoriteToy).isEqualTo("Squeaky Fish");

return toy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

public class DogTest {

@Test
public void testGetAge() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
assertThat(dog.getAge()).isEqualTo(2);
}

@Test
public void testGetBreed() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
assertThat(dog.getBreed()).isEqualTo("Mutt");
}

@Test
public void testGetGender() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
assertThat(dog.getGender()).isEqualTo("male");
}

@Test
public void testGetColor() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
assertThat(dog.getColor()).isEqualTo(Dog.Colors.BROWN);
}

@Test
public void testIsFed() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
assertThat(dog.isFed()).isTrue();
}

@Test
public void testDogConstructorThrowsExceptionForNegativeAge() {
// Act & Assert
assertThatThrownBy(() -> new Dog(-1, "Mutt", "male", Dog.Colors.BROWN, true))
.isInstanceOf(Dog.DogAgeException.class)
.hasMessage("Age cannot be negative.");
}

@Test
public void testAddFavoriteToy() throws Dog.DogAgeException {
Dog dog = new Dog(2, "Mutt", "male", Dog.Colors.BROWN, true);
String favoriteToy = "Squeaky Fish";
dog.addFavoriteToy(favoriteToy);
assertThat(dog.getFavoriteToys()).contains(favoriteToy);
}
}
Loading