0

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);
        }
    }
}
Cee
  • 1
  • 1
  • Missing a _break_ on _case:103_ However _Console.WriteLine(customer1)_ will print just the class name because your _customerX_ don't provide an override for ToString() method called implicitly by WriteLine – Steve Jul 17 '22 at 20:19
  • Of course, creating three identical classes is really a _no no_ but I suppose that you haven't already studied the _Array_ or _List_ classes – Steve Jul 17 '22 at 20:22
  • 1
    Perhaps you need to declare your class variables (`name`,`id`,`type` ) as public so they can be accessed outside of the class. Then you could call `Console.WriteLine(customer1.name);`. Other than that, it's not the best idea to create a new class for each customer. Instead, you'll want to create a new instance of that class. (Think of it like having the blueprints compared to actually building the object) You could have multiple `Customer` objects, each with their own `name`, `id`, etc. while only having to type out the code for the class structure one time. – Ibrennan208 Jul 17 '22 at 20:22
  • That's not how classes are supposed to work. You certainly do not want to create a new class for each customer. You'll want to have _one_ Customer class and many _instances_ of it. – Fildor Jul 17 '22 at 20:22
  • You need to learn the [difference between a class and an instance](https://stackoverflow.com/questions/2885385/). – Dour High Arch Jul 17 '22 at 21:09

1 Answers1

0

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);
    }
}
Gec
  • 428
  • 2
  • 10