家谱,无法解决孩子

时间:2018-12-06 14:37:51

标签: java

我对这项任务一无所知。我需要像这样

enter image description here

但是我无法在Main.java中解决孩子问题。对于每个名字,我都必须给孩子写空值。

这是我的代码:Main.java。 第二个文件:Osoba.java

public class Main {

    public static void main(String[] args) {

        Osoba abraham = new Osoba("Abraham Simpson", null, null, null, null, null, null, null);
        Osoba penelope = new Osoba("Penelope Olsen", null, null, null, null, null, null,null);
        Osoba pan = new Osoba("Pan Bouvier", null, null, null, null, null,null,null);
        Osoba jackie = new Osoba("Jackie Bouvier", null, null, null, null, null,null,null);
        Osoba herb = new Osoba("Herb Powers", abraham, penelope, null, null, null,null,null);
        Osoba homer = new Osoba("Homer Simpson", abraham, penelope, null, null, null,null,null);
        Osoba marge = new Osoba("Marge Simpson", pan, jackie, null, null, null,null,null);
        Osoba selma = new Osoba ("Selma Bouvier", pan, jackie, null, null, null,null,null);
        Osoba bart = new Osoba("Bart Simpson", homer, abraham, penelope,marge, pan,jackie,null);

        bart.sestavRodokmen();
        System.out.println();
        homer.sestavRodokmen();
    }
}



public class Osoba {

    private String name;
    private Osoba father;
    private Osoba dad_grandpa;
    private Osoba dad_grandma;
    private Osoba mother;
    private Osoba mom_grandpa;
    private Osoba mom_grandma;
    private Osoba child;

    public Osoba (String name, Osoba father, Osoba dad_grandpa, Osoba dad_grandma, Osoba mother, Osoba mom_grandpa, Osoba mom_grandma, Osoba child) {
        this.name = name;
        this.father = father;
        this.dad_grandpa = dad_grandpa;
        this.dad_grandma = dad_grandma;
        this.mother = mother;
        this.mom_grandpa = mom_grandpa;
        this.mom_grandma = mom_grandma;
        this.child = child;
    }

    private String rodokmen;

    public void sestavRodokmen () {

        System.out.println("Rodokmen pro osobu: " + name);

        System.out.println("Name: " + name);
        System.out.println("Father: "+ father);
        System.out.println("Grandfather: "+ dad_grandpa);
        System.out.println("Grandmother: "+ dad_grandma);
        System.out.println("Mother: "+ mother);
        System.out.println("Grandfather: "+ mom_grandpa);
        System.out.println("Grandmother: "+ mom_grandma);
        System.out.println("Child: "+ child);

        rodokmen = "";
        System.out.println(rodokmen);
    }

    @Override
    public String toString () {
        String a = "";
        if(mother != null && father != null)
            a += name + "\n" + father + "\n" + mother +"\n" +"\n" + child;
        else
            a += name;

        return a;
    }
}

3 个答案:

答案 0 :(得分:2)

在Osaba,只需要直接父母和/或直接子女。

在创建新的Osaba时,仍然不能定义父母或孩子。

创作的完全自由度是:

    Osoba abraham = new Osoba("Abraham Simpson");
    Osoba penelope = new Osoba("Penelope Olsen");
    Osoba pan = new Osoba("Pan Bouvier");
    Osoba jackie = new Osoba("Jackie Bouvier");
    Osoba herb = new Osoba("Herb Powers");
    Osoba homer = new Osoba("Homer Simpson");
    Osoba marge = new Osoba("Marge Simpson");
    Osoba selma = new Osoba ("Selma Bouvier");
    Osoba bart = new Osoba("Bart Simpson");

然后使用类似以下的命令创建祖先图:

    herb.setParents(abraham, penelope);
    martha.setParents(null, penelope); // If no father.

现在,如果您想检查所有的Osaba:是否有人没有父母,某人不是他自己的祖先等等,也许您需要一个容器类:

public class Osabas {
    private List<Osaba> persons = new ArrayList<>();

    public Osaba createOsaba(String name) {
        Osaba osaba = new Osaba(name);
        persons.add(osaba);
        return osaba;
    }

    public void drawAncestryDiagram() {
        ...
    }
}

祝你好运!

答案 1 :(得分:0)

  1. 我建议不要照顾grand-parents,因为您会被父母的父母发现

    public Osoba(String name, Osoba father, Osoba mother, Osoba child) {
        this.name = name;
        this.father = father;
        this.mother = mother;
        this.child = child;
    }
    
  2. toString()应该看起来更像是跟随者,只是名字,而不是完整的信息,否则,如果您像Bart那样打印,您将面临整个家庭的负担:

    @Override
    public String toString() {
        return String.join(" / ", name,
                father != null ? father.name : null,
                mother != null ? mother.name : null,
                child != null ? child.name : null);
    }
    
  3. 要在控制台中打印树,可以为每个新级别添加一个选项卡,依次打印人,其父亲和母亲,然后使用递归性,因为您不知道其大小家庭

    void sestavRodokmen(int level) {
        System.out.println(toString());
        printParentTab(level, father);
        printParentTab(level, mother);
    }
    
    void printParentTab(int level, Osoba parent) {
        if (parent != null) {
            for (int i = 0; i <= level; i++) {
                System.out.print("\t");
            }
            parent.sestavRodokmen(level + 1);
        }
    }
    
  4. 请参见Code Demo for result

    Bart Simpson / Homer Simpson / Marge Simpson / null
        Homer Simpson / Abraham Simpson / Penelope Olsen / null
            Abraham Simpson / null / null / null
            Penelope Olsen / null / null / null
        Marge Simpson / Pan Bouvier / Jackie Bouvier / null
            Pan Bouvier / null / null / null
            Jackie Bouvier / null / null / null
    

答案 2 :(得分:0)

这就是我要解决的问题:

首先,我们需要一个类来代表一个人。

您提供的图表似乎跟踪了4个属性:

  • 名称
  • 性别
  • 联系(父亲和母亲)

这反映在Person类中。

static class Person{
    private Person father;
    private Person mother;
    private List<Person> children = new ArrayList<>();
    private String firstname;
    private String surname;
    private boolean isMale;
    public Person(String firstname, String surname, boolean isMale){
        this.firstname = firstname;
        this.surname = surname;
        this.isMale = isMale;
    }
    public Person addChild(Person child){
        this.children.add(child);
        if(this.isMale){
            child.father = this;
        }else{
            child.mother = this;
        }
        return this;
    }

    public void printTreeUp(){
        printTreeUp(0);
    }
    private void printTreeUp(int depth){
        String tabs = "";
        if(depth != 0){
            tabs = "";
            for(int i=0;i<depth;i++)
                tabs += "\t";
            tabs += "∟ ";
        }
        System.out.println(tabs + firstname + " " + surname + " " + (isMale ? "♂" : "♀"));
        if(father != null)
            father.printTreeUp(depth+1);
        if(mother != null)
            mother.printTreeUp(depth+1);
    }
    public void printTreeDown(){
        printTreeDown(0);
    }
    private void printTreeDown(int depth){
        String tabs = "";
        if(depth != 0){
            tabs = "∟";
            for(int i=1;i<depth;i++)
                tabs += "_";
        }
        System.out.println(tabs + firstname + " " + surname + " " + (isMale ? "♂" : "♀"));
        for(Person child : children)
            child.printTreeDown(depth+1);
    }
}

如您所见,我还添加了一些打印家族树的方法。 特别是,printTreeUp(从给定的Person开始)将打印其父母,祖父母等。 还有printTreeDown(从给定的Person开始)将打印其子代,孙子等。

主要方法(用于构造示例族谱)如下:

public static void main (String[] args) throws java.lang.Exception
{
    Person gp0 = new Person("Abraham","Simpson", true);
    Person gp1 = new Person("Penelope","Olsen", false);

    Person gp2 = new Person("Pan","Bouvier", true);
    Person gp3 = new Person("Jackie","Bouvier", false);

    Person p0 = new Person("Herb","Powers", true);
    gp0.addChild(p0);
    gp1.addChild(p0);

    Person p1 = new Person("Homer","Simpson", true);
    gp0.addChild(p1);
    gp1.addChild(p1);

    Person p2 = new Person("Marge","Bouvier", false);
    gp2.addChild(p2);
    gp3.addChild(p2);

    Person p3 = new Person("Selma","Bouvier", true);
    gp2.addChild(p3);
    gp3.addChild(p3);

    Person c0 = new Person("Bart", "Simpson", true);
    p1.addChild(c0);
    p2.addChild(c0);

    // your code goes here
    c0.printTreeUp();
}

它输出Bart Simpson的家族树,看起来像这样:

 Bart Simpson ♂  
    ∟ Homer Simpson ♂  
        ∟ Abraham Simpson ♂  
        ∟ Penelope Olsen ♀  
    ∟ Marge Bouvier ♀  
        ∟ Pan Bouvier ♂  
        ∟ Jackie Bouvier ♀