Question How to write a program that handles commands

marjar94

New member
Joined
Feb 21, 2022
Messages
3
Programming Experience
Beginner
Hi!
i want to write a program that handles commands like:

add: Asks for the name of person, age

list: displays a list of names/surnames in a directory

remove <planet name=""> removes the given person from the list
detail <planet name="">: display person data (given by add)

open <filename> loads the person directory from a file

save <filename> saves the persons list to a file.

reading / writing to a file can be in the selected format - JSON or XML


C#:
class Person
    {

        public string Name { get; set; }
        public string Surname { get; set; }
        public override string ToString()
        {
            return "Imie: " + Name + "Nazwisko: " + Surname;
        }
        public static void Main()
        {
            Console.WriteLine("Dokonaj wyboru");
            string choice = Console.ReadLine();
            string a = Console.ReadLine();
            string b = Console.ReadLine();
            Person person = new Person { Name = " " + a, Surname = " " + b };
            Console.WriteLine(person);
            while (true)

            {

                switch (choice)
                {
                    case "Add":
                        using (StreamWriter sw = File.AppendText(@"logowanie64.txt"))
                        {
                            
                            sw.WriteLine(person);
                        }
                        return;

                    case "Detail":
                        Detail();
                        return;

                    case "List":
                        List();
                        return;

                    case "Remove":
                        removeArtist();
                        return;

                    case "Save":
                        string json = JsonSerializer.Serialize(person);
                        File.WriteAllText(@"path.json", json);
                        return;
                }

            }
        }

        public static void removeArtist()
        {
            Console.Write("Please enter name you want to delete: ");
            string name = Console.ReadLine();
            List<string> lst = File.ReadAllLines(@"logowanie64.txt").Where(arg => !string.IsNullOrWhiteSpace(arg)).ToList();
            lst.RemoveAll(x => x.Split('.')[0].Equals(name));
            File.WriteAllLines(@"logowanie64.txt", lst);
        }

        public static void Detail()
        {

            string[] lines = File.ReadAllLines(@"logowanie64.txt");
            Console.WriteLine("Contents of WriteLines2.txt = ");

            foreach (string line in lines)
            {

                Console.WriteLine("\t" + line);
            }
        }

        public static void List()
        {

            string[] s = File.ReadAllLines(@"logowanie64.txt");

            foreach (string line in s)
                Console.WriteLine(line);

        }

    }
 
Congratulations. I assume that you didn't come here just to inform us of that. Do you have a question about a specific issue you're having in trying to do so?
Yes, my problem...

1.how to write remove method of removing person from txt
2. why the "save" command doesn't save all people checked out earlier
 
That's two problems. One topic per thread a one thread per topic please. In future, please provide a FULL and CLEAR explanation of a specific issue/question, then provide a title that summarises that issue/question.

You didn't explain the problem but it appears that you are serialising a list to JSON. To remove an item, read the JSON file into a list, find the item you want to remove, remove it from the list, then serialise the list again. Please provide information specific to this issue in this thread and ask the other question in another thread.
 

Latest posts

Back
Top Bottom