[英]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.