From bfd40b07b1b24db38e31f7d626eff6e0f556e057 Mon Sep 17 00:00:00 2001 From: Haysel Santiago Date: Tue, 27 Feb 2018 08:00:02 -0500 Subject: [PATCH 1/2] Finished --- src/main/java/io/zipcoder/Application.java | 96 +++++++++++++++++++ src/main/java/io/zipcoder/Bird.java | 12 +++ src/main/java/io/zipcoder/Cat.java | 12 +++ src/main/java/io/zipcoder/Dog.java | 12 +++ src/main/java/io/zipcoder/Pet.java | 20 ++++ .../java/io/zipcoder/ApplicationTest.java | 18 ++++ src/test/java/io/zipcoder/BirdTest.java | 26 +++++ src/test/java/io/zipcoder/CatTest.java | 26 +++++ src/test/java/io/zipcoder/DogTest.java | 25 +++++ src/test/java/io/zipcoder/PetTest.java | 16 ++++ 10 files changed, 263 insertions(+) create mode 100644 src/main/java/io/zipcoder/Bird.java create mode 100644 src/main/java/io/zipcoder/Cat.java create mode 100644 src/main/java/io/zipcoder/Dog.java create mode 100644 src/main/java/io/zipcoder/Pet.java create mode 100644 src/test/java/io/zipcoder/BirdTest.java create mode 100644 src/test/java/io/zipcoder/CatTest.java create mode 100644 src/test/java/io/zipcoder/DogTest.java create mode 100644 src/test/java/io/zipcoder/PetTest.java diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 3a257cb..38205b7 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -1,5 +1,101 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Scanner; +import java.util.Collections; + public class Application { + + ArrayList pets = new ArrayList(); + + //split into own method + Scanner input = new Scanner(System.in); +public String petInformation() { + System.out.println("How many pets do you own?"); + int numberOfPets = input.nextInt(); + input.nextLine(); + + for (int i = 1; i <= numberOfPets; i++) { + System.out.println("What type of pet is pet number " + i + "?"); + String typeOfPet = input.nextLine(); + + if ("dog".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your dog?"); + String dogName = input.nextLine(); + + pets.add(new Dog(dogName)); + } else if ("cat".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your cat?"); + String catName = input.nextLine(); + + pets.add(new Cat(catName)); + } else if ("bird".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your bird?"); + String birdName = input.nextLine(); + + pets.add(new Bird(birdName)); + } else { + System.out.println("Sorry, I pretty much only care about your story if you own a dog ," + + "cat, or bird. Not a " + input); + } + } +StringBuilder sb = new StringBuilder(); + for (Pet pet : pets) { + System.out.println(pet.getName() + " says, " + pet.speak()); + sb.append(pet.getName() + " says, " + pet.speak()); + } + return sb.toString(); +} + + + public static void main(String[] args) { + ArrayList pets = new ArrayList(); + + Scanner input = new Scanner(System.in); + + System.out.println("How many pets do you own?"); + int numberOfPets = input.nextInt(); + input.nextLine(); + + for(int i = 1; i <= numberOfPets; i++) { + System.out.println("What type of pet is pet number " + i + "?"); + String typeOfPet = input.nextLine(); + + if("dog".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your dog?"); + String dogName = input.nextLine(); + + pets.add(new Dog(dogName)); + } + else if ("cat".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your cat?"); + String catName = input.nextLine(); + + pets.add(new Cat(catName)); + } + else if("bird".equalsIgnoreCase(typeOfPet)) { + + System.out.println("What is the name of your bird?"); + String birdName = input.nextLine(); + + pets.add(new Bird(birdName)); + } + else { + System.out.println("Sorry, I pretty much only care about your story if you own a dog ," + + "cat, or bird. Not a " + input); + } + } + + for(Pet pet : pets) { + System.out.println(pet.getName() + " says, " + pet.speak()); + } + + } + } diff --git a/src/main/java/io/zipcoder/Bird.java b/src/main/java/io/zipcoder/Bird.java new file mode 100644 index 0000000..e9e26e0 --- /dev/null +++ b/src/main/java/io/zipcoder/Bird.java @@ -0,0 +1,12 @@ +package io.zipcoder; + +public class Bird extends Pet { + public Bird(String name) { + super(name); + } + + @Override + public String speak() { + return "Squak"; + } +} diff --git a/src/main/java/io/zipcoder/Cat.java b/src/main/java/io/zipcoder/Cat.java new file mode 100644 index 0000000..9083db0 --- /dev/null +++ b/src/main/java/io/zipcoder/Cat.java @@ -0,0 +1,12 @@ +package io.zipcoder; + +public class Cat extends Pet { + public Cat(String name) { + super(name); + } + + @Override + public String speak() { + return "Meow"; + } +} diff --git a/src/main/java/io/zipcoder/Dog.java b/src/main/java/io/zipcoder/Dog.java new file mode 100644 index 0000000..d219b72 --- /dev/null +++ b/src/main/java/io/zipcoder/Dog.java @@ -0,0 +1,12 @@ +package io.zipcoder; + +public class Dog extends Pet { + public Dog(String name) { + super(name); + } + + @Override + public String speak() { + return "Woof"; + } +} diff --git a/src/main/java/io/zipcoder/Pet.java b/src/main/java/io/zipcoder/Pet.java new file mode 100644 index 0000000..b197d9b --- /dev/null +++ b/src/main/java/io/zipcoder/Pet.java @@ -0,0 +1,20 @@ +package io.zipcoder; + +public abstract class Pet { + + private String name; + + public Pet (String name) { + this.name = name; + } + public abstract String speak(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/src/test/java/io/zipcoder/ApplicationTest.java b/src/test/java/io/zipcoder/ApplicationTest.java index b744df5..2e5865f 100644 --- a/src/test/java/io/zipcoder/ApplicationTest.java +++ b/src/test/java/io/zipcoder/ApplicationTest.java @@ -1,5 +1,23 @@ package io.zipcoder; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import org.junit.Assert; +import org.junit.Test; public class ApplicationTest { +@Test + public void petsWithNamesAndSounds() { + Application app = new Application(); + Bird Bird = new Bird("Ugly Bird"); + Cat cat = new Cat("Fitzgerald"); + Dog dog = new Dog("Oreo"); + + String expected = "Oreo says, Woof\nUgly Bird says, Squak\nFitzgerald says, Meow\n"; + String actual = app.petInformation(); + Assert.assertEquals(expected, actual); +} + } diff --git a/src/test/java/io/zipcoder/BirdTest.java b/src/test/java/io/zipcoder/BirdTest.java new file mode 100644 index 0000000..9b3487d --- /dev/null +++ b/src/test/java/io/zipcoder/BirdTest.java @@ -0,0 +1,26 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class BirdTest { + + @Test + public void speak() { + Bird bird = new Bird("Birdie Bird"); + String expected = "Squak"; + String actual = bird.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void setNameTest() { + Bird bird = new Bird(""); + String expected = "Ugly Bird"; + bird.setName("Ugly Bird"); + String actual = bird.getName(); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/CatTest.java b/src/test/java/io/zipcoder/CatTest.java new file mode 100644 index 0000000..dfa739f --- /dev/null +++ b/src/test/java/io/zipcoder/CatTest.java @@ -0,0 +1,26 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CatTest { + + @Test + public void speak() { + Cat cat = new Cat("Fitzgerald"); + String expected = "Meow"; + String actual = cat.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void setNameTest() { + Cat cat = new Cat(""); + String expected = "Fitzgerald"; + cat.setName("Fitzgerald"); + String actual = cat.getName(); + Assert.assertEquals(expected, actual); + } + +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/DogTest.java b/src/test/java/io/zipcoder/DogTest.java new file mode 100644 index 0000000..dfd5315 --- /dev/null +++ b/src/test/java/io/zipcoder/DogTest.java @@ -0,0 +1,25 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class DogTest { + + @Test + public void speak() { + Dog dog = new Dog("Oreo"); + String expected = "Woof"; + String actual = dog.speak(); + Assert.assertEquals(expected, actual); + } + @Test + public void setNameTest() { + Dog dog = new Dog(""); + String expected = "Oreo"; + dog.setName("Oreo"); + String actual = dog.getName(); + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/PetTest.java b/src/test/java/io/zipcoder/PetTest.java new file mode 100644 index 0000000..e61f797 --- /dev/null +++ b/src/test/java/io/zipcoder/PetTest.java @@ -0,0 +1,16 @@ +package io.zipcoder; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PetTest { + + @Test + public void getName() { + Cat cat = new Cat("Fitzgerald"); + String actual = cat.getName(); + String expected = "Fitzgerald"; + assertEquals(expected, actual); + } +} \ No newline at end of file From b8134daa225c9f7db8d3ef8690727c092af60e07 Mon Sep 17 00:00:00 2001 From: Haysel Santiago Date: Tue, 27 Feb 2018 14:55:13 -0500 Subject: [PATCH 2/2] Changes made --- src/main/java/io/zipcoder/Application.java | 120 ++++++------------ src/main/java/io/zipcoder/Pet.java | 18 ++- .../java/io/zipcoder/ApplicationTest.java | 15 ++- 3 files changed, 63 insertions(+), 90 deletions(-) diff --git a/src/main/java/io/zipcoder/Application.java b/src/main/java/io/zipcoder/Application.java index 38205b7..81de582 100644 --- a/src/main/java/io/zipcoder/Application.java +++ b/src/main/java/io/zipcoder/Application.java @@ -7,95 +7,53 @@ public class Application { - ArrayList pets = new ArrayList(); - - //split into own method + private int numberOfPets; + protected ArrayList pets = new ArrayList(); Scanner input = new Scanner(System.in); -public String petInformation() { - System.out.println("How many pets do you own?"); - int numberOfPets = input.nextInt(); - input.nextLine(); - - for (int i = 1; i <= numberOfPets; i++) { - System.out.println("What type of pet is pet number " + i + "?"); - String typeOfPet = input.nextLine(); - - if ("dog".equalsIgnoreCase(typeOfPet)) { - - System.out.println("What is the name of your dog?"); - String dogName = input.nextLine(); - - pets.add(new Dog(dogName)); - } else if ("cat".equalsIgnoreCase(typeOfPet)) { - - System.out.println("What is the name of your cat?"); - String catName = input.nextLine(); - - pets.add(new Cat(catName)); - } else if ("bird".equalsIgnoreCase(typeOfPet)) { - System.out.println("What is the name of your bird?"); - String birdName = input.nextLine(); - - pets.add(new Bird(birdName)); - } else { - System.out.println("Sorry, I pretty much only care about your story if you own a dog ," + - "cat, or bird. Not a " + input); + public int storePetInformation(){ + System.out.println("How many pets do you own?"); + numberOfPets = input.nextInt(); + for(int i = 0; i pets = new ArrayList(); - - Scanner input = new Scanner(System.in); - - System.out.println("How many pets do you own?"); - int numberOfPets = input.nextInt(); - input.nextLine(); + public Pet userInputEachPet(){ + System.out.println("Type of pet?"); + String typeOfPet = input.next(); - for(int i = 1; i <= numberOfPets; i++) { - System.out.println("What type of pet is pet number " + i + "?"); - String typeOfPet = input.nextLine(); - - if("dog".equalsIgnoreCase(typeOfPet)) { - - System.out.println("What is the name of your dog?"); - String dogName = input.nextLine(); - - pets.add(new Dog(dogName)); - } - else if ("cat".equalsIgnoreCase(typeOfPet)) { - - System.out.println("What is the name of your cat?"); - String catName = input.nextLine(); - - pets.add(new Cat(catName)); - } - else if("bird".equalsIgnoreCase(typeOfPet)) { - - System.out.println("What is the name of your bird?"); - String birdName = input.nextLine(); - - pets.add(new Bird(birdName)); - } - else { - System.out.println("Sorry, I pretty much only care about your story if you own a dog ," + - "cat, or bird. Not a " + input); - } + System.out.println("What is the pet's name?"); + String nameOfPet = input.next(); + Pet pet = new Pet(nameOfPet); + if(typeOfPet.equalsIgnoreCase("Cat")){ + pet = new Cat(nameOfPet); } - - for(Pet pet : pets) { - System.out.println(pet.getName() + " says, " + pet.speak()); + else if (typeOfPet.equalsIgnoreCase("Dog")){ + pet = new Dog(nameOfPet); + } + else if(typeOfPet.equalsIgnoreCase("Bird")){ + pet = new Bird(nameOfPet); } + return pet; + } + public String petNameAndSpeak(ArrayList pets) { + StringBuilder petList = new StringBuilder(); + for(int i = 0; i < pets.size(); i++) { + petList.append(pets.get(i).getName() + " " + pets.get(i).speak()+"\n"); + } + System.out.println(petList.toString().trim()); + return petList.toString().trim(); + } + public void addPet(Pet pet) { + pets.add(pet); + } + public static void main(String [] args){ + Application application = new Application(); + application.storePetInformation(); + application.petNameAndSpeak(application.pets); } -} +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Pet.java b/src/main/java/io/zipcoder/Pet.java index b197d9b..1abf3d3 100644 --- a/src/main/java/io/zipcoder/Pet.java +++ b/src/main/java/io/zipcoder/Pet.java @@ -1,13 +1,14 @@ package io.zipcoder; -public abstract class Pet { +import java.util.Comparator; + +public class Pet implements Comparable, Comparator{ private String name; - public Pet (String name) { + public Pet(String name) { this.name = name; } - public abstract String speak(); public String getName() { return name; @@ -17,4 +18,15 @@ public void setName(String name) { this.name = name; } + public String speak() { + return ""; + } + + public int compareTo(Pet o) { + return 0; + } + + public int compare(Pet o1, Pet o2) { + return 0; + } } diff --git a/src/test/java/io/zipcoder/ApplicationTest.java b/src/test/java/io/zipcoder/ApplicationTest.java index 2e5865f..25c4cb5 100644 --- a/src/test/java/io/zipcoder/ApplicationTest.java +++ b/src/test/java/io/zipcoder/ApplicationTest.java @@ -9,15 +9,18 @@ public class ApplicationTest { @Test - public void petsWithNamesAndSounds() { +public void petNameAndSpeakTest() { + Application app = new Application(); - Bird Bird = new Bird("Ugly Bird"); - Cat cat = new Cat("Fitzgerald"); + Bird bird = new Bird("Ugly Bird"); + Cat cat = new Cat ("Fitzgerald"); Dog dog = new Dog("Oreo"); + app.pets.add(bird); + app.pets.add(cat); + app.pets.add(dog); - String expected = "Oreo says, Woof\nUgly Bird says, Squak\nFitzgerald says, Meow\n"; - String actual = app.petInformation(); - Assert.assertEquals(expected, actual); + String expected = "Ugly Bird Squak\nFitzgerald Meow\nOreo Bark"; + String actual = app.petNameAndSpeak(app.pets); } }