Beispiel #1
0
        static void Main(string[] args)
        {
            // Clear output
            OutputLoggerHelper.ClearOutputFile();

            OutputLoggerHelper.WriteToOutput("*** Message Application Started ***");

            OutputLoggerHelper.WriteToOutput($"* Simulating data start: { DateTime.Now.ToString() } *");

            StartupMessageGenerator generator = new StartupMessageGenerator();

            generator.CreateMessages();

            OutputLoggerHelper.WriteToOutput($"* Simulating data finish: { DateTime.Now.ToString() } * ");

            // Invoke execution only if you manage to get Instance
            ApplicationExecutor.Instance?.StartExecution();

            OutputLoggerHelper.WriteToOutput("*** Message Application Finished ***");

            // Only display the output file if it exists and we did not set it to skip
            if (OutputLoggerHelper.OutputFileExists() && !OutputLoggerHelper.SkipOutputFile)
            {
                Console.WriteLine("Press Enter to open the output log txt file ");
                Console.ReadLine();
                OutputLoggerHelper.DisplayOutputFile();
            }

            Console.WriteLine("Press any key to exit application.");

            Console.ReadLine();
        }
Beispiel #2
0
        public override bool MessageIsValid(Message message)
        {
            if (!base.MessageIsValid(message))
            {
                return(false);
            }

            if (message.MessageType != MessageType.Adjustment)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage("The message type must be Adjustment.", message.MessageId));
                return(false);
            }

            if (message.SaleAdjustment == null)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage("The sale adjustment cannot be empty for sale adjustment message.", message.MessageId));
                return(false);
            }

            if (message.SaleAdjustment.AdjustmentValue < 1 || message.SaleAdjustment.AdjustmentValue == Decimal.MinValue)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage($"The sale adjustment value must be at least { new decimal(1).ToString("n2") } for sale adjustment message. Current sale adjustment: { message.SaleAdjustment.AdjustmentValue.ToString() }", message.MessageId));
                return(false);
            }

            return(true);
        }
        public void ProcessMessage(Message message)
        {
            try
            {
                // Validate message
                bool messageValidated = MessageValidatorFactory.MessageIsValid(message);

                if (!messageValidated)
                {
                    DataManager.AddFailedMessage(message);
                    return;
                }

                // Get appropriate sale executor and invoke him
                bool saleCompleted = SaleExecutorFactory.GetSaleExecutor(message).ExecuteSale();

                // Mark as processed
                message.ProcessedAt = DateTime.Now;

                if (!saleCompleted)
                {
                    // Code duplication. TODO: Refactor to method and slight logic change
                    DataManager.AddFailedMessage(message);
                    return;
                }

                DataManager.AddCompletedMessage(message);

                // check messages and evaluate if we need to report
                if (DataManager.GetReadOnlyCompletedMessages().Count % REPORT_SALES_THRESHOLD == 0)
                {
                    ReportManager.ReportSalesPerProduct();
                }

                // Here we should freeze application until finished
                if (DataManager.GetReadOnlyCompletedMessages().Count % REPORT_ADJUSTMENTS_THRESHOLD == 0)
                {
                    appStatus = ApplicationStatus.Paused;
                    OutputLoggerHelper.WriteToOutput("*** Application paused. Queueing all incoming messages.");

                    ReportManager.ReportSaleAdjustments();

                    appStatus = ApplicationStatus.Running;
                    OutputLoggerHelper.WriteToOutput("*** Application resumed. Queueing all incoming messages.");
                    OutputLoggerHelper.WriteToOutput(string.Empty);
                }
            }
            catch (Exception ex)
            {
                DataManager.AddFailedMessage(message);
                string initialMsg = "An error occured while processing the message";
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedExceptionMessage(initialMsg, ex));
            }
        }
Beispiel #4
0
 /// <summary>
 /// Adds a sale to the data manager.
 /// </summary>
 /// <param name="sale">The incoming sale</param>
 /// <returns>True if success, False if failed. In case of exception it will report the exception</returns>
 public static bool AddSale(Sale sale)
 {
     try
     {
         _sales.Add(sale);
         return(true);
     }
     catch (Exception ex)
     {
         string initialMsg = "An exception occured trying to save the incoming sale";
         OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedExceptionMessage(initialMsg, ex));
         return(false);
     }
 }
Beispiel #5
0
        public override bool MessageIsValid(Message message)
        {
            if (!base.MessageIsValid(message))
            {
                return(false);
            }

            if (message.MessageType != MessageType.Single)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage("The message type must be Single.", message.MessageId));
                return(false);
            }

            return(true);
        }
        public virtual bool MessageIsValid(Message message)
        {
            if (message.Sale == null)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage("The incoming sale cannot be empty", message.MessageId));
                return(false);
            }

            if (message.Sale.SaleValue <= 0)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage($"The incoming sale value must be higher than 0. Current value { message.Sale.SaleValue }", message.MessageId));
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Reports the summary of sales per product
        /// </summary>
        public static void ReportSalesPerProduct()
        {
            OutputLoggerHelper.WriteToOutput(string.Empty);
            OutputLoggerHelper.WriteToOutput("*** Start: Reporting Sales per Product. ***");

            var groupedSalesByProduct = DataManager.GetReadOnlySaleList().GroupBy(s => s.Product);

            foreach (var sale in groupedSalesByProduct)
            {
                OutputLoggerHelper.WriteToOutput("* Product:\t\t " + sale.Key.ToString());
                OutputLoggerHelper.WriteToOutput("* Sale Nmbr:\t\t " + sale.Count().ToString());
                OutputLoggerHelper.WriteToOutput("* Total Value:\t\t " + sale.Sum(s => s.SaleValue).ToString("n2"));
                OutputLoggerHelper.WriteToOutput(string.Empty);
            }
            OutputLoggerHelper.WriteToOutput("*** End: Reporting Sales per Product. ***");
            OutputLoggerHelper.WriteToOutput(string.Empty);
        }
        /// <summary>
        /// Reports the sale adjustments made per adjustment type.
        /// </summary>
        public static void ReportSaleAdjustments()
        {
            OutputLoggerHelper.WriteToOutput(string.Empty);
            OutputLoggerHelper.WriteToOutput("*** Start: Reporting Sale Adjustments. ***");

            var groupedSalesByAdjustmentType = DataManager.GetReadOnlySaleAdjustments().GroupBy(s => s.AdjustmentType);

            foreach (var saleAdjustmentType in groupedSalesByAdjustmentType)
            {
                OutputLoggerHelper.WriteToOutput(Environment.NewLine + "* Sale Adjustment Type: " + saleAdjustmentType.Key.ToString());

                foreach (SaleAdjustmentLog sa in saleAdjustmentType)
                {
                    OutputLoggerHelper.WriteToOutput(sa.ToString());
                }
            }
            OutputLoggerHelper.WriteToOutput("*** End: Reporting Sale Adjustments. ***");
            OutputLoggerHelper.WriteToOutput(string.Empty);
        }
Beispiel #9
0
        public override bool MessageIsValid(Message message)
        {
            if (!base.MessageIsValid(message))
            {
                return(false);
            }

            if (message.MessageType != MessageType.Multi)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage("The message type must be Multi.", message.MessageId));
                return(false);
            }

            if (message.Sale.SaleOccurrences.GetValueOrDefault(1) <= 1)
            {
                OutputLoggerHelper.WriteToOutput(ExceptionHelper.GetUnifiedWarningMessage($"The sale occurrence must be bigger than 1 for a multi sale. Current value: { message.Sale.SaleOccurrences }", message.MessageId));
                return(false);
            }
            return(true);
        }