Skip to content
Open

Dev2 #58

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
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
<groupId>io.zipcoder</groupId>
<artifactId>polymorphism</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>


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

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Console {

private final Scanner input;
private final PrintStream output;

public Console(InputStream in, PrintStream out){

this.input = new Scanner(in);
this.output = out;
}

public void print(String val, Object... args) {
output.format(val, args);
}

public void println(String val, Object... vals) {
print(val + "\n", vals);
}

public String getStringInput(String prompt, Object... args) {
println(prompt, args);
return input.nextLine();
}

public Integer getIntegerInput(String prompt, Object... args) {

String stringInput = getStringInput(prompt, args);
try {
Integer intInput = Integer.valueOf(stringInput);
return intInput;

} catch (NumberFormatException nfe) { // TODO - Eliminate recursive nature
println("[ %s ] is an invalid user input!", stringInput);
println("Try inputting an integer value!");
}

return getIntegerInput(prompt, args);
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
package io.zipcoder.polymorphism;

import io.zipcoder.polymorphism.Pets.*;

/**
* Created by leon on 11/6/17.
*/
public class MainApplication {

Console console = new Console(System.in, System.out);
Pets[] setOfPets;

public void run() {

printInfo(getTypes(getNumberOfPets()));

}

public Integer getNumberOfPets(){

int numberOfPets = console.getIntegerInput("How many pets do you have?");

return numberOfPets;
}


public Pets[] getTypes(int numberOfPets){

setOfPets = new Pets[numberOfPets];

for (int index = 0; index < numberOfPets; index++) {
String type = console.getStringInput("\nWhat type of pet is it?\n" +
" Options: Dog, Cat, Cow");
switch(type.toLowerCase()){

case "dog": setOfPets[index] = new Dog(getName()); break;
case "cat": setOfPets[index] = new Cat(getName()); break;
case "cow": setOfPets[index] = new Cow(getName()); break;
default: console.println("\nNot a valid animal."); index -= 1; break;
}
}

return setOfPets;
}


public String getName() {

return console.getStringInput("\nWhat is the pet's name?");
}


public void printInfo(Pets[] pets){

for (Pets pet: pets) {
console.println(pet.toString());
}
}


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

public class Cat extends Pets {

public Cat(String name) {

super(name);
}

public Cat(){
super("Fluffy");
}

@Override
public String type() {
return "Cat";
}

@Override
public String speak() {
return "meow!";
}
}
22 changes: 22 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pets/Cow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.zipcoder.polymorphism.Pets;

public class Cow extends Pets {

public Cow(String name) {
super(name);
}

public Cow(){
super("Mimi");
}

@Override
public String type() {
return "Cow";
}

@Override
public String speak() {
return "moo!";
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pets/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.zipcoder.polymorphism.Pets;

public class Dog extends Pets {

public Dog(String name) {
super(name);
}

public Dog(){
super("Dingo");
}

public String type(){
return "Dog";
}

@Override
public String speak() {
return "woof!";
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/zipcoder/polymorphism/Pets/Pets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.zipcoder.polymorphism.Pets;

public abstract class Pets {

private String name;
public abstract String type();
public abstract String speak();

public Pets(String name) {
this.name = name;
}

public String getName() {
return name;
}

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

public String toString() {
String[] className;
className = this.getClass().getName().split("\\.");

return String.format("\n%s is a %s and sounds like %s",
this.getName(), className[className.length - 1], this.speak());
}

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

public class main {

public static void main(String[] args) {

MainApplication newMain = new MainApplication();

newMain.run();

}
}
41 changes: 41 additions & 0 deletions src/test/java/io/zipcoder/polymorphism/MainApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
package io.zipcoder.polymorphism;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by leon on 11/6/17.
*/
public class MainApplicationTest {
MainApplication testApp = new MainApplication();

@Test
public void addPet() {

testApp.getTypes(2);
int expected = 2;
int actual = testApp.setOfPets.length;

Assert.assertEquals(expected,actual);
}

@Test
public void printPets() {
// testApp.addPet("Cat");
// testApp.addPet("Dog");
//
// String expected = "No name cat";
// String actual = testApp.petList.get(0).getName();
// Assert.assertEquals(expected,actual);
//
// expected = "No name dog";
// actual = testApp.petList.get(1).getName();
// Assert.assertEquals(expected,actual);
}

// ****************************

@Test
public void getNumberOfPets() {
}

@Test
public void getTypes() {
}

@Test
public void getName() {
}
}