我正在尝试使用 switch case 来显示存储在类中的数据

问题描述 投票:0回答:1

我正在尝试创建一个银行场景,其中工作人员输入客户 ID 并显示客户信息,我遇到了该类无法访问的问题,现在似乎根本找不到它。

public class Program
{
    public class customer1
    {
        string name = "Akan Udoh";
        int id = 101;
        String type = "Current";
    }
    public class customer2
    {
        string name = "Clara Udoh";
        int id = 102;
        string type = "Savings";
    }
    public class customer3
    {
        string name = "jane doe";
        int id = 103;
        string type = "Fixed deposit";
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Enter Customer id");
        int id = Convert.ToInt32(Console.ReadLine());

        switch (id)
        {
            case 101:
                customer1 customer1 = new customer1();
                Console.WriteLine(customer1);
                Console.WriteLine(customer1);
                Console.WriteLine(customer1);
                break;
            case 102:
                customer2 customer2 = new customer2();
                Console.WriteLine(customer2);
                Console.WriteLine(customer2);
                Console.WriteLine(customer2);
                break;
            case 103:
                customer3 customer3 = new customer3();
                Console.WriteLine(customer3);
                Console.WriteLine(customer3);
                Console.WriteLine(customer3);
        }
    }
}
c# .net xcode switch-statement case-sensitive
1个回答
0
投票

正如其他人在评论中提到的,您可能不想为每个客户创建一个类,而是创建一个包含许多实例的 Customer 类。

我相信你想要实现这样的目标:

public class Program
{
    public class Customer
    {
        public string Name;
        public int Id;
        public string Type;
    }

    static void Main(string[] args)
    {
        var customers = new Customer[]
        {
            new Customer
            {
                Name = "Akan Udoh",
                Id = 101,
                Type = "Current"
            },
            new Customer
            {
                Name = "Clara Udoh",
                Id = 102,
                Type = "Savings"
            },
            new Customer
            {
                Name = "jane doe",
                Id = 103,
                Type = "Fixed deposit"
            },
        };

        // As an alternative, you could add all customers to a dictionary for a faster search
        // var customerDictionary = new Dictionary<int, Customer>();
        // foreach (Customer cust in customers)
        //     customerDictionary.Add(cust.Id, cust);

        Console.WriteLine("Enter Customer id");
        var id = Convert.ToInt32(Console.ReadLine());

        var customer = customers.Where(x => x.Id == id).FirstOrDefault();

        // If you choose a dictionary, you can find it like this instead of the above Linq query
        // var customer = customerDictionary[id];

        Console.WriteLine(customer.Name);
        Console.WriteLine(customer.Id);
        Console.WriteLine(customer.Type);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.