Exemple #1
0
        public async Task <List <StarShips> > GetStarShipsInformation(int distance = 0)
        {
            var result    = new List <StarShips>();
            var starShips = new StarShips();

            try
            {
                var count = 1;
                do
                {
                    starShips = await GetStarshipsPerPage(count);

                    if (distance > 0)
                    {
                        await GetStops(distance, starShips);
                    }

                    if (starShips != null)
                    {
                        result.Add(starShips);
                    }
                    count++;
                } while (starShips.next != null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(result);
        }
Exemple #2
0
        public async void GetAllShipsAsync()
        {
            try
            {
                Rest starwars = new Rest();
                //hämta 5 sidor med starships
                for (var i = 1; i < 5; i++)
                {
                    var response = await starwars.StarWarsApiRequestAsync($"starships/?page={i}");

                    var ship = starwars.Deserialize <dynamic>(response);

                    //Skriv ut hela sidan med skepp
                    for (var j = 0; j < ship["results"].Count; j++)
                    {
                        var results = ship["results"][j];
                        if (results != null)
                        {
                            StarShips.Add(results);
                            listBox.Items.Add($"{results["name"]}");
                        }
                    }
                }
            }
            catch (Exception)
            {
                listBox.Items.Add("Ops, något fel hände");
            }
        }
Exemple #3
0
        private async Task <StarShips> GetStarshipsPerPage(int page)
        {
            var result = new StarShips();

            using (var client = new HttpClient())
            {
                var uri = "https://swapi.co/api/starships/";
                if (page != 1)
                {
                    uri = $"{uri}?page={page}";
                }

                var message = new HttpRequestMessage()
                {
                    Method     = HttpMethod.Get,
                    RequestUri = new Uri(uri)
                };

                var response = await client.SendAsync(message);

                if (response.IsSuccessStatusCode)
                {
                    var responseJson = await response.Content.ReadAsStringAsync();

                    result = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject <StarShips>(responseJson));
                }
                else
                {
                    throw new Exception("Coudln't get starships information");
                }
            }

            return(result);
        }
Exemple #4
0
        public void ProcessReSupplyTest()
        {
            StarShips starShips = new StarShips();
            var       response  = starShips.ProcessReSupply(70000);

            Assert.IsTrue(response);
        }
Exemple #5
0
        private async void button1_Click_1Async(object sender, EventArgs e)
        {
            var list = await GetShipsFromSelectedPerson();

            if (list.Count != 0)
            {
                foreach (var ship in list)
                {
                    listBox.Items.Add($"{ship["name"]}, owned by {Person["name"]}");
                    StarShips.Add(ship);
                }
            }
            else
            {
                listBox.Items.Add($"{Person["name"]} doesn't own a starship");
            }
        }
Exemple #6
0
        //Välj skepp knapp
        private async void button1_Click(object sender, EventArgs e)
        {
            if (listBox.SelectedItem != null)
            {
                string[] shipname = listBox.SelectedItem.ToString().Split(',');
                foreach (var ship in StarShips.ToList())
                {
                    if (ship["name"] == shipname[0])
                    {
                        await Task.Delay(1000);

                        form3.ShowForm(Person, ship);
                        this.Hide();
                    }
                }
            }
        }
Exemple #7
0
        private async Task GetStops(int distance, StarShips starShips)
        {
            foreach (var item in starShips.results)
            {
                if (!item.MGLT.Contains("unknown") && !item.consumables.Contains("unknown"))
                {
                    decimal result = 0;
                    var     aux    = item.consumables.Split(" ");

                    if (item.consumables.ToLower().Contains("day"))
                    {
                        //Quantidade de horas em um dia
                        result = distance / ((24 * int.Parse(aux[0])) * int.Parse(item.MGLT));
                    }
                    else if (item.consumables.ToLower().Contains("week"))
                    {
                        //Quantidade de horas em uma semana
                        result = distance / ((168 * int.Parse(aux[0])) * int.Parse(item.MGLT));
                    }
                    else if (item.consumables.ToLower().Contains("month"))
                    {
                        //Quantidade de horas em um mes
                        result = distance / ((730 * int.Parse(aux[0])) * int.Parse(item.MGLT));
                    }
                    else if (item.consumables.ToLower().Contains("year"))
                    {
                        //Quantidade de horas em um ano
                        result = distance / ((8760 * int.Parse(aux[0])) * int.Parse(item.MGLT));
                    }
                    item.stops = result;
                }
                else
                {
                    item.stops = -1;
                }
            }
        }