marcel jarasch

marcel jarasch

  • NA
  • 6
  • 681

How to make a app for add, delete list of persons

Apr 22 2022 9:08 PM

I would like to write a program that will handle commands like

add: And asks for your first name, surname, age list: List the people in a directory remove: removes detail: Displays the details of the person open: opens a file. (from json and xml) save: save the file (in json and xml)

I have written methods ... and I have been fighting for a couple of hours what's next.

class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class FileExample
{
    static void Main(string[] args)
    {

        while (true)
        {
            string choice = Console.ReadLine();
            switch (choice)
            {
                case "Add":
                    break;
                case "Update":
                    break;
                case "Remove":
                    break;
                case "Exit":
                    return;
            }
        }
    }

    static List<Person> LoadList()
    {
        string text = File.ReadAllText("persons.txt");
        if (string.IsNullOrEmpty(text))
            return new List<Person>() { };
        return JsonConvert.DeserializeObject<Person[]>(text).ToList();
    }
    static void SaveList(IEnumerable<Person> persons)
    {
        File.WriteAllText("persons.txt", JsonConvert.SerializeObject(persons.ToArray()));
    }
    static void AddPerson(List<Person> list, Person newPerson)
    {
        list.Add(newPerson);
    }
    static void RemovePerson(List<Person> list, int personId)
    {
        var foundPerson = list.FirstOrDefault(x => x.Id == personId);
        if (foundPerson != null)
            list.Remove(foundPerson);
    }
}

 


Answers (1)