Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to save data in list which i input in console.I have 3 function`Show(),Add(),Delete(). When i input 1 it must show me my data's list,when i input 2 it must ask me "Enter new user's data with comma",for example` John,25,then it must be saved in my list, that's to say when i input 1 it must show me my list + new adding data, and when i input 3 it must ask me "Please type the index of Person, whom you want to delete",for example i will input 2 and it must delete second index person, and then when i input 1 it must show me my list without second index person.
But in my code it's not working, after adding or deleting it's not show me that person whom i added or deleted.
Please help me,What's my mistake???????

What I have tried:

class Controller
    {
        public List<Person> all; 
        public void Show()
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            foreach (Person item in all)
            {
                Console.WriteLine(item.name + " " + item.age);
            }
            fs.Close();
        }
        public void Add()
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            
            Console.WriteLine("Enter new user's data with comma");
            string text = Console.ReadLine();
            string[] segments = text.Split(',');
            string a = segments[0];
            int b;
            bool hajoxvec = int.TryParse(segments[1], out b);
            if (!hajoxvec)
            {
                Console.WriteLine("Please try again");
                this.Add();
            }
            else
            {
                all.Add(new Person(a, b));

            }
                foreach (Person item in all)
                {
                    Console.WriteLine(item.name + " " + item.age);
                }
            
            fs.Close();
        }
        public void Delete()
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            Console.WriteLine();
            Console.WriteLine("Please type the index of Person, whom you want to delete");
            int num = int.Parse(Console.ReadLine());
            if (num < 0 || num > 4)
            {
                Console.WriteLine("Please try again");
                this.Delete();
            }
            else
            {
                all.RemoveAt(num);
            }
            foreach (Person item in all)
            {
                Console.WriteLine(item.name + " " + item.age);
            }
            fs.Close();
        }
    }





[Serializable]
    class Person
    {
        public string name;
        public int age;
        public Person(string a, int b)
        {
            this.name = a;
            this.age = b;
        }
    }




static void Main(string[] args)
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            //List<Person> mardik = new List<Person>();
            //mardik.Add(new Person("Valod", 20));
            //mardik.Add(new Person("Petros", 23));
            //mardik.Add(new Person("Poghos", 25));
            //mardik.Add(new Person("Hranush", 22));
            //mardik.Add(new Person("Suren", 18));
            //FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            //BinaryFormatter bf = new BinaryFormatter();
            //bf.Serialize(fs, mardik);
            //fs.Close();
            Controller c = new Controller();
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("Incheq uzum anel:\n1.Show all People\n2.Add new person\n3.Delete any person");
                int x = int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        c.Show();
                        break;
                    case 2:
                        c.Add();
                        break;
                    case 3:
                        c.Delete();
                        break;
                }
            }
        }
Posted
Updated 16-Feb-18 5:22am

1 solution

Is there any point in asking questions, if you are just going to ignore everything we tell you?
How to add data in list to .txt file with switch-case[^]
Certainly, there appears to be little point in answering them...
 
Share this answer
 
Comments
Suren97 16-Feb-18 11:35am    
i couldn't find the answer of my question there :)
Suren97 16-Feb-18 11:37am    
i think it will be a little code, but i can't solve that little problem :(
OriginalGriff 16-Feb-18 11:49am    
You can't solve it because you aren't listening to what you are told.
Go back there, and read what I said yesterday.
How much of that have you actually done?
Suren97 16-Feb-18 14:29pm    
Can you send me any source where i can find the answer of my question?

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