Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

C#
<pre>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);

        }

    }
Posted
Updated 23-Apr-22 23:49pm
Comments
The Other John Ingram 24-Apr-22 17:58pm    
i would move the person into a separate class and list that with methods

1 solution

That code is a bit ... um ... unplanned

You are expecting your user to type full words - which is OK, but limiting - and then without giving them any idea what to do expecting them to type exactly the right words, using exactly the right mix of case: "List" will not match "list" or "LIST" for example.

And you only give them one chance to do it: your loop is useless because it doesn't fetch any more inputs from the user if there is no match, while your switch exits the function - and the whole app - if there is a match.

And if it ever did work, it would immediately fail in production because the EXE folder is write protected when you install properly! See here: Where should I store my data?[^] for some better ideas.

I'd suggest that you start with a clean sheet and plan what to do first: how is the user to interact with your app and design the code around that before you even start making anything work "behind the scenes".

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900