帮我删除特定关系
Person=Alex Relation=Brothers
预期输出应为
Brothers=John,Joe
Husband=Bern Wife=Julia
申请书应该加上朱莉娅作为伯尔尼的配偶。现在我想删除伯尔尼和妻子朱莉娅之间的关系,但我做不到
Person=Bern Relation=Wife
应将输出返回为:
妻子=朱莉娅
$run\u应用程序
输入:丈夫=伯尔尼妻子=朱莉娅
输出:欢迎来到这个家庭,朱莉娅!
输入:person=bern relation=wife
输出:妻子=朱莉娅
import java.util.*;
import java.util.stream.Collectors;
class Person {
private String name;
private Gender gender;
private List<Relation> relations = new ArrayList<>();
Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public List<Relation> getRelations() {
return relations;
}
public Gender getGender() {
return gender;
}
public void addRelation(Relation relation) {
relations.add(relation);
}
public void deleteRelation(Relation relation) {
relations.remove(relation);
}
}
class Relation {
private TreeRelationType type;
private Person person1;
private Person person2;
Relation(TreeRelationType type, Person person1, Person person2) {
this.type = type;
this.person1 = person1;
this.person2 = person2;
}
public TreeRelationType getType() {
return type;
}
public Person getPerson2() {
return person2;
}
}
enum TreeRelationType {
SPOUSE, PARENT, CHILD
}
enum Gender {
MALE, FEMALE
}
enum RelationType {
FATHER, MOTHER, BROTHER, SON,GRANDCHILDREN, DAUGHTER, AUNT, HUSBAND, WIFE
}
class InvalidInputException extends Exception {
static final long serialVersionUID = -3387516993334229948L;
public InvalidInputException(String message) {
super(message);
}
}
public class FamilyTree {
private Person root;
private Map<String, Boolean> visted = new HashMap<>();
private Gender fetchGender(RelationType type) {
if(RelationType.MOTHER.equals(type)||RelationType.DAUGHTER.equals(type)|| RelationType.WIFE.equals(type))
return Gender.FEMALE;
else
return Gender.MALE;
}
private TreeRelationType fetchTreeRelationType(RelationType type) {
if (RelationType.MOTHER.equals(type) || RelationType.FATHER.equals(type))
return TreeRelationType.CHILD;
else if (RelationType.HUSBAND.equals(type) || RelationType.WIFE.equals(type))
return TreeRelationType.SPOUSE;
else
return TreeRelationType.PARENT;
}
public void addPerson(String name1, RelationType type1, String name2, RelationType type2)
throws InvalidInputException {
TreeRelationType relationType1 = fetchTreeRelationType(type1);
TreeRelationType relationType2 = fetchTreeRelationType(type2);
Gender gender1 = fetchGender(type1);
Gender gender2 = fetchGender(type2);
if (this.root == null) {
Person person1 = new Person(name1, gender1);
Person person2 = new Person(name2, gender2);
this.root = person1;
addRelation(relationType1, person1, relationType2, person2);
} else {
Person person1 = findPerson(this.root, name1);
if (person1 == null) {
throw new InvalidInputException("Invalid Input");
}
Person person2 = new Person(name2, gender2);
addRelation(relationType1, person1, relationType2, person2);
if (TreeRelationType.CHILD.equals(relationType1)) {
for (Relation relation : person1.getRelations()) {
if (TreeRelationType.SPOUSE.equals(relation.getType())) {
person1 = relation.getPerson2();
break;
}
}
deleteRelation(relationType1, person1, relationType2, person2);
}
}
}
public void deleteperson(String name1, RelationType type1, String name2, RelationType type2)
throws InvalidInputException {
TreeRelationType relationType1 = fetchTreeRelationType(type1);
TreeRelationType relationType2 = fetchTreeRelationType(type2);
Gender gender1 = fetchGender(type1);
Gender gender2 = fetchGender(type2);
Person person1 = findPerson(this.root, name1);
if (person1 == null) {
throw new InvalidInputException("Invalid Input");
}
Person person2 = findPerson(this.root, name2);
if (person2 == null) {
throw new InvalidInputException("Invalid Input");
}
deleteRelation(relationType1, person1, relationType2, person2);
if (TreeRelationType.CHILD.equals(relationType1)) {
for (Relation relation : person1.getRelations()) {
if (TreeRelationType.SPOUSE.equals(relation.getType())) {
person1 = relation.getPerson2();
break;
}
}
addRelation(relationType1, person1, relationType2, person2);
}
}
private Person findPerson(Person cur, String name) {
this.visted.put(cur.getName(), Boolean.TRUE);
if (cur.getName().equals(name)) {
this.visted.clear();
return cur;
} else {
for (Relation relation : cur.getRelations()) {
Person person2 = relation.getPerson2();
if (!visted.containsKey(person2.getName())) {
Person person = findPerson(person2, name);
if (person != null) {
return person;
}
}
}
}
return null;
}
private void addRelation(TreeRelationType type1, Person person1, TreeRelationType type2, Person person2) {
Relation relation1 = new Relation(type1, person1, person2);
person1.addRelation(relation1);
Relation relation2 = new Relation(type2, person2, person1);
person2.addRelation(relation2);
}
private void deleteRelation(TreeRelationType type1, Person person1, TreeRelationType type2, Person person2) {
Relation relation1 = new Relation(type1, person1, person2);
person1.deleteRelation(relation1);
Relation relation2 = new Relation(type2, person2, person1);
person2.deleteRelation(relation2);
}
private List<Person> fetchChildren(String name) throws InvalidInputException {
List<Person> children = new ArrayList<>();
Person person = findPerson(this.root, name);
if (person == null) {
throw new InvalidInputException("Invalid Input");
}
for (Relation relation : person.getRelations()) {
if (TreeRelationType.CHILD.equals(relation.getType())) {
children.add(relation.getPerson2());
}
}
return children;
}
private List<Person> fetchParents(String name) throws InvalidInputException {
List<Person> parents = new ArrayList<>();
Person person = findPerson(this.root, name);
if (person == null) {
throw new InvalidInputException("Invalid Input");
}
for (Relation relation : person.getRelations()) {
if (TreeRelationType.PARENT.equals(relation.getType())) {
parents.add(relation.getPerson2());
}
}
return parents;
}
private Person fetchFather(String name) throws InvalidInputException {
Person father = null;
List<Person> parents = fetchParents(name);
for (Person person : parents) {
if (Gender.MALE.equals(person.getGender()))
father = person;
}
return father;
}
private Person fetchMother(String name) throws InvalidInputException {
Person mother = null;
List<Person> parents = fetchParents(name);
for (Person person : parents) {
if (Gender.FEMALE.equals(person.getGender()))
mother = person;
}
return mother;
}
private List<Person> fetchSiblings(String name) throws InvalidInputException {
List<Person> siblings = new ArrayList<>();
Person father = fetchFather(name);
if (father != null) {
List<Person> children = fetchChildren(father.getName());
for (Person person : children) {
if (!person.getName().equals(name)) {
siblings.add(person);
}
}
}
return siblings;
}
private List<Person> fetchBrothers(String name) throws InvalidInputException {
List<Person> brothers = new ArrayList<>();
List<Person> siblings = fetchSiblings(name);
for (Person person : siblings) {
if (Gender.MALE.equals(person.getGender())) {
brothers.add(person);
}
}
return brothers;
}
private List<Person> fetchGrandChildren(String name) throws InvalidInputException {
List<Person> children = fetchChildren(name);
List<Person> grandChildren = new ArrayList<>();
for (Person person : children) {
grandChildren.addAll(fetchChildren(person.getName()));
}
return grandChildren;
}
private List<Person> fetchAunts(String name) throws InvalidInputException {
List<Person> aunts = new ArrayList<>();
List<Person> parents = fetchParents(name);
for (Person person : parents) {
List<Person> siblings = fetchSiblings(person.getName());
for (Person sibling : siblings) {
if (Gender.FEMALE.equals(sibling.getGender())) {
aunts.add(sibling);
} else {
Optional<Person> spouce = Optional.ofNullable(fetchSpouce(sibling.getName()));
if (spouce.isPresent()) {
aunts.add(spouce.get());
}
}
}
}
return aunts;
}
private Person fetchSpouce(String name) throws InvalidInputException {
Person spouce = null;
Person person = findPerson(this.root, name);
if (person == null) {
throw new InvalidInputException("Invalid Input");
}
for (Relation relation : person.getRelations()) {
if (TreeRelationType.SPOUSE.equals(relation.getType())) {
spouce = relation.getPerson2();
break;
}
}
return spouce;
}
public static void main(String args[]) throws InvalidInputException {
FamilyTree tree = new FamilyTree();
tree.addPerson("Dilshada", RelationType.MOTHER, "Laraib", RelationType.SON);
tree.addPerson("Dilshada", RelationType.WIFE, "Mushtaq", RelationType.HUSBAND);
tree.addPerson("Dilshada", RelationType.MOTHER, "Laieba", RelationType.DAUGHTER);
tree.addPerson("Mushtaq", RelationType.FATHER, "Farheen", RelationType.DAUGHTER);
tree.addPerson("Mushtaq", RelationType.BROTHER, "Ashraf", RelationType.BROTHER);
tree.addPerson("Ashraf", RelationType.HUSBAND, "Kulsum", RelationType.WIFE);
tree.addPerson("Mushtaq", RelationType.SON, "Nani", RelationType.MOTHER);
Scanner sc = new Scanner(System.in);
int choice;
do{
System.out.println("1. Add Relation\n2. View Relation\n3. Delete Relation\n4. Exit\n");
System.out.println("Enter Your Choice");
choice = sc.nextInt();
switch (choice) {
case 1:
try {
System.out.println("Enter The {name1}={relationtype1} {name2}={relationtype2}");
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String inputString = sc.nextLine();
if (!inputString.isEmpty()) {
String[] input = inputString.split(" ");
String name1 = input[0].split("=")[1];
String name2 = input[1].split("=")[1];
RelationType type1 = RelationType.valueOf(input[0].split("=")[0].toUpperCase());
RelationType type2 = RelationType.valueOf(input[1].split("=")[0].toUpperCase());
tree.addPerson(name1, type1, name2, type2);
System.out.println("Welcome to the family, " + name2 + "!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case 2:
try {
System.out.println("Enter The {name} {relationtype}");
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String inputString = sc.nextLine();
System.out.println(inputString);
if (!inputString.isEmpty()) {
String[] input = inputString.split(" ");
String relation = input[1];
String name = input[0];
String value = relation.toUpperCase();
if (value.charAt(value.length() - 1) == 'S') {
value = value.substring(0, value.length() - 1);
}
RelationType relationType = RelationType.valueOf(value);
switch (relationType) {
case FATHER:
Optional<Person> father = Optional.ofNullable(tree.fetchFather(name));
if (father.isPresent()) {
System.out.println(relation + "=" + tree.fetchFather(name).getName());
}
break;
case MOTHER:
Optional<Person> mother = Optional.ofNullable(tree.fetchMother(name));
if (mother.isPresent()) {
System.out.println(relation + "=" + tree.fetchMother(name).getName());
}
break;
case BROTHER:
System.out.println(relation + "=" + tree.fetchBrothers(name).stream()
.map(Person::getName).collect(Collectors.joining(",")));
break;
case GRANDCHILDREN:
System.out.println(relation + "=" + tree.fetchGrandChildren(name).stream()
.map(Person::getName).collect(Collectors.joining(",")));
break;
case AUNT:
System.out.println(relation + "=" + tree.fetchAunts(name).stream().map(Person::getName)
.collect(Collectors.joining(",")));
break;
case WIFE:
Optional<Person> spouce = Optional.ofNullable(tree.fetchSpouce(name));
if (spouce.isPresent()) {
System.out.println(relation + "=" + tree.fetchSpouce(name).getName());
}
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case 3:
try {
System.out.println("Enter The {name1}={relationtype1} {name2}={relationtype2}");
sc.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String inputString = sc.nextLine();
if (!inputString.isEmpty()) {
String[] input = inputString.split(" ");
String name1 = input[0].split("=")[1];
String name2 = input[1].split("=")[1];
RelationType type1 = RelationType.valueOf(input[0].split("=")[0].toUpperCase());
RelationType type2 = RelationType.valueOf(input[1].split("=")[0].toUpperCase());
System.out.println(name1 +" "+name2+" "+type1+" "+type2);
tree.deleteperson(name1, type1, name2, type2);
System.out.println("Deleted from the family, " + name2 + "!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case 4:
System.out.println("Existing");
break;
}
}while(choice !=4);
}
}
帮助我更正删除关系的主要方法中的案例3
暂无答案!
目前还没有任何答案,快来回答吧!