逻辑问题 - Java
我有一个家谱应用程序,它允许您构建节点。我遇到了一个需要编辑成员出生日期的问题。出生日期只是一个格式为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解析日期的标准方法是使用
SimpleDateFormat
。但我认为这对你的情况没有帮助,所以我不会去那里。但是当您谈论日期时,您应该使用 Date 对象(或者,正如其他人所说:使用 JodaTime 的 DateTime 对象),这会让事情变得更容易。
将
Date
设为dob
的类型并交换此代码:
为此:(
您应该丢失所有
if (m.length() == 1) 的东西,因为带有前导零的字符串将被解析为八进制值)
现在您可以验证
parentBirthDate.compareTo(childBirthDate)>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 ofdob
and exchange this code:
for this:
(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:
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 :-)
它可以是smt之类的
It can be smt like
您应该将验证和数据输入分离。首先,您拥有包含当前值和要输入以更改值的新字符串的树模型。
现在您已经有了一个独立的验证方法,您应该在任何要添加新节点或编辑节点的时候调用它。在验证之前不要更改实际数据模型中的任何值。这意味着您的树将始终处于有效状态,并且编辑只是本地操作。
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.
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.