Created
August 19, 2013 13:40
-
-
Save Fonsan/6269237 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package relation; | |
/** | |
* | |
* @author fonsan | |
*/ | |
public class Person { | |
private String name; | |
private int age; | |
public Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public void printInfo() { | |
System.out.println(name); | |
System.out.println(age); | |
} | |
} |
This file contains 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package relation; | |
/** | |
* | |
* @author fonsan | |
*/ | |
public class Relation { | |
private Person person1; | |
private Person person2; | |
private String rel; | |
private static String [] VALID_RELATIONS = {"father", "mother", "sister", "brother", "cousin", "unknown"}; | |
public Relation(Person person1, Person person2, String rel) { | |
this.person1 = person1; | |
this.person2 = person2; | |
setRel(rel); | |
} | |
public Person getPerson1() { | |
return person1; | |
} | |
public void setPerson1(Person person1) { | |
this.person1 = person1; | |
} | |
public Person getPerson2() { | |
return person2; | |
} | |
public void setPerson2(Person person2) { | |
this.person2 = person2; | |
} | |
public String getRel() { | |
return rel; | |
} | |
public void setRel(String rel) { | |
boolean foundRelation = false; | |
for (int i = 0; i < VALID_RELATIONS.length; i++) { | |
String relation = VALID_RELATIONS[i]; | |
if (rel.equals(relation)) { | |
foundRelation = true; | |
break; | |
} | |
} | |
if (!foundRelation) { | |
rel = "unknown"; | |
} | |
this.rel = rel; | |
} | |
public void printInfo() { | |
person1.printInfo(); | |
person2.printInfo(); | |
System.out.println("Relation: " + rel); | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
Person p1 = new Person("Erik", 22); | |
Person p2 = new Person("Ylva", 22); | |
Relation r = new Relation(p1, p2, "sister"); | |
r.printInfo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment