Skip to content

console.log("Hello world!"); #55

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Bird.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.polymorphism;

public class Bird extends Pet {
public Bird(String name) {
super(name, "bird");
}
public String speak() {
String sound = "chirp!";
// Console.println(sound);
return sound;
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.polymorphism;

public class Cat extends Pet {
public Cat(String name) {
super(name, "cat");
}
public String speak() {
String sound = "meow!";
// Console.println(sound);
return sound;
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Console.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.zipcoder.polymorphism;

import java.util.Scanner;

public class Console {
public static void print(String output, Object... args) {
System.out.printf(output, args);
}

public static void println(String output, Object... args) {
print(output + "\n",args);
}

public static String getStringInput(String prompt) {
Scanner in = new Scanner(System.in);
print(prompt);
String userInput = in.nextLine();
return userInput;
}

public static Integer getIntegerInput(String prompt) {
Scanner scanner = new Scanner(System.in);
String userInput = "";
Boolean check = false;
do {
print(prompt);
userInput = scanner.nextLine();
check = userInput.matches("^\\d{0,9}$");
if (!check) println("Error: please enter a valid number");
} while (!check);

return Integer.valueOf(userInput);
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.polymorphism;

public class Dog extends Pet {
public Dog(String name) {
super(name, "dog");
}
public String speak() {
String sound = "woof!";
// Console.println(sound);
return sound;
}
}
27 changes: 27 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@
* Created by leon on 11/6/17.
*/
public class MainApplication {
public static void main(String[] args) {
petApp();
}

public static void petApp() {
Integer numberOfPets = getNumberOfPets();
Console.println("Can you tell me some more about your %s pets?", numberOfPets);

String[] petTypes = new String[numberOfPets];
String[] petNames = new String[numberOfPets];
for (int i = 0; i < numberOfPets; i++) {
petNames[i] = Console.getStringInput(String.format("What is pet %s's name? ", i+1));
petTypes[i] = Console.getStringInput(String.format("What kind of pet is %s? ", petNames[i]));
}
PetWarehouse petWarehouse = new PetWarehouse(numberOfPets, petTypes, petNames);
Console.print(petWarehouse.displayPetInfo());
}

public static Integer getNumberOfPets() {
return Console.getIntegerInput("How many pets do you have? ");
}






}
27 changes: 27 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.zipcoder.polymorphism;

abstract public class Pet {
private String name;
private String type;

public Pet(String name, String type) {
this.name = name;
this.type = type;
}

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

public void setType(String type) {
this.type = type;
}
public String getType() {
return this.type;
}

abstract public String speak();
}
58 changes: 58 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/PetWarehouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.zipcoder.polymorphism;

public class PetWarehouse {
private Pet[] pets;

public PetWarehouse() {}

public PetWarehouse(Pet... pets) {
this.pets = pets;
}
public PetWarehouse(Integer numberOfPets, String[] petTypes, String[] petNames) {
this.pets = createPets(numberOfPets, petTypes, petNames);
}

public Pet[] createPets(Integer numberOfPets, String[] petTypes, String[] petNames) {
Pet[] pets = new Pet[numberOfPets];
for (int i = 0; i < numberOfPets; i++) {
pets[i] = createPetFromType(petTypes[i], petNames[i]);
}
return pets;
}

public Pet createPetFromType(String petType, String petName) {
Pet pet;
if (petType.equals("dog")) {
pet = new Dog(petName);
}
else if (petType.equals("cat")) {
pet = new Cat(petName);
}
else if (petType.equals("bird")) {
pet = new Bird(petName);
}
else if (petType.equals("snake")) {
pet = new Snake(petName);
}
else {
pet = new UnknownPet(petName, petType);
}
return pet;
}

public Pet[] getPets() {
return this.pets;
}

public String displayPetInfo() {
StringBuilder output = new StringBuilder();
Console.println("\n\n\n");
output.append(String.format("You have %s pets.", pets.length));

for (int i = 0; i < pets.length; i++) {
output.append(String.format("\nPet %s is a %s named %s.", i+1, pets[i].getType(), pets[i].getName()));
output.append("\n" + pets[i].speak());
}
return output.toString();
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Snake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.polymorphism;

public class Snake extends Pet {
public Snake(String name) {
super(name, "snake");
}
public String speak() {
String sound = "slither!";
// Console.println(sound);
return sound;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/UnknownPet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.zipcoder.polymorphism;

public class UnknownPet extends Pet {
public UnknownPet(String name, String type) {
super(name, type);
}

public String speak() {
String sound = String.format("(%s noises)!", getType());
// Console.println(sound);
return sound;
}
}
25 changes: 25 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/BirdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

public class BirdTest {
@Test
public void testSpeak() {
Bird bird = new Bird("Tucker");
String actual = bird.speak();
String expected = "chirp!";
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructor1() {
Bird bird = new Bird("Bort");
Assert.assertEquals("Bort", bird.getName());
}
@Test
public void testConstructor2() {
Bird bird = new Bird("Tucker");
Assert.assertEquals("bird", bird.getType());
}
}
25 changes: 25 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/CatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

public class CatTest {
@Test
public void testSpeak() {
Cat cat = new Cat("Charlie");
String actual = cat.speak();
String expected = "meow!";
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructor1() {
Cat cat = new Cat("Charlie");
Assert.assertEquals("Charlie", cat.getName());
}
@Test
public void testConstructor2() {
Cat cat = new Cat("Charlie");
Assert.assertEquals("cat", cat.getType());
}
}
25 changes: 25 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/DogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

public class DogTest {
@Test
public void testSpeak() {
Dog dog = new Dog("Tucker");
String actual = dog.speak();
String expected = "woof!";
Assert.assertEquals(expected, actual);
}

@Test
public void testConstructor1() {
Dog dog = new Dog("Tucker");
Assert.assertEquals("Tucker", dog.getName());
}
@Test
public void testConstructor2() {
Dog dog = new Dog("Tucker");
Assert.assertEquals("dog", dog.getType());
}
}
15 changes: 15 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
* Created by leon on 11/6/17.
*/
public class MainApplicationTest {
@Test
public void testGetNumberOfPets() {
String input = "4";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
Integer actual = MainApplication.getNumberOfPets();
Integer expected = 4;
Assert.assertEquals(expected, actual);
}
}
52 changes: 52 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/PetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.zipcoder.polymorphism;

import org.junit.Assert;
import org.junit.Test;

public class PetTest {
@Test
public void dogConstructorTest() {
Dog dog = new Dog("Tucker");
String expectedName = "Tucker";
String actualName = dog.getName();
String expectedType = "dog";
String actualType = dog.getType();
Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void catConstructorTest() {
Cat cat = new Cat("Tom");
String expectedName = "Tom";
String actualName = cat.getName();
String expectedType = "cat";
String actualType = cat.getType();
Assert.assertEquals(expectedName, actualName);
Assert.assertEquals(expectedType, actualType);
}

@Test
public void setNameTest() {
Dog dog = new Dog("Tucker");
dog.setName("Bill");
String expected = "Bill";
String actual = dog.getName();
Assert.assertEquals(expected, actual);
}

@Test
public void speakTest() {
Dog dog = new Dog("Tucker");
dog.speak();

}
@Test
public void testSetType() {
Dog dog = new Dog("Tucker");
dog.setType("orange");
String actual = dog.getType();
String expected = "orange";
Assert.assertEquals(expected, actual);
}
}
Loading