Esempio n. 1
0
 public void CheckGetAllStarshipsIsOk()
 {
     try
     {
         var starships = _starshipService.GetAllStarships().Result;
         Assert.NotNull(starships);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Calculates number of stops for number of megalights for each starship in StarWars, returns list of string
        /// </summary>
        /// <param name="mglt"></param>
        public async Task <List <Tuple <string, string> > > CalculateNumberOfStopsForEachStarship(int mglt)
        {
            #region log
            _logger.LogInformation($"Invoked CalculateNumberOfStopsForEachStarship, mglt: {mglt}");
            #endregion
            List <Tuple <string, string> > retList = new List <Tuple <string, string> >();

            try
            {
                var starships = await _starshipService.GetAllStarships();//get all starships

                #region log
                _logger.LogInformation($"{starships.Count} number of starship found, beggining calculation");
                #endregion

                var starshipsDetailed = starships.ToListStarshipDetailedModel();//map to detailed model

                foreach (var starshipDetailed in starshipsDetailed)
                {
                    string numberOfStops = string.IsNullOrEmpty(starshipDetailed.MGLT) || string.Equals(starshipDetailed.MGLT, "unknown") ?
                                           "UNKNOWN" :
                                           CalculateNumberOfStops(Convert.ToInt32(starshipDetailed.MGLT), starshipDetailed.ConsumablesValueInHours.Value, mglt).ToString();

                    retList.Add(new Tuple <string, string>(starshipDetailed.Name, numberOfStops));//add to return list
                }
                #region log
                _logger.LogInformation($"Successfully calculated!");
                #endregion
            }
            catch (Exception ex)
            {
                #region log
                _logger.LogError(GetErrorMessage(ex));
                #endregion
                throw ex;
            }

            return(retList);
        }
        public List <ResultDTO> CalculateStops(long distance)
        {
            StarshipService starshipService = new StarshipService();
            var             result          = new List <ResultDTO>();
            var             starships       = starshipService.GetAllStarships();

            foreach (var starship in starships)
            {
                var  resultToAdd = new ResultDTO();
                int  MGLT        = 0;
                long stops       = 0;
                if (starship.MGLT == "unknown")
                {
                    resultToAdd.Unknown = true;
                }
                else
                {
                    MGLT = Convert.ToInt32(starship.MGLT);
                }
                string[] consumablesParsing = starship.Consumables.Split(' ');
                if (consumablesParsing.Length == 2 && MGLT != 0)
                {
                    int    consumableNumber = Convert.ToInt32(consumablesParsing[0]); // get the consumables lemits for the the star ship
                    string strTime          = consumablesParsing[1];                  // Get the Start time form consumable
                    int    consumablesDays  = getDays(consumableNumber, strTime);

                    int movimentInOneDay      = MGLT * 24;
                    int distanceUntilNextStop = movimentInOneDay * consumablesDays;
                    stops = distance / distanceUntilNextStop;
                }
                resultToAdd.StarshipName  = starship.Name;
                resultToAdd.NumberOfStops = stops;
                result.Add(resultToAdd);
            }
            return(result);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            bool repeat = true;

            while (repeat)
            {
                Console.Clear();
                Console.WriteLine("Author: Igor Abílio Santana Santos");
                Console.WriteLine("Created: 21/03/2018");

                Console.WriteLine();
                Console.WriteLine();

                int  distance = 0;
                bool success  = false;
                Console.Write("Could you inform a distance between 2 planets in MGLT: ");

                while (!success)
                {
                    try
                    {
                        distance = Convert.ToInt32(Console.ReadLine());
                        success  = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("The distance needs to be a integer.");
                        Console.WriteLine();
                        Console.Write("Please inform a distance between 2 planets in MGLT: ");
                    }
                }

                Console.WriteLine();

                Console.WriteLine("Result:");

                Conversion      conversion      = new Conversion();
                StarshipService starshipService = new StarshipService();

                try
                {
                    var starships = starshipService.GetAllStarships();
                    foreach (var starship in starships)
                    {
                        var hours  = conversion.CalculateHours(starship.consumables);
                        var result = distance / (hours * Convert.ToInt32(starship.MGLT));
                        Console.WriteLine($"-> { starship.name }: { result } stop(s)");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.WriteLine();
                Console.WriteLine();

                Console.Write("Would you like to make another simulation? If YES press Y or press ENTER to exit... ");
                ConsoleKeyInfo answer = Console.ReadKey();
                if (!answer.KeyChar.ToString().ToUpper().Equals("Y"))
                {
                    repeat = false;
                }
            }
        }