逻辑问题 - Java

发布于 2024-10-12 06:07:38 字数 2365 浏览 3 评论 0原文

我有一个家谱应用程序,它允许您构建节点。我遇到了一个需要编辑成员出生日期的问题。出生日期只是一个格式为 dd-mm-yyyy 的字符串。当检查出生日期是否有效(即任何父母不能比孩子年轻)时,我的问题出现了。因此,如果该节点既有父母又有孩子,并且用户选择编辑其出生日期,则该函数必须不断检查两个日期之间的年龄是否已添加。我遇到的问题是使用我定义的方法进行持续检查。我希望有人能理解这个问题并提供帮助。注意 checkDOb 也设置 dob。就我而言,这是一个糟糕的命名。

这是代码:

private void dateCheck(FamilyMember node) {
    String dob = enterDateOfBirth();
    if (node.hasChildren()) {
        node.setDob(dob);
        checkDob(node, node.getOldestChild(), 0);            
    }

    FamilyMember parent = null;
    if (node.hasMother() && node.hasFather()) {
        if (node.getMother().getAge() > node.getFather().getAge()) {
            parent = node.getFather();
        } else {
            parent = node.getMother();
        }
        checkDob(parent, node, 1);
    } else {
        //single parent
        if (node.hasMother()) {
            parent = node.getMother();
            checkDob(parent, node, 1);
        }

        if (node.hasFather()) {
            parent = node.getFather();
            checkDob(parent, node, 1);
        }
    }
}

private void checkDob(FamilyMember parent, FamilyMember child, int member) {
    out.println(parent.getYear());
    out.println(child.getYear());
    while (parent.getYear() > child.getYear()) {
        out.println("Invalid Date - The Oldest Child of " + parent.getFullName()
                + "(" + child.getFullName() + ")\n cannot older than his/her parents. Try Again.");
        out.println();
        if (member == 0) {
            parent.setDob(enterDateOfBirth());
        }
        if (member == 1) {
            child.setDob(enterDateOfBirth());
        }
    }
}

private String enterDateOfBirth() {
    out.print("Enter Year Of Birth (0 - 2011): ");
    String y = in.nextLine();

    out.print("Enter Month Of Birth (1-12): ");
    String m = in.nextLine();
    if (m.trim().equals("")) {
        m = "0";
    }
    if (m.length() == 1) {
        m = "0" + m;
    }
    m += "-";

    out.print("Enter Date of Birth (1-31): ");
    String d = in.nextLine();

    if (d.trim().equals("")) {
        d = "0";
    }
    if (d.length() == 1) {
        d = "0" + d;
    }
    d += "-";

    String dob = d + m + y;
    while (!DateValidator.isValid(dob)) {
        out.println("Invalid date. Try again.");
        dob = enterDateOfBirth();
    }
    return (dob);
}

提前致谢。

I have a family tree app which allows you to build nodes. I am stuck on a problem which requires editing a members date of birth. The date of birth is just a string in the following format dd-mm-yyyy. My problem arises when checking if the date of birth is valid (i.e. any parent cannot be younger than a child). So if the node has both parents and children and user selects to edit it's date of birth, the function must continuously check to see whether an age between the two dates has been added. The problem I am having is getting this continual check to occur using the methods I have defined. I'm hoping someone understands the isue and can help. Note checkDOb also sets the dob too. its bad naming on my part.

here is the code:

private void dateCheck(FamilyMember node) {
    String dob = enterDateOfBirth();
    if (node.hasChildren()) {
        node.setDob(dob);
        checkDob(node, node.getOldestChild(), 0);            
    }

    FamilyMember parent = null;
    if (node.hasMother() && node.hasFather()) {
        if (node.getMother().getAge() > node.getFather().getAge()) {
            parent = node.getFather();
        } else {
            parent = node.getMother();
        }
        checkDob(parent, node, 1);
    } else {
        //single parent
        if (node.hasMother()) {
            parent = node.getMother();
            checkDob(parent, node, 1);
        }

        if (node.hasFather()) {
            parent = node.getFather();
            checkDob(parent, node, 1);
        }
    }
}

private void checkDob(FamilyMember parent, FamilyMember child, int member) {
    out.println(parent.getYear());
    out.println(child.getYear());
    while (parent.getYear() > child.getYear()) {
        out.println("Invalid Date - The Oldest Child of " + parent.getFullName()
                + "(" + child.getFullName() + ")\n cannot older than his/her parents. Try Again.");
        out.println();
        if (member == 0) {
            parent.setDob(enterDateOfBirth());
        }
        if (member == 1) {
            child.setDob(enterDateOfBirth());
        }
    }
}

private String enterDateOfBirth() {
    out.print("Enter Year Of Birth (0 - 2011): ");
    String y = in.nextLine();

    out.print("Enter Month Of Birth (1-12): ");
    String m = in.nextLine();
    if (m.trim().equals("")) {
        m = "0";
    }
    if (m.length() == 1) {
        m = "0" + m;
    }
    m += "-";

    out.print("Enter Date of Birth (1-31): ");
    String d = in.nextLine();

    if (d.trim().equals("")) {
        d = "0";
    }
    if (d.length() == 1) {
        d = "0" + d;
    }
    d += "-";

    String dob = d + m + y;
    while (!DateValidator.isValid(dob)) {
        out.println("Invalid date. Try again.");
        dob = enterDateOfBirth();
    }
    return (dob);
}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏月而来 2024-10-19 06:07:38

解析日期的标准方法是使用 SimpleDateFormat。但我认为这对你的情况没有帮助,所以我不会去那里。

但是当您谈论日期时,您应该使用 Date 对象(或者,正如其他人所说:使用 JodaTime 的 DateTime 对象),这会让事情变得更容易。

Date 设为 dob 的类型

并交换此代码:

String dob = d + m + y;

为此:(

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(y));
cal.set(Calendar.MONTH, Integer.parseInt(m)-1);
cal.set(Calendar.DATE, Integer.parseInt(d));
Date dob = cal.getTime();

您应该丢失所有 if (m.length() == 1) 的东西,因为带有前导零的字符串将被解析为八进制值)

现在您可以验证 parentBirthDate.compareTo(childBirthDate)>0

另外,更准确地说,您应该截断所有字段小于日:

cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

另外:不要使用 System.out.println(),使用 Log4J 或 SLF4J 等日志框架。

哦,顺便说一句,验证孩子比父母年轻是不够的。您可能必须验证差异是否为 12 年或更长:-)

Well the standard way to parse Dates is using SimpleDateFormat. But I don't think that will help in your case you so I won't go there.

But you should use Date objects (or, as others will say: use JodaTime's DateTime object) when you are talking about Dates, it makes things easier.

make Date the type of dob

and exchange this code:

String dob = d + m + y;

for this:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.parseInt(y));
cal.set(Calendar.MONTH, Integer.parseInt(m)-1);
cal.set(Calendar.DATE, Integer.parseInt(d));
Date dob = cal.getTime();

(you should lose all the if (m.length() == 1) stuff, because Strings with leading zeroes will be parsed as octal values)

Now you can just validate that parentBirthDate.compareTo(childBirthDate)>0

Also, to be more precise you should truncate all the fields that are smaller than day:

cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

Also: don't use System.out.println(), use a logging framework like Log4J or SLF4J.

Oh, BTW, validating that the child is younger than the parent won't be enough. You will probably have to validate that the difference is 12 years or more :-)

束缚m 2024-10-19 06:07:38

它可以是smt之类的

//return true if one of arguments is true!
private boolean bornBefore(FamilyMember first, FamilyMember second){
    if(first == null || second == null){
        return true;
    }
    return /*your magic date comaparator*/;
}
private boolean validDate(FamilyMember node, MagicDate date) {
    return bornBefore(node, node.getOldestChild())
            && bornBefore(node.getFather(), node)
            && bornBefore(node.getMother(), node); 
    }
}

It can be smt like

//return true if one of arguments is true!
private boolean bornBefore(FamilyMember first, FamilyMember second){
    if(first == null || second == null){
        return true;
    }
    return /*your magic date comaparator*/;
}
private boolean validDate(FamilyMember node, MagicDate date) {
    return bornBefore(node, node.getOldestChild())
            && bornBefore(node.getFather(), node)
            && bornBefore(node.getMother(), node); 
    }
}
别低头,皇冠会掉 2024-10-19 06:07:38

您应该将验证和数据输入分离。首先,您拥有包含当前值和要输入以更改值的新字符串的树模型。

bool checkDOB(String date, FamilyMember node) {
  //return true if date is more recent than any parents of node and
  //older than any children of node
}

现在您已经有了一个独立的验证方法,您应该在任何要添加新节点或编辑节点的时候调用它。在验证之前不要更改实际数据模型中的任何值。这意味着您的树将始终处于有效状态,并且编辑只是本地操作。

You should decouple your validation and data entry. To start of you have your tree model with the current values and a new string that you want to enter in to change a value.

bool checkDOB(String date, FamilyMember node) {
  //return true if date is more recent than any parents of node and
  //older than any children of node
}

Now that you have a stand alone validation method, you should call it anytime you are about to add a new node or edit one. Do not change any values in the actual data model until it has been validated This means your tree will always be in a valid state and an edit is just a local operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文