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());
}
}
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);
}
}
//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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With