'Relation between mother an child [closed]

I have 2 classes : Mother and Child + ofcourse an Main. Class Mother:

public class Mother extends Child {

    private int id;
    private String name;
    private int age;

    public Mother(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    @Override
    public String toString() {
        return "Mother{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Class Child:

public class Child {
    private String name;
    private int age;

    public Child(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Child() {

    }

    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;
    }

    @Override
    public String toString() {
        return "Child{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

I have to make a relation between the classes that a mother can have many childs but a child can have only one mother .

So in main I read(the file.txt witch contains the name, age and id for mother and name and age fro child)and parse it Main:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main5 {
    public static void main(String[] args) throws IOException {
        List<Mother> mothers = mother();
        System.out.println(mothers);

        List<Child> child = child();
        System.out.println(child);
    }

    public static List<Mother> mother() throws IOException {
        List<Mother> mothers = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("mom.txt"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] s = line.split("\\s+");
            mothers.add(new Mother(Integer.parseInt(s[0]), s[1], Integer.parseInt(s[2])));
        }
        bufferedReader.close();
        return mothers;
    }
    public static List<Child> child() throws IOException {
        List<Child> childs = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("child.txt"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] s = line.split("\\s+");
            childs.add(new Child(s[0], Integer.parseInt(s[1])));
        }
        bufferedReader.close();
        return childs;
    }
}

Is there somoane that can give me an example of how I should to it or where I should start?



Solution 1:[1]

You can maintain a list of child on mother. By doing this a mother object will have many child and maintain a mother class ref. in child. will allow you to have a child only one mother.

Example:

class Mother{

    private List<Child> childList = new ArrayList<>();

    ....................................................
    ....................................................
}
class Child{

    private Mother mother;

    ....................................................
    ....................................................
}

BTW you should post what you have tried so far. Try reading about java collection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amimul Ehsan Rahi