package Person; import java.util.Collection; import java.util.Iterator; import java.util.ArrayList; /* * @author GEORGII ZALIZKO 073896 */ public class Person implements Iterable<Person> { public String name; public Collection<Person> childs = new ArrayList<Person>(); private int found = 0; private Collection<Person> foundHeirs = new ArrayList<Person>(); public Person(String s) { name = s; } public Iterator<Person> iterator() { return childs.iterator(); } public boolean addChild (Person child) { return (childs.add(child)); } public int search(String name) { found = 0; recursion_search(this, name); return found; } void recursion_search(Person obj, String s) { Iterator<Person> iter = obj.childs.iterator(); while(iter.hasNext()) { Person person = (Person) iter.next(); recursion_search(person, s); if(person.name.equalsIgnoreCase(s)) found++; } } public Collection<Person> searchPersons(String name) { foundHeirs.removeAll(null); recursion_searchPersons(this, name); return foundHeirs; } void recursion_searchPersons(Person obj, String s) { Iterator<Person> iter = obj.childs.iterator(); while(iter.hasNext()) { Person person = (Person) iter.next(); recursion_searchPersons(person, s); if(person.name.equalsIgnoreCase(s)) foundHeirs.add(person); } } public void print() { Iterator<Person> iter = this.childs.iterator(); if(iter.hasNext()) { System.out.print("Person '" + this.name + "' have " + this.childs.size() + " children: "); while(iter.hasNext()) { Person person = (Person) iter.next(); System.out.print(person.name); if(iter.hasNext()) System.out.print(", "); } System.out.println("."); } } public boolean isGrandParent(Person obj) { for(Person p: obj) { if(p.equals(this)) return true; } return false; } public boolean isAncestor(Person obj) { for(Person p: this.childs) { if(p.equals(obj)) return true; } return false; } }
и второй. для тестирования задания:
package testPackage; import java.util.Iterator; import Person.*; /* * @author GEORGII ZALIZKO 073896 */ public class testimine { public static void main(String[] args) { Person George = new Person("George"); Person Alex = new Person("Alex"); Person David = new Person("David"); Person Marek = new Person("Marek"); Person Allan = new Person("Allan"); Person Steve = new Person("Steve"); Person Brad = new Person("Brad"); Person Brad2 = new Person("Brad"); George.addChild(Alex); George.addChild(David); George.addChild(Marek); Alex.addChild(Allan); Marek.addChild(Steve); Marek.addChild(Brad); Steve.addChild(Brad2); for (Person p: Marek) { System.out.println(p.name + "\t (" + p.toString() + ") has " + p.childs.size() + " children."); } System.out.println(); /* * Checking function isGrandParent() */ System.out.println("Marek is GrandParent of Brad? -- " + Brad.isGrandParent(Marek)); System.out.println("David is GrandParent of Brad? -- " + Brad.isGrandParent(David)); System.out.println(); /* * Checking function isAncestor() */ System.out.println("Marek is the sun of Brad? -- " + Brad.isAncestor(Marek)); System.out.println("Allan is the sun of Alex? -- " + Alex.isAncestor(Allan)); System.out.println(); /* * Checking function search() */ System.out.println("George have " + George.search("Brad") + " heirs with name 'Brad'."); System.out.println(); /* * Checking function searchPersons() */ System.out.println(Marek.searchPersons("Brad")); /* * Checking function print() */ System.out.println(); Marek.print(); System.out.println(); Iterator<Person> i1 = George.iterator(); System.out.println(i1.next()); System.out.println(i1.next()); Iterator<Person> i2 = George.iterator(); System.out.println(i2.next()); } }