91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

利用C# Map集合解決實際應用問題的案例

c#
小樊
93
2024-09-04 16:00:06
欄目: 編程語言

在這個案例中,我們將使用C#的Dictionary集合(它是一個鍵值對集合,類似于其他編程語言中的Map)來存儲員工的ID和他們的薪水。我們將創建一個簡單的控制臺應用程序,用于添加員工、顯示員工薪水以及更新員工薪水。

首先,我們需要創建一個Employee類來存儲員工的信息:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Salary { get; set; }
}

接下來,我們將創建一個EmployeeManager類來處理員工的添加、顯示和更新操作:

using System;
using System.Collections.Generic;

public class EmployeeManager
{
    private Dictionary<int, Employee> _employees = new Dictionary<int, Employee>();

    public void AddEmployee(Employee employee)
    {
        if (!_employees.ContainsKey(employee.Id))
        {
            _employees.Add(employee.Id, employee);
        }
        else
        {
            Console.WriteLine("Employee with this ID already exists.");
        }
    }

    public void DisplayEmployeeSalary(int employeeId)
    {
        if (_employees.ContainsKey(employeeId))
        {
            Employee employee = _employees[employeeId];
            Console.WriteLine($"Employee {employee.Name} has a salary of {employee.Salary}.");
        }
        else
        {
            Console.WriteLine("Employee not found.");
        }
    }

    public void UpdateEmployeeSalary(int employeeId, decimal newSalary)
    {
        if (_employees.ContainsKey(employeeId))
        {
            Employee employee = _employees[employeeId];
            employee.Salary = newSalary;
            Console.WriteLine($"Employee {employee.Name}'s salary has been updated to {employee.Salary}.");
        }
        else
        {
            Console.WriteLine("Employee not found.");
        }
    }
}

最后,我們將在Main方法中使用EmployeeManager類來處理用戶輸入:

using System;

class Program
{
    static void Main(string[] args)
    {
        EmployeeManager manager = new EmployeeManager();

        while (true)
        {
            Console.WriteLine("1. Add Employee");
            Console.WriteLine("2. Display Employee Salary");
            Console.WriteLine("3. Update Employee Salary");
            Console.WriteLine("4. Exit");
            Console.Write("Enter your choice: ");
            int choice = Convert.ToInt32(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.Write("Enter employee ID: ");
                    int id = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter employee name: ");
                    string name = Console.ReadLine();
                    Console.Write("Enter employee salary: ");
                    decimal salary = Convert.ToDecimal(Console.ReadLine());

                    Employee employee = new Employee { Id = id, Name = name, Salary = salary };
                    manager.AddEmployee(employee);
                    break;

                case 2:
                    Console.Write("Enter employee ID: ");
                    int displayId = Convert.ToInt32(Console.ReadLine());
                    manager.DisplayEmployeeSalary(displayId);
                    break;

                case 3:
                    Console.Write("Enter employee ID: ");
                    int updateId = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter new salary: ");
                    decimal newSalary = Convert.ToDecimal(Console.ReadLine());
                    manager.UpdateEmployeeSalary(updateId, newSalary);
                    break;

                case 4:
                    Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("Invalid choice. Please try again.");
                    break;
            }
        }
    }
}

現在,當你運行這個程序時,你可以添加員工、顯示員工薪水以及更新員工薪水。這個簡單的例子展示了如何使用C#的Dictionary集合(類似于其他編程語言中的Map)來解決實際應用問題。

0
姚安县| 建瓯市| 微山县| 墨竹工卡县| 石门县| 苍南县| 孟连| 宁陵县| 饶河县| 永宁县| 资溪县| 鞍山市| 合江县| 岐山县| 菏泽市| 龙州县| 碌曲县| 临安市| 喀什市| 星座| 嵊州市| 荣昌县| 聂荣县| 万全县| 嘉兴市| 会昌县| 乌兰浩特市| 石渠县| 佛冈县| 南召县| 宁武县| 乡宁县| 忻州市| 德钦县| 高安市| 五常市| 大方县| 天柱县| 广饶县| 前郭尔| 长垣县|