📜  世界人口 - C# (1)

📅  最后修改于: 2023-12-03 15:35:56.231000             🧑  作者: Mango

世界人口 - C#

简介

这个 C# 程序是一个简单的控制台应用程序,它可以显示世界人口的实时统计信息。这个程序调用 World Population API 来获取实际人口数据。

功能
  • 显示世界人口的实时统计信息
  • 提供交互界面,可进行以下操作:
    • 显示总人口和出生率
    • 显示死亡率和迁移率
    • 根据特定地区查询人口统计数据
使用
  1. 克隆或下载项目
  2. 打开控制台并进入项目目录
  3. 执行 dotnet run 命令来运行程序
实现

这个程序使用 .NET Core 和 RestSharp 包来调用 REST API 来获取世界人口数据。以下是代码片段:

using System;
using RestSharp;
using Newtonsoft.Json;

// 定义 WorldPopulation 类,用于反序列化 API 响应数据
public class WorldPopulation
{
    [JsonProperty("population")]
    public long TotalPopulation { get; set; }

    [JsonProperty("births_today")]
    public long BirthsToday { get; set; }

    [JsonProperty("deaths_today")]
    public long DeathsToday { get; set; }

    [JsonProperty("net_population_increase_today")]
    public long NetIncreaseToday { get; set; }

    [JsonProperty("population_yearly_increase")]
    public long YearlyIncrease { get; set; }

    [JsonProperty("population_day_increase")]
    public long DailyIncrease { get; set; }

    [JsonProperty("population_density")]
    public long Density { get; set; }

    [JsonProperty("median_age")]
    public long MedianAge { get; set; }

    [JsonProperty("urban_population")]
    public double UrbanPopulationPercent { get; set; }

    [JsonProperty("world_population")]
    public long WorldPopulationPercent { get; set; }
}

// 定义 WorldPopulationApiClient 类,用于调用 API 并解析响应数据
public class WorldPopulationApiClient
{
    private readonly string _baseUrl;
    private readonly RestClient _restClient;

    public WorldPopulationApiClient(string baseUrl = "https://api.worldometers.info")
    {
        _baseUrl = baseUrl;
        _restClient = new RestClient(_baseUrl);
    }

    public WorldPopulation GetWorldPopulation()
    {
        var request = new RestRequest("/world-population");
        var response = _restClient.Get(request);

        var json = response.Content;
        var worldPopulation = JsonConvert.DeserializeObject<WorldPopulation>(json);

        return worldPopulation;
    }

    public WorldPopulation GetWorldPopulationByCountry(string country)
    {
        var request = new RestRequest($"/population/{country}");
        var response = _restClient.Get(request);

        var json = response.Content;
        var worldPopulation = JsonConvert.DeserializeObject<WorldPopulation>(json);

        return worldPopulation;
    }
}

// 示例程序
public class Program
{
    static void Main(string[] args)
    {
        var client = new WorldPopulationApiClient();

        var worldPopulation = client.GetWorldPopulation();

        Console.WriteLine($"World Population: {worldPopulation.TotalPopulation:n0}");
        Console.WriteLine($"Births Today: {worldPopulation.BirthsToday:n0}");
        Console.WriteLine($"Deaths Today: {worldPopulation.DeathsToday:n0}");
        Console.WriteLine($"Net Increase Today: {worldPopulation.NetIncreaseToday:n0}");
        Console.WriteLine($"Yearly Increase: {worldPopulation.YearlyIncrease:n0}");
        Console.WriteLine($"Daily Increase: {worldPopulation.DailyIncrease:n0}");
        Console.WriteLine($"Density: {worldPopulation.Density:n0}");
        Console.WriteLine($"Median Age: {worldPopulation.MedianAge:n0}");
        Console.WriteLine($"Urban Population: {worldPopulation.UrbanPopulationPercent:n2}%");
        Console.WriteLine($"World Population: {worldPopulation.WorldPopulationPercent:n0}%");
    }
}
结束语

这个 C# 程序是一个简单的但有用的示例,展示了如何使用 .NET Core 和 RestSharp 调用 REST API 并解析响应数据。开发者可以根据自己的喜好和需求来使用它进行改进和扩展。