如何获得客户的名字和姓氏

时间:2016-06-06 01:54:39

标签: c#

我想知道如何更改此代码以获取名字和姓氏。我的朋友和我开发此代码但我需要更改此代码。还有Customer类,我设置了它们的属性。所以寻找改变这个的建议:

//Getting No: Of Customers for user wish to enter data.
do
{
    needToGetInputFromUser = false;
    Console.WriteLine("Please enter customer name");
    customerName = Console.ReadLine();

    if (customerName.Length < 5 || customerName.Length > 20)
    {
        Console.WriteLine("Invalid name length, must be between 5 and 20 characters");
        Console.WriteLine("Please try again.");
        Console.WriteLine(" ");

        needToGetInputFromUser = true;
    }
    else
    {
        isUserEnteredValidInputData = true;
    }

} while (needToGetInputFromUser);
//Getting Account number

3 个答案:

答案 0 :(得分:1)

我的方法与其他所有人略有不同,我首先要求名字,然后是姓氏。这是Customer类:

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return FirstName + " " + LastName; } }
}

基本上我创建了一个Customer对象,然后根据用户输入单独设置FirstNameLastName,如下所示:

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var customer = new Customer();
            customer.FirstName = GetStringValueFromConsole("Customer First Name");
            customer.LastName = GetStringValueFromConsole("Customer Last Name");
            Console.WriteLine("New Customers name: " + customer.FullName);
            Console.WriteLine("Finished");
            Console.ReadLine();
        }

        private static string GetStringValueFromConsole(string valueToAskFor)
        {
            var needToGetInputFromUser = false;
            var stringValue = string.Empty;
            do
            {
                Console.WriteLine("Please enter " + valueToAskFor);
                stringValue = Console.ReadLine();
                if (stringValue.Length < 5 || stringValue.Length > 20)
                {
                    Console.WriteLine("Invalid \"" + valueToAskFor + "\", must be between 5 and 20 characters");
                    Console.WriteLine("Please try again.");
                    Console.WriteLine(" ");
                    needToGetInputFromUser = true;
                }
                else
                {
                    needToGetInputFromUser = false;
                }
            } while (needToGetInputFromUser);
            return stringValue;
        }
    }
}

答案 1 :(得分:0)

List<Customer>  ListOfCustomer = new  List<Customer>  ();
do
{
    needToGetInputFromUser = false;
    Console.WriteLine("Please enter customer name");
    customerName = Console.ReadLine();

    if (customerName.Length < 5 ||   customerName.Length > 20)
    {
        Console.WriteLine("Invalid name length, must be between 5 and 20 characters");
        Console.WriteLine("Please try again.");
        Console.WriteLine(" ");

        needToGetInputFromUser = true;
    }
    else
    {
        Customer c = new Customer();
        c.Name =  customerName;
        ListOfCustomer.Add(c);
        isUserEnteredValidInputData = true;
    }

} while (needToGetInputFromUser);

int CustomerCount = ListOfCustomer.Count;

答案 2 :(得分:0)

我假设你得到一个字符串:

  

Brian Mains

你想在Brian和Mains之间拆分“”,将它存储在Customer对象的名/姓中吗?我想你正在尝试做类似的事情:

Customer c = new Customer();

if (customerName.Contains(" ")) {
   var terms = customerName.Split(' ');
   if (terms.Length == 2) {
      c.FirstName = terms[0];
      c.LastName = terms[1];
   }
   else { 
      //TBD
   }
}