这个问题也存在于以下链接Question at this link
我能够清除3个中的2个测试用例,但不能清除1个测试用例。我还将在这里上传我的代码。

●创建一个名为AlertDAO的新程序包本地接口,其中包含与MapAlertDAO相同的方法。

●MapAlertDAO应该实现AlertDAO接口。

●AlertService应该具有接受AlertDAO的构造函数。

●raiseAlert和getAlertTime方法应使用通过构造函数传递的对象

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

interface AlertDAO
{
   public UUID addAlert(Date time);
   public Date getAlert(UUID id);
}

class AlertService
{
   private AlertDAO objAlertDAO;
   private final MapAlertDAO storage = new MapAlertDAO();
   public AlertService(AlertDAO objAlertDAO)
   {
      this.objAlertDAO=objAlertDAO;
   }
   public UUID raiseAlert()
   {
      return this.storage.addAlert(new Date());
   }
   public Date getAlertTime(UUID id)
   {
      return this.storage.getAlert(id);
   }
}
class MapAlertDAO implements AlertDAO
{
    private final Map<UUID, Date> alerts = new HashMap<UUID, Date>();
    public UUID addAlert(Date time)
    {
        UUID id = UUID.randomUUID();
        this.alerts.put(id, time);
        return id;
    }
    public Date getAlert(UUID id)
    {
        return this.alerts.get(id);
    }
    public static void main(String args[])
    {
        AlertService obj =new AlertService(new MapAlertDAO());
    }
}

最佳答案

传递代码

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

class AlertService {
    private final AlertDAO storage;

    public AlertService(AlertDAO storage) {
        this.storage = storage;
    }

    public UUID raiseAlert() {
        return this.storage.addAlert(new Date());
    }

    public Date getAlertTime(UUID id) {
        return this.storage.getAlert(id);
    }
}

interface AlertDAO {

    UUID addAlert(Date time);
    Date getAlert(UUID id);

}

class MapAlertDAO implements AlertDAO {
    private final Map<UUID, Date> alerts = new HashMap<UUID, Date>();

    @Override
    public UUID addAlert(Date time) {
        UUID id = UUID.randomUUID();
        this.alerts.put(id, time);
        return id;
    }

    @Override
    public Date getAlert(UUID id) {
        return this.alerts.get(id);
    }
}

07-27 22:54