Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert Service (Refactor the AlertService and MapAlertDAO classes)

Tags:

java

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());       
    }    
}
like image 791
Piyush Yawalkar Avatar asked Dec 13 '22 14:12

Piyush Yawalkar


2 Answers

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);
    }   
}
like image 173
Martin Strejc Avatar answered Mar 29 '23 23:03

Martin Strejc


//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];
}
}
like image 23
Vishal Khatal Avatar answered Mar 29 '23 23:03

Vishal Khatal