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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>Interfaces</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/io/zipcoder/Application.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package io.zipcoder;


import java.util.Scanner;

public class Application {

Scanner userInput = new Scanner(System.in);
int numberOfPets;
String petType;
String petName;

public static void main(String[] args) {
Application app = new Application();
app.setNumberOfPets();
app.buildPetInventory();
}

public void setNumberOfPets() {

System.out.println("Hello. How many cool pets do you have?\n(Cool pets are defined as dogs, cats or otters)");
numberOfPets = userInput.nextInt();
}


public void buildPetInventory() {

PetInventory newInventory = new PetInventory();
System.out.println("Tell me one pet at a time...");

while (numberOfPets > 0) {
System.out.println("What is your pet type?");
petType = userInput.next();

System.out.println("And its name?");
petName = userInput.next();

newInventory.addPet(petType, petName);

numberOfPets--;

if (numberOfPets > 0) {
System.out.println("Thanks. Next...");
}
}

System.out.println("Okay, that's it! These are your cool, loud pets:");
System.out.println(newInventory.listPetsAndSpeak());
}

public String getPetType() {
return petType;
}

public int getNumberOfPets() {
return numberOfPets;
}

public String getPetName() {
return petName;
}
}
144 changes: 144 additions & 0 deletions src/main/java/io/zipcoder/PetInventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package io.zipcoder;

import io.zipcoder.pets.*;

import java.lang.reflect.Array;
import java.util.*;

public class PetInventory {

HashMap<String, ArrayList<Pet>> pets;

public PetInventory() {
this.pets = new HashMap<String, ArrayList<Pet>>();
}


public void addPet(String petType, String petName) {
int petTypeInt;

if (petType.equalsIgnoreCase("cat")) {
petTypeInt = 1;
} else if (petType.equalsIgnoreCase("dog")) {
petTypeInt = 2;
} else {
petTypeInt = 3;
}

switch (petTypeInt) {
case 1:
Cat newCat = new Cat(petName);
if (pets.containsKey(petType)) {
pets.get(petType).add(newCat);
} else {
ArrayList<Pet> newCatList = new ArrayList<Pet>();
newCatList.add(newCat);
pets.put(petType, newCatList);
}
break;
case 2:
Dog newDog = new Dog(petName);
if (pets.containsKey(petType)) {
pets.get(petType).add(newDog);
} else {
ArrayList<Pet> newDogList = new ArrayList<Pet>();
newDogList.add(newDog);
pets.put(petType, newDogList);
}
break;
case 3:
Otter newOtter = new Otter(petName);
if (pets.containsKey(petType)) {
pets.get(petType).add(newOtter);
} else {
ArrayList<Pet> newOtterList = new ArrayList<Pet>();
newOtterList.add(newOtter);
pets.put(petType, newOtterList);
}
break;
}

}

public void removePet(String petName) {

for (Map.Entry<String, ArrayList<Pet>> entry : pets.entrySet()) {
for (int i = 0; i < pets.entrySet().size(); i++) {
if (entry.getValue().get(i).getName().equalsIgnoreCase(petName)) {
entry.getValue().remove(i);
}
}
}
}


public String getPetType(String petName) {

for (String key : pets.keySet()) {
for (int i = 0; i < getPetsByType(key).size(); i++) {
if (getPetsByType(key).get(i).getName().equalsIgnoreCase(petName)) {
return key;
}
}
} return null;
}

public String listPetsAndSpeak() {

String list = "";
Set<String> keys = pets.keySet();
for (String i : keys) {
for (int j = 0; j < pets.get(i).size(); j++)
list += pets.get(i).get(j).getName() + "==>" + pets.get(i).get(j).speak() + "\n";
}
return list;

}

public ArrayList<Pet> getPetsByType(String petType) {
return pets.get(petType);
}

public ArrayList<Pet> getAllPets() {

ArrayList<Pet> allPets = new ArrayList<Pet>();

for (int i = 0; i < getPetsByType("cat").size(); i++) {
allPets.add(getPetsByType("cat").get(i));
}
for (int i = 0; i < getPetsByType("dog").size(); i++) {
allPets.add(getPetsByType("dog").get(i));
}
for (int i = 0; i < getPetsByType("otter").size(); i++) {
allPets.add(getPetsByType("otter").get(i));
}

return allPets;
}

public ArrayList<Pet> sortedPetsByName() {

ArrayList<Pet> allPets = getAllPets();

Collections.sort(allPets);

return allPets;
}

public ArrayList<Pet> sortedPetsByType() {

Compare comp = new Compare();

ArrayList<Pet> allPets = getAllPets();

Collections.sort(allPets, comp);

return allPets;

}





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

public class Cat extends Pet {


public Cat(String catName) {
super(catName);
}


public String speak() {

return "Meow!";
}



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

import java.util.Comparator;

public class Compare implements Comparator<Pet> {

@Override
public int compare(Pet thisPet, Pet anotherPet) {

String thisPetClass = thisPet.getClass().getSimpleName();
String otherPetClass = anotherPet.getClass().getSimpleName();
int result = -(otherPetClass.compareToIgnoreCase(thisPetClass));
boolean sameClass = (result ==0);

if (sameClass) {
String thisPetName = thisPet.getName();
String otherPetName = anotherPet.getName();
result = -(otherPetName.compareToIgnoreCase(thisPetName));
}

return result;
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/zipcoder/pets/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.zipcoder.pets;

public class Dog extends Pet {


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


public String speak() {

return "Woof!";

}


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

public class Otter extends Pet {


public Otter(String otterName) {
super(otterName);
}


public String speak() {

return "Muahaha";
}


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

abstract public class Pet implements PetBehavior, Comparable<Pet> {

private String name;

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

public String speak() {
return null;
}

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

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

@Override
public int compareTo(Pet anotherPet) {
String otherPetName = anotherPet.getName();
int result = -(otherPetName.compareTo(name));
boolean sameName = (result == 0);
if (sameName) {
Class otherPetClass = anotherPet.getClass();
String otherPetClassName = otherPetClass.getSimpleName();

Class thisPetClass = this.getClass();
String thisPetClassName = thisPetClass.getSimpleName();

result = -(otherPetClassName.compareTo(thisPetClassName));
}
return result;
}





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

public interface PetBehavior {
public String speak();
}
Loading