警报服务(重构AlertService和MapAlertDAO类)

4
这个问题也可以在下面的链接中找到:此链接中的问题 我能够通过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());       
    }    
}
5个回答

12
传递的代码
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);
    }   
}

1
以下代码通过了所有三个测试。
class AlertService {
    
    private final MapAlertDAO storage = new MapAlertDAO();
    private AlertDAO obj;
    
    public AlertService(AlertDAO obj){
        this.obj = obj;
    }
        
    public UUID raiseAlert() {
        return this.obj.addAlert(new Date());
    }
    
    public Date getAlertTime(UUID id) {
        return this.obj.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);
    }   
}

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

1
//This code pass the all test cases
using System.Collections.Generic;
using System;

public class AlertService
{
private readonly IAlertDAO storage;
public AlertService(IAlertDAO _alertDAO)
{
    storage = _alertDAO;
}
public Guid RaiseAlert()
{
    return this.storage.AddAlert(DateTime.Now);
}
public DateTime GetAlertTime(Guid id)
{
    return this.storage.GetAlert(id);
}
}
public interface IAlertDAO
{
   Guid AddAlert(DateTime time);
   DateTime GetAlert(Guid id);
}

public class AlertDAO : IAlertDAO
{
private readonly Dictionary<Guid, DateTime> alerts = new Dictionary<Guid, DateTime> 
();
public Guid AddAlert(DateTime time)
{
    Guid id = Guid.NewGuid();
    this.alerts.Add(id, time);
    return id;
}
public DateTime GetAlert(Guid id)
{
    return this.alerts[id];
}
}

0

这段代码通过了所有的测试用例

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

class AlertService{
    private final MapAlertDAO storage = new MapAlertDAO();
    private AlertDAO obj;

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

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

    public Date getAlertTime(UUID id) {
        return this.obj.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);
    }   
}

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

-1
“raiseAlert”和“getAlertTime”方法应该使用通过构造函数传递的对象。这意味着您不应在本地创建“MapAlertDAO”。您需要使用传递给其构造函数的那个对象。
class AlertService 
{
   private AlertDAO objAlertDAO;
   public AlertService(AlertDAO objAlertDAO)
   {
      this.objAlertDAO=objAlertDAO;
   }
   public UUID raiseAlert() 
   {
      return objAlertDAO.addAlert(new Date());
   }    
   public Date getAlertTime(UUID id) 
   {
      return objAlertDAO.getAlert(id);
   }    
}

这个也不起作用,我已经尝试过了,你甚至可以在给定的链接上尝试相同的操作。 - Piyush Yawalkar
1
我不知道为什么它失败了。它已经满足了所有的要求。 - Thiyagu
是的,我也尝试过,根据我的理解它满足所有条件,但不知道为什么只有2个测试用例通过了。我昨天试过你给出的解决方案,但没起作用,所以我在这里发了问题。 - Piyush Yawalkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接