简体   繁体   English

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

[英]I'm trying to use switch case to display data stored in a class

I am trying to create a bank scenario where the worker inputs the customer id and the customer's information is displayed, i've had issues with the class being inaccessible, and now it can't seem to find it at all.我正在尝试创建一个银行场景,其中工作人员输入客户 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);
        }
    }
}

Welcome to StackOverflow!欢迎来到 StackOverflow!

As other people mentioned in the comments, you probably don't want to create a class for each customer, but rather a single Customer class with many instances.正如评论中提到的其他人一样,您可能不想为每个客户创建一个类,而是一个具有许多实例的单个 Customer 类。

I believe you want to achieve something like this:我相信你想要实现这样的目标:

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的逻辑正确吗? 我正在尝试从组合框(下拉列表)获取数据,并使用该数据在清单框中搜索和显示信息 - Is my logic correct? I'm trying to get data from combobox(dropdownlist) and use that data to search and display information in checkedlistbox 我正在尝试使用会话在 .net6 中存储变量,但未存储值 - I'm trying to use sessions to store variables in .net6, but the values are not getting stored 尝试将颜色转换为字符串格式t在切换情况下使用 - Trying to convert color in string format t use in switch case 我正在尝试使用代码在 UI 文本中显示分数,但它没有显示/更改文本 object - I'm trying to use a code to display score in the UI text but it didn't show up/change the text object 避免使用开关盒 - Avoiding use of switch case 我正在尝试在新类中使用dll导入,但是在此声明类型上出现错误属性&#39;DllImport&#39;无效 - I'm trying to use dll import in my new class but getting error Attribute 'DllImport' is not valid on this declaration type 我正在尝试为iOS使用消息模板 - I'm trying to use message template for iOS 我可以在带有 switch case 的 c# 中使用正则表达式吗? - Can I use regex expression in c# with switch case? 在ac#开关中,我可以使用大小写作为默认值吗? - In a c# switch, can I use a case as default? 我可以在案例中使用ENUM代替带有Enums的switch语句吗? - Can I use an ENUM instead of a switch statement with Enums in the CASE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM