目前,我正在尝试用 Java 对家谱应用程序的一些值进行硬编码。
只是需要一些关于如何去做的想法,不知道如何进行。
我还包括了构造函数类:
public class TreeImpl {
private FamilyMember root;
public FamilyMember getRoot() {
return root;
}
public void setRoot(FamilyMember root) {
this.root = root;
}
public List<FamilyMember> getAllMembers() {
return allMembers;
}
public void setAllMembers(List<FamilyMember> allMembers) {
this.allMembers = allMembers;
this.setRoot(this.allMembers.get(0));
}
private List<FamilyMember> allMembers = new ArrayList<>();
public TreeImpl(FamilyMember root)
{
this.root=root;
addMembers(root);
}
private void addMembers(FamilyMember node)
{
if(node==null) return;
allMembers.add(node);
addMembers(node.getFather());
addMembers(node.getMother());
addMembers(node.getSpouse());
for(FamilyMember child : node.getChildren())
addMembers(child);
}
public void addSpouse(int index, FamilyMember spouse)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setSpouse(spouse);
allMembers.add(spouse);
return;
}
}
}
public void addFather(int index, FamilyMember father)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setFather(father);
allMembers.add(father);
return;
}
}
}
public void addMother(int index, FamilyMember mother)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setMother(mother);
allMembers.add(mother);
return;
}
}
}
public void addChild(int index, FamilyMember child)
{
for(FamilyMember member : allMembers)
{
if(member.getMemberID()==index)
{
member.setSpouse(child);
allMembers.add(child);
return;
}
}
}
public FamilyMember getDetailsForMember(String member)
{
for(FamilyMember m : this.getAllMembers())
{
if(m.getFirstName().equals(member))
return m;
}
return null;
}
我的构造函数类
public class FamilyMember implements Serializable{
private static int id=1;
private String firstName, surName, surNameAfterMarriage, life;
private Gender gender;
private Address address;
private FamilyMember mother, father, spouse;
private int memberID;
public boolean hasSub()
{
return (this.getFather()!=null || this.getMother()!=null || this.getChildren().size()>0);
}
public int getMemberID() {
return memberID;
}
public void setMemberID(int memberID) {
this.memberID = memberID;
}
private List<FamilyMember> children, grandChildren;
public static int getId() {
return id;
}
public static void incrementId()
{
id++;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getSurNameAfterMarriage() {
return surNameAfterMarriage;
}
public void setSurNameAfterMarriage(String surNameAfterMarriage) {
this.surNameAfterMarriage = surNameAfterMarriage;
}
public String getLife() {
return life;
}
public void setLife(String life) {
this.life = life;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public FamilyMember getMother() {
return mother;
}
public void setMother(FamilyMember mother) {
this.mother = mother;
}
public FamilyMember getFather() {
return father;
}
public void setFather(FamilyMember father) {
this.father = father;
}
public FamilyMember(String firstName, String surName, String surNameAfterMarriage, String life, Gender gender,
Address address) {
super();
this.firstName = firstName;
this.surName = surName;
this.surNameAfterMarriage = surNameAfterMarriage;
this.life = life;
this.gender = gender;
this.address = address;
this.memberID = this.getId();
this.children = new ArrayList<>();
this.incrementId();
}
for(FamilyMember child : this.getChildren())
{
text = text + " " + child.firstName;
}
return text;
}
public FamilyMember getSpouse() {
return spouse;
}
public void setSpouse(FamilyMember spouse) {
this.spouse = spouse;
}
public List<FamilyMember> getChildren() {
return children;
}
public void setChildren(List<FamilyMember> childer) {
this.children = childer;
}
public void addChild(FamilyMember child) {
this.children.add(child);
}
public List<FamilyMember> getGrandChildren() {
return grandChildren;
}
public void setGrandChildren(List<FamilyMember> grandChildren) {
this.grandChildren = grandChildren;
}
}
例如,我想为父亲、母亲、孩子等添加详细信息,所以当我运行程序时,会显示这些值
例如:Familymember father = new Familymember ("xyz". "xyx" )