Created
June 30, 2015 17:38
-
-
Save calindotgabriel/a34b03376584c764c444 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package domain; | |
import domain.model.Dog; | |
import tree.BinaryTree; | |
/** | |
* Created by motan on 30.06.2015. | |
*/ | |
public class Controller { | |
private BinaryTree<Dog> dogs; | |
public Controller() { | |
dogs = new BinaryTree<Dog>(); | |
} | |
public void saveAncestor(String name, String breed) { | |
Dog ancestor = new Dog(name, breed); | |
dogs.createLeaf(ancestor); | |
} | |
public void saveChild(String parentName, String childName, String childBreed) { | |
BinaryTree parent = findByName(parentName); | |
BinaryTree<Dog> childTree = new BinaryTree<Dog>(new Dog(childName, childBreed)); | |
boolean canAdd = false; | |
if (parent.left() == null) { | |
canAdd = true; | |
parent.addSubLeft(childTree); | |
} | |
else if (parent.right() == null) { | |
canAdd = true; | |
parent.addSubRight(childTree); | |
} | |
if (!canAdd) { | |
throw new ChildrenOccupiedException(); | |
} | |
} | |
public BinaryTree findByName(String name) { | |
for (BinaryTree<Dog> dogTree: dogs) { | |
Dog dog = dogTree.element(); | |
if (dog.getName().equals(name)) { | |
return dogTree; | |
} | |
} | |
return null; | |
} | |
public boolean exists(String name) { | |
return findByName(name) != null; | |
} | |
public void listSpecies() { | |
for (BinaryTree<Dog> dogTree: dogs) { | |
Dog dog = dogTree.element(); | |
System.out.println(dog); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment