创建一个应用程序以充当电话簿

问题描述 投票:0回答:1

此问题已提交给我和我的同学,我们遇到了一些麻烦。任何输入都会有所帮助!

创建一个应用程序来模拟电话簿的行为。该应用程序应具有3个类-Person,Address和具有main()方法的Program类。

人员班级将包含联系人的详细信息,例如名,姓等地址类别将包含街道,城市,邮政编码等字段]

在main方法中,编写代码来执行以下操作:

  1. 添加新联系人
  2. 按名字搜索联系人
  3. 按姓氏搜索联系人
  4. 按城市搜索联系人
  5. 删除名字的联系人
  6. 按联系人的名字对列表进行排序
  7. 退出

运行程序时,用户应该看到包含上述选项的菜单。用户应该能够继续执行所需的操作,直到他们选择退出。

c# object collections switch-statement phonebook
1个回答
0
投票

由于您没有写出遇到困难的确切内容,所以我敢于建议您不理解一般概念。我很快为您草绘了您的类和方法。该代码并非伪装成当年的最佳代码,而是为了使您理解需要朝哪个方向移动而编写的。链接到项目:https://dotnetfiddle.net/UupO7M

public class Person
{
    public string _FirstName { get; set; }
    public string _LastName { get; set; }
    public string _PhoneNumber { get; set; }
    public Address _Address { get; set; }
}

public class Address
{
    public string _Street { get; set; }
    public string _City { get; set; }
    public string _Zip { get; set; }
}

public class AddressBook
{
    public List<Person> PersonList = new List<Person>();

    public void AddPerson(Person person)
    {
        PersonList.Add(person);
    }

    public List<Person> SearchByFirstNamePerson(string FirstName)
    {
        return PersonList.Where(d => d._FirstName == FirstName).ToList();
    }

    public List<Person> SearchByLastNamePerson(string LastName)
    {
        return PersonList.Where(d => d._LastName == LastName).ToList();
    }

    public List<Person> SearchByCity(string City)
    {
        return PersonList.Where(d => d._Address._City == City).ToList();
    }

    public void DeleteByFirstName(string FirstName)
    {
        PersonList.Where(d => d._FirstName == FirstName).ToList().ForEach(d=> PersonList.Remove(d));
    }

    public List<Person> SortByFirstName()
    {
        return PersonList.OrderBy(d => d._FirstName).ToList();
    }
}

public static void Main()
{
    AddressBook addressBook = new AddressBook();
    addressBook.AddPerson(new Person() { _FirstName = "Alex", _LastName = "Fox", _PhoneNumber = "+7912345678", _Address = new Address() { _City = "London", _Street = "Angel Lane", _Zip = "123456" } });
    addressBook.AddPerson(new Person() { _FirstName = "Andrey", _LastName = "Big", _PhoneNumber = "+7999999999", _Address = new Address() { _City = "Moscow", _Street = "Lenin", _Zip = "654321" } });
    addressBook.AddPerson(new Person() { _FirstName = "Miki", _LastName = "Snow", _PhoneNumber = "+78888888888", _Address = new Address() { _City = "Berlin", _Street = "BeerHouse", _Zip = "121212" } });

    var SearchByFirstNamePersonItems = addressBook.SearchByFirstNamePerson("Alex");
    var SearchByLastNamePersonItems = addressBook.SearchByLastNamePerson("Fox");
    var SearchByCity = addressBook.SearchByCity("London");
    addressBook.DeleteByFirstName("Alex");
    var SortByFirstNameList = addressBook.SortByFirstName();    
}
© www.soinside.com 2019 - 2024. All rights reserved.