'Alert Service (Refactor the AlertService and MapAlertDAO classes)

This question is there on the following link also Question at this link I am able to clear 2 test cases out of 3 but not able to clear 1 test case. I will also upload my code here.

●Create a new package local interface, named AlertDAO, that contains the same methods as MapAlertDAO.

●MapAlertDAO should implement the AlertDAO interface.

●AlertService should have a constructor that accepts AlertDAO.

●The raiseAlert and getAlertTime methods should use the object passed through the constructor

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());       
    }    
}


Solution 1:[1]

The passing code

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);
    }   
}

Solution 2:[2]

//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];
}
}

Solution 3:[3]

I don't have the answer however I think that the test is asking to test various edge cases, which I am not sure how to do. I think that the answer may lie in testing the UUID. Could it be NULL or incorrectly formed? Again, I am not sure how to test the UUID.

Solution 4:[4]

This code passes all test cases

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);
}

Solution 5:[5]

The code below passes all 3

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);
    
}

Solution 6:[6]

The raiseAlert and getAlertTime methods should use the object passed through the constructor

This means that you should not create a MapAlertDAO locally. You need to use the one passed to its constructor.

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);
   }    
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Strejc
Solution 2 Vishal Khatal
Solution 3 Adie
Solution 4 sally
Solution 5 Andronicus
Solution 6 user7