private List <ChangeData> CalculateEntities(long changeAmount) { long changeRest = changeAmount; List <ChangeData> changeDataCollection = new List <ChangeData>(); while (changeRest > 0) { ChangeData currentData = new ChangeData(); AbstractProcessor proc = ProcessorFactory.CreateProcessor(changeRest); Dictionary <int, long> monetaryObjs = proc.CalculateChange(changeRest); currentData.Name = proc.GetName(); currentData.ChangeDictionary = monetaryObjs.Where(p => p.Value > 0).ToDictionary(p => p.Key, p => p.Value); long currentProduct = monetaryObjs.Sum(p => p.Key * p.Value); changeRest = changeRest - currentProduct; changeDataCollection.Add(currentData); // Dispara o evento informando que um processador foi executado. if (this.OnProcessorExecuted != null) { string nameProcessor = proc.GetName(); this.OnProcessorExecuted(this, nameProcessor, currentProduct); } } return(changeDataCollection); }
public ComputeChangeResponse ComputeChange(ComputeChangeRequest request) { ComputeChangeResponse response = new ComputeChangeResponse(); Logger.Log(request); try { // Verifica se os dados recebidos são válidos. if (request.IsValid == false) { response.OperationReport = request.ValidationReport; return(response); } uint change = CalculateTotalChangeAmount(request.ReceivedAmount, request.ProductAmount); uint totalChangeAmount = change; List <ChangeData> changeDataList = new List <ChangeData>(); while (change > 0) { AbstractProcessor processor = ProcessorFactory.Create(change); if (processor == null) { Report report = new Report(); report.Message = "Ocorreu um erro: Não há troco disponível"; response.OperationReport.Add(report); return(response); } Dictionary <uint, uint> changeDictionary = processor.Calculate(change); ChangeData changeData = new ChangeData() { Name = processor.GetName(), ChangeDictionary = changeDictionary }; changeDataList.Add(changeData); long totalCurrentAmount = changeDictionary.Sum(p => (p.Value * p.Key)); change -= (uint)totalCurrentAmount; } response.ChangeDataList = changeDataList; response.TotalChangeAmount = totalChangeAmount; response.Success = true; } catch (Exception e) { Logger.Log(e); Report report = new Report(); report.Message = "Ocorreu um erro: não foi possível processar sua operação."; response.OperationReport.Add(report); } Logger.Log(response); return(response); }
public Client(AbstractFactory factory) { pc = factory.CreatePc(); box = factory.CreateBox(); processor = factory.CreateProcessor(); mainboard = factory.CreateMainBoard(); hdd = factory.CreateHdd(); memory = factory.CreateMemory(); }
public override void Interact( AbstractBox box, AbstractProcessor processor, AbstractMainBoard mainBoard, AbstractHdd hdd, AbstractMemory memory) { Console.WriteLine(this + " interacts with \n{0}, \n{1}, \n{2}, \n{3}, \nand {4}", box, processor, mainBoard, hdd, memory); }
public virtual void SetSuccessor(AbstractProcessor successor) { if (Successor != null) { Successor.SetSuccessor(successor); } else { this.Successor = successor; } }
public CompositeBigProcessor(int exponent) { if (exponent <= 3) { lowProcessor = ratusProcessor; } else { lowProcessor = new CompositeBigProcessor(exponent - 3); } this.exponent = exponent; }
public ReturnAmountResponse ReturnAmount(ReturnAmountRequest returnCoinsRequest) { LogService.Save("Request", returnCoinsRequest); ReturnAmountResponse returnAmountResponse = new ReturnAmountResponse(); try { // Verifica se os dados recebidos são válidos. if (returnCoinsRequest.IsValid == false) { returnAmountResponse.OperationReport = returnCoinsRequest.ValidationReport; return(returnAmountResponse); } long changeAmount = returnCoinsRequest.PaidAmount - returnCoinsRequest.ProductAmount; long currentChangeAmount = changeAmount; List <ChangeData> chData = new List <ChangeData>(); while (currentChangeAmount > 0) { AbstractProcessor processor = ProcessorFactory.Create(currentChangeAmount); if (processor == null) { returnAmountResponse.OperationReport.Add(new Report(null, "Não foi possivel processar seu troco.")); return(returnAmountResponse); } Dictionary <long, int> result = processor.CalculateChange(currentChangeAmount); long resultTotalAmount = result.Sum(t => t.Key * t.Value); chData.Add(new ChangeData() { Type = processor.GetName(), TotalAmount = resultTotalAmount, Changes = result }); currentChangeAmount -= resultTotalAmount; } returnAmountResponse.TotalAmount = changeAmount; returnAmountResponse.Result = chData; returnAmountResponse.Success = true; } catch (Exception ex) { returnAmountResponse.OperationReport.Add(new Report(null, "Ocorreu um erro interno.")); LogService.Save("Exception", ex.Message); } LogService.Save("Response", returnAmountResponse); return(returnAmountResponse); }
public override void Render(AbstractRendererBase rendererBase, PonyTextContext textContext) { AbstractProcessor abstractProcessor = null; if (CustomProperty.HasProperty(AbstractProcessor.RENDER_PROCESSOR_PROPERTY)) { var processor = CustomProperty.GetProperty(AbstractProcessor.RENDER_PROCESSOR_PROPERTY) as string; abstractProcessor = textContext.ProcessorFactory.GetRenderProcessor(processor); abstractProcessor.PreRendering(textContext, rendererBase, this); } rendererBase.RenderText(Content, CustomProperty); abstractProcessor?.PostRendering(textContext, rendererBase, this); }
public override void Render(AbstractRendererBase rendererBase, PonyTextContext textContext) { rendererBase.StartParagraphRendering(CustomProperty); AbstractProcessor abstractProcessor = null; if (CustomProperty.HasProperty(AbstractProcessor.RENDER_PROCESSOR_PROPERTY)) { var processor = CustomProperty.GetProperty(AbstractProcessor.RENDER_PROCESSOR_PROPERTY) as string; abstractProcessor = textContext.ProcessorFactory.GetRenderProcessor(processor); abstractProcessor.PreRendering(textContext, rendererBase, this); } foreach (var text in texts) { text.Render(rendererBase, textContext); } abstractProcessor?.PostRendering(textContext, rendererBase, this); rendererBase.EndParagraphRendering(); }
/// <summary> /// Entry point. /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public async Task <object> FunctionHandler(JObject request, ILambdaContext context) { if (request == null) { throw new Exception("No request data given"); } if (Log.IsDebugEnabled()) { Log.Debug(context, "Request: " + request.ToString()); } var alexaRequest = request.ToObject <AlexaSmartHomeRequest>(); if (alexaRequest.Directive == null) { throw new Exception("Invalid request, directive missing"); } var h = alexaRequest.Directive.Header; if (h == null) { throw new Exception("Invalid request, header missing"); } Log.Debug(context, $"Checking request in namespace {h.Namespace}, name {h.Name}, version {h.PayloadVersion}"); if (h.PayloadVersion != "3") { throw new Exception($"Invalid payload version {h.PayloadVersion}, not supported!"); } AbstractProcessor processor = null; switch (h.Namespace) { case "Aloxi.MetaControl": processor = new AloxiMetaProcessor(); break; case "Alexa.Discovery": case "Alexa.PowerController": case "Alexa.PowerLevelController": case "Alexa.ModeController": processor = new AlexaPassthroughProcessor(); break; //case "Alexa.ConnectedHome.Query": // processor = new AlexaCachedQueryProcessor(); // break; } if (processor == null) { throw new Exception($"Namespace {h.Namespace} is not supported"); } Log.Info(context, $"Sending request '{h.Name}' to processor '{processor.Name}'"); var response = await processor.ProcessRequest(alexaRequest, context); if (Log.IsDebugEnabled()) { string respText = response?.ToString() ?? "<null>"; Log.Debug(context, "Response: " + respText); } return(response); }
public abstract void Interact( AbstractBox box, AbstractProcessor processor, AbstractMainBoard mainBoard, AbstractHdd hdd, AbstractMemory memory);