简体   繁体   中英

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.

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM