2

我的代码没有显示正确或所需的输出时遇到问题。这是我写的代码。

import java.time.LocalDateTime;
public class DriversLicense
{
   private String name;
   private int id;
   private int expYear;
   private int expMonth; 
   private int expDay;


    public DriversLicense(String name, int id, int expYear, int expMonth, int expDay)
    {
        this.name = name;
        this.id = id;
        this.expYear = expYear;
        this.expMonth = expMonth;
        this.expDay = expDay;
    }

    public boolean isExpired()
    {
        LocalDateTime date = LocalDateTime.now();

        boolean tORf = false;

        int year = date.getYear();
        int month = date.getMonthValue();
        int day = date.getDayOfMonth();

        if(year > this.expYear && month > this.expMonth && day > this.expDay)
        {
            return true;
        }
        return tORf;
    }

    public void displayInfo()
    {
        System.out.println("Name: " + this.name);
        System.out.println("ID: " + this.id);
        System.out.println("Expiration: " + this.expYear + "/" + this.expMonth + "/" + this.expDay);
    }
}

在我的isExpired()方法中,它应该检查天气当前日期是否晚于 ID 的到期日期。如果 ID 过期,它应该打印出 true,否则它应该打印出 false。出于某种原因,当我的程序检查的三个 id 应该是 false true false 时,我得到了所有的错误。这是我下面的测试文件。

public class TestDL
{
   public static void main(String[] args)
   {
      DriversLicense dr1 = new DriversLicense("John Smith", 192891, 6, 21, 2018);
      dr1.displayInfo();
      System.out.println("Expired? " + dr1.isExpired());
      System.out.println();


      DriversLicense dr2 = new DriversLicense("Jennifer Brown", 728828, 5, 31, 2017);
      dr2.displayInfo();
      System.out.println("Expired? " + dr2.isExpired());
      System.out.println();

      DriversLicense dr3 = new DriversLicense("Britney Wilson", 592031, 7, 15, 2019);
      dr3.displayInfo();
      System.out.println("Expired? " + dr3.isExpired());
      System.out.println();
   }
}

这也是我目前得到的输出。

Name: John Smith
ID: 192891
Expiration: 6/21/2018
Expired? false

Name: Jennifer Brown
ID: 728828
Expiration: 5/31/2017
Expired? false

Name: Britney Wilson
ID: 592031
Expiration: 7/15/2019
Expired? false
4

2 回答 2

5

您可以简单地为您的类使用LocalDate API,DriversLicense如下所示:

public class DriversLicense {

   private String name;

   private int id;

   private LocalDate expiryDate;

   public DriversLicense(String name,int id,int expYear,int expMonth,int expDay) {
      this.name = name;
      this.id = id;
      this.expiryDate = LocalDate.of(expYear, expMonth, expDay);
   }

   public boolean isExpired() {
      LocalDate currentDate = LocalDate.now();
      return currentDate.isAfter(expiryDate);
   }

   public void displayInfo() {
      System.out.println("Name: " + this.name);
      System.out.println("ID: " + this.id);
      System.out.println("Expiration: " + 
         expiryDate.getYear() + "/" + expiryDate.getMonth() + "/" + 
          expiryDate.getDayOfMonth());
   }

   @Override
   public String toString() {
      return "name=" + name + ", id=" + id + ", expiryDate=" + expiryDate;
   }
}

作为旁注,请记住,toString()如果您想查看对象内部的值,可以使用如上所示。

System.out.println()此外,使用一些日志框架(如log4j api )删除你的。

于 2018-01-29T06:54:21.563 回答
1

当您比较多个“嵌套”值(如 3 个日期字段)时,您应该只在年份值相等时比较月份值,因为当年份不同时,月份比较没有意义。

为了说明比较哪些字段,这里有一个表格:

+==================================================+================+
|                 Tests to perform                 |   Conclusion   |
+================+=================================+================+
| year1 < year2  |                                 |                |
+----------------+------------------+              |                |
|                | month1 < month2  |              | Date1 < Date2  |
|                +------------------+--------------+                |
|                |                  | day1 < day2  |                |
|                |                  +--------------+----------------+
| year1 == year2 | month1 == month2 | day1 == day2 | Date1 == Date2 |
|                |                  +--------------+----------------+
|                |                  | day1 > day2  |                |
|                +------------------+--------------+                |
|                | month1 > month2  |              | Date1 > Date2  |
+----------------+------------------+              |                |
| year1 > year2  |                                 |                |
+----------------+---------------------------------+----------------+

如您所见,您的测试非常不完整:

year > this.expYear && month > this.expMonth && day > this.expDay

我将由您自己编写正确的测试代码,尽管在构造函数中将 3 个expXxx字段组合成一个LocalDate对象会容易得多,因为LocalDate对象知道如何进行这种比较。

于 2018-01-29T07:05:58.530 回答