Exemplo n.º 1
0
        public void Calculate_Stops_Number()
        {
            StarshipModel test = new StarshipModel()
            {
                Name        = "Millennium Falcon",
                Consumables = "2 months",
                Mglt        = "75"
            };

            Assert.IsTrue(Convert.ToInt32(MainCalculator.CalculateStops(1000000, test).Stops) == 9, "OK");

            test = new StarshipModel()
            {
                Name        = "Y-wing",
                Consumables = "1 week",
                Mglt        = "80"
            };

            Assert.IsTrue(Convert.ToInt32(MainCalculator.CalculateStops(1000000, test).Stops) == 74, "OK");

            test = new StarshipModel()
            {
                Name        = "Rebel Transport",
                Consumables = "6 months",
                Mglt        = "20"
            };

            Assert.IsTrue(Convert.ToInt32(MainCalculator.CalculateStops(1000000, test).Stops) == 11, "OK");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Method that calculates the number of necessary stops using as reference the value of consumables, distance and mglt
        /// </summary>
        /// <param name="distance">Long integer representing the distance in MGLT</param>
        /// <param name="starship">Model containing the ship information to be calculated</param>
        /// <returns>Object containing the name, number of necessary stops and other auxiliary properties</returns>
        public static StarshipModel CalculateStops(int distance, StarshipModel starship)
        {
            if (starship == null || distance == 0)
            {
                starship.Stops = "unknown";
                return(starship);
            }

            try
            {
                int consumable = ConverterComsumableToInt(starship.Consumables);

                if (int.TryParse(starship.Mglt, out int mglt))
                {
                    if (mglt == 0 || consumable == 0)
                    {
                        starship.Stops = "unknown";
                    }

                    starship.Stops = ((long)(distance / mglt / consumable)).ToString();
                }
                else
                {
                    starship.Stops = "unknown";
                }

                return(starship);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 3
0
        public static async Task <StarshipModel> GetSingleStarship(string id)
        {
            string url = $"https://swapi.co/api/starships/{ id }/";

            StarshipModel cached = starshipCache.Where(s => s.Id == id).FirstOrDefault();

            if (cached != null)
            {
                return(cached);
            }

            using (HttpResponseMessage response = await apiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    // if response was successful
                    // read the content of the response asynchronously
                    // map it over to the person model
                    StarshipModel output = await response.Content.ReadAsAsync <StarshipModel>();

                    output.Id = id;

                    return(output);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int distance   = GetDistance();
            var gatewayApi = new GatewayApi();

            try
            {
                var starships = gatewayApi.GetAllStarships();

                foreach (StarshipModel item in starships)
                {
                    StarshipModel stops = MainCalculator.CalculateStops(distance, item);
                    if (int.TryParse(stops.Stops, out int stop))
                    {
                        if (Convert.ToInt32(stops.Stops) >= 0)
                        {
                            Console.WriteLine($"{item.Name}: {stops.Stops}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"An error occurred. {e}");
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);
        }
        public void ShouldReturnTheCorrectNumberOfStops()
        {
            JObject                json            = JObject.Parse(StarshipJson);
            StarshipModel          starship        = json.ToObject <StarshipModel>();
            CalculateNumberOfStops stopsCalculator = new CalculateNumberOfStops();
            var numberOfStops           = stopsCalculator.GetRequiredNumberOfStops(1000000, starship.MGLT, starship.consumables);
            var invalidMGLTInput        = stopsCalculator.GetRequiredNumberOfStops(1, string.Empty, starship.consumables);
            var invalidConsumablesInput = stopsCalculator.GetRequiredNumberOfStops(1, starship.MGLT, string.Empty);

            Assert.IsNotNull(numberOfStops);
            Assert.AreEqual(9, numberOfStops);
            Assert.IsNull(invalidMGLTInput);
            Assert.IsNull(invalidConsumablesInput);
        }
Exemplo n.º 6
0
        public static async Task <List <StarshipModel> > DisplayAllStarships(List <StarshipModel> starships)
        {
            string lookUpAnother = "";

            do
            {
                try
                {
                    foreach (StarshipModel s in starships)
                    {
                        string id = s.Url.Split('/')[5];
                        s.Id = id;
                        Console.WriteLine($"{s.Id} {s.Name}");
                    }
                    Console.Write("\nPlease enter an ID number: ");
                    string idText = Console.ReadLine();

                    StarshipModel starship = await GetSingleStarship(idText);

                    List <PersonModel> person = await CharacterController.GetStarWarsCharacters(starship.Pilots);

                    List <FilModel> film = await FilmController.GetStarWarsFilms(starship.Films);

                    Console.WriteLine("============================================================");
                    Console.WriteLine($"Name: {starship.Name} | Model: {starship.Model}");
                    Console.WriteLine($"Manufacturer: {starship.Manufacturer} | Class: {starship.StarshipClass}");
                    Console.WriteLine($"Cost: {starship.CostInCredits} | Cargo Capacity: {starship.CargoCapacity}");
                    Console.WriteLine($"Consumables: {starship.Consumables} | Max Speed: {starship.MaxAtmospheringSpeed}");
                    Console.WriteLine($"Hyperdrive: {starship.HyperdriveRating} | MGLT: {starship.MGLT} | Length: {starship.Length}");
                    Console.WriteLine($"Crew: {starship.Crew} | Passengers: {starship.Passengers}");
                    Console.WriteLine($"Pilots: {CharacterController.FormatPersonLine(person)}");
                    Console.WriteLine($"Films: {FilmController.FormatFilmLine(film)}");
                    Console.WriteLine("============================================================");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }

                Console.Write("Do you want to look up another character (y/n)?");
                lookUpAnother = Console.ReadLine();
            } while (lookUpAnother == "y");

            return(null);
        }
Exemplo n.º 7
0
        public static async Task <List <StarshipModel> > GetStarWarsStarShips(string[] id)
        {
            string url = "";
            List <StarshipModel> model = new List <StarshipModel>();

            foreach (string apiLink in id)
            {
                apiLink.Split(',');

                url = $"{ apiLink }";

                StarshipModel cahced = starshipCache.Where(s => s.Id == apiLink).FirstOrDefault();

                if (cahced != null)
                {
                    model.Add(cahced);
                }
                else
                {
                    using (HttpResponseMessage response = await apiClient.GetAsync(url))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            StarshipModel output = await response.Content.ReadAsAsync <StarshipModel>();

                            output.Id = apiLink;

                            model.Add(output);
                            starshipCache.Add(output);
                        }
                        else
                        {
                            throw new Exception(response.ReasonPhrase);
                        }
                    }
                }
            }

            return(model);
        }
Exemplo n.º 8
0
 internal void UpdateCell(StarshipModel starship)
 {
     NameLabel.Text   = "Name: " + starship.Name;
     LengthLabel.Text = "Length: " + starship.Length.ToString(CultureInfo.InvariantCulture);
 }