Esempio n. 1
0
        public static async Task <TicketsData> GetData(string path)
        {
            var ticketsData = new TicketsData();

            var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                string line;
                line = await streamReader.ReadLineAsync();

                ticketsData.People  = Convert.ToInt32(line.Split(" ")[0]);
                ticketsData.Windows = Convert.ToInt32(line.Split(" ")[1]);

                var destinations = Convert.ToInt32(line.Split(" ")[2]);

                for (var i = 0; i < destinations; i++)
                {
                    line = await streamReader.ReadLineAsync();

                    ticketsData.Destinations.Add(line.Split(" ")[0], Convert.ToInt32(line.Split(" ")[1]));
                }

                for (var i = 0; i < ticketsData.People; i++)
                {
                    line = await streamReader.ReadLineAsync();

                    ticketsData.PersonDestinations.Enqueue(line);
                }
            }

            return(ticketsData);
        }
Esempio n. 2
0
        private static IEnumerable <int> GetInvalidNumbers(TicketsData ticketsData)
        {
            var nearbyTicketsNumbers = ticketsData.NearbyTicketsData.SelectMany(t => t.Numbers);
            var validNumbersRanges   = ticketsData.FieldsData.SelectMany(f => new[] { f.FirstRange, f.SecondRange });

            return(nearbyTicketsNumbers.Where(n => !validNumbersRanges.Any(r => n >= r.Start.Value && n <= r.End.Value)));
        }
Esempio n. 3
0
        /// <summary>
        /// Get Enum and Othe rrequired Data which is related to Tickets
        /// </summary>
        /// <returns>TblTicketMasterDto list</returns>
        public ResultMessage <TicketsData> GetTicketData()
        {
            var result = new ResultMessage <TicketsData> {
                Item = new TicketsData()
            };
            var userData = this.userManager.Users.Where(x => x.IsActive).Select(x => new CustomUserDto {
                FirstName = x.FirstName, Id = x.Id, LastName = x.LastName, UserName = x.UserName
            }).ToList();

            var ticketData = new TicketsData
            {
                AssignedTo = userData.Select(x => new NameValueIntPair
                {
                    Name  = x.FullName,
                    Value = x.Id
                }).ToList(),

                Priority = new TicketPriority().ToNameValueIntList(),
                Status   = new TicketStatus().ToNameValueIntList(),
                Type     = new TicketType().ToNameValueIntList()
            };

            result.Item = ticketData;

            return(result);
        }
Esempio n. 4
0
        public IActionResult Post([FromBody] TicketsData value)
        {
            var validationResult = value.Validate();

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }
            _memCache.Add(value);
            return(Ok($"{value.ToString()} has been added"));
        }
Esempio n. 5
0
        public IActionResult Post([FromBody] TicketsData value)
        {
            var validationResult = value.Validate();

            if (!validationResult.IsValid)
            {
                Log.Error($"[Tickets Controller] [Validation error] Fail post object \"{value.ToString()}\"");
                return(BadRequest(validationResult.Errors));
            }
            _memCache.Add(value);
            Log.Information($"[Tickets Controller] Object \"{value.ToString()}\" has ben added");
            return(Ok($"{value.ToString()} has been added"));
        }
Esempio n. 6
0
        public IActionResult Put(Guid id, [FromBody] TicketsData value)
        {
            if (!_memCache.Has(id))
            {
                return(NotFound("No such"));
            }
            var validationResult = value.Validate();

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }
            var previousValue = _memCache[id];

            _memCache[id] = value;
            return(Ok($"{previousValue.ToString()} has been updated to {value.ToString()}"));
        }
Esempio n. 7
0
        public IActionResult Put(Guid id, [FromBody] TicketsData value)
        {
            if (!_memCache.Has(id))
            {
                Log.Error($"[Tickets Controller] [No id's] Fail put object with id = {id}.");
                return(NotFound("No such"));
            }
            Log.Warning($"[Tickets Controller] Trying update object with id = {id}.");
            var validationResult = value.Validate();

            if (!validationResult.IsValid)
            {
                Log.Error($"[Tickets Controller] [Validation error] Cancel update object with id = {id}.");
                return(BadRequest(validationResult.Errors));
            }
            var previousValue = _memCache[id];

            _memCache[id] = value;
            Log.Information($"[Tickets Controller] Object {previousValue.ToString()} has been updated to {value.ToString()}");
            return(Ok($"{previousValue.ToString()} has been updated to {value.ToString()}"));
        }
Esempio n. 8
0
File: App.cs Progetto: taboo09/TDD
        public static OutputData PriceCalculation(TicketsData ticketData)
        {
            var output = new OutputData();

            decimal totalPrice = 0M;

            // records last dest per windows
            var windowLastDest = InitializeList(new List <string>(), ticketData.Windows, "None");

            // records the window number per customers
            var windows = new List <int>();

            for (int i = 0; i < ticketData.People; i++)
            {
                var personDestination = ticketData.PersonDestinations.Dequeue();
                var index             = windowLastDest.IndexOf(personDestination);

                if (index > -1)
                {
                    // destination has been previously used
                    windows.Add(index + 1);
                    totalPrice = totalPrice + Convert.ToDecimal(
                        ticketData.Destinations[personDestination] -
                        (ticketData.Destinations[personDestination] * 0.2)
                        );
                }
                else
                {
                    // assign new dest to an empty item or to the first one
                    AssignDestination(windowLastDest, personDestination, windows);
                    totalPrice = totalPrice + ticketData.Destinations[personDestination];
                }
            }

            output.Windows    = windows;
            output.TotalPrice = totalPrice;

            return(output);
        }
Esempio n. 9
0
File: App2.cs Progetto: taboo09/TDD
        public static OutputData PriceCalculation(TicketsData ticketData)
        {
            var output = new OutputData();

            decimal totalPrice = 0M;

            // records last dest per windows
            var windowLastDest = InitializeList(new List <string>(), ticketData.Windows, "None");

            // records the window number per customers
            var windows = new List <int>();

            // holds new M (windows ticket number) customer dest
            var nextDestinations = new List <string>();

            if (ticketData.People >= ticketData.Windows)
            {
                for (int i = 0; i < ticketData.Windows; i++)
                {
                    nextDestinations.Add(ticketData.PersonDestinations.Dequeue());
                }
            }
            else
            {
                for (int i = 0; i < ticketData.People; i++)
                {
                    nextDestinations.Add(ticketData.PersonDestinations.Dequeue());
                }
            }

            while (nextDestinations.Count > 0)
            {
                // find if dest has been used before
                var index = windowLastDest.IndexOf(nextDestinations[0]);

                if (index > -1)
                {
                    windows.Add(index + 1);

                    totalPrice = totalPrice + Convert.ToDecimal(
                        ticketData.Destinations[nextDestinations[0]] -
                        (ticketData.Destinations[nextDestinations[0]] * 0.2));
                }
                else
                {
                    // check if next dests have a match in windowLastDest list
                    var availableIndex = AssignDestination(windowLastDest, nextDestinations);
                    windows.Add(availableIndex + 1);

                    totalPrice = totalPrice + ticketData.Destinations[nextDestinations[0]];
                }

                // remove first element from temp dest list
                nextDestinations.RemoveAt(0);

                // remove first element from queue and add it to list if exists
                if (ticketData.PersonDestinations.Count > 0)
                {
                    nextDestinations.Add(ticketData.PersonDestinations.Dequeue());
                }
            }

            output.Windows    = windows;
            output.TotalPrice = totalPrice;

            return(output);
        }