Home > Enterprise >  How delete object from List
How delete object from List

Time:04-26

I fight with this code couple hours. I need an advice. how to remove the entire object by entering the planet name in the console? Next question. How to write the code to inform the user that he gave the wrong information, e.g. in the place of mass he wrote a string instead of int? I will be grateful for a few tips This is my code...

public class Lad
{
    public string PlanetName { get; set; }

    public MyDate Szczegoly { get; set; }
}

public class MyDate
{
    public int masa { get; set; }
    public int sredni { get; set; }

}


class Program
{

    static void RemovePerson(List<Lad> _data, string planet)
    {
        var foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
        if (foundPerson != null)
            _data.Remove(foundPerson);
    }

    static void End()
    {

        Environment.Exit(0);

    }
   
    static void Main()
    {
        
        Console.WriteLine("hej, dokonaj wyboru");
        string Continue;
        List<Lad> _data = new List<Lad>();
        string choice = Console.ReadLine();
        string uppchoice = choice.ToUpper();
        do
        {

            switch (uppchoice)
            {

                case "ADD":
                    Console.WriteLine("Podaj nazwe planety");
                    string name = Console.ReadLine();
                    Console.WriteLine("Podaj mase");
                    int masa = Convert.ToInt16(Console.ReadLine());
                    Console.WriteLine("Podaj srednice");
                    int sred = Convert.ToInt16(Console.ReadLine());
                    _data.Add(new Lad()
                    {
                        PlanetName = name,

                        Szczegoly = new MyDate
                        {
                            masa = masa,
                            sredni = sred

                        }
                    });
                    var json = System.Text.Json.JsonSerializer.Serialize(_data);
                    Console.WriteLine(json);
                    


                    break;
                case "SAVE":
                    string json2 = JsonConvert.SerializeObject(_data, Formatting.Indented);
                    File.AppendAllText(@"tescikkk.json", json2);
                    Console.WriteLine("plik zapisany");
                    break;
                case "LIST":

                    var path = @"tescikkk.json";
                    if (File.Exists(path))
                    {
                        string text = File.ReadAllText(path, System.Text.Encoding.UTF8);
                        Console.WriteLine(text);
                    }

                    break;
                case "DELETE":
                    
                    break;
                case "EXIT":
                        End();
                        break;
            }

            Console.Write("Czy chcesz powtórzyć? (Y/N) : ");
            Continue = Console.ReadLine();
        } while (Continue != "N" && Continue != "n");

    }

   
   
    
}

CodePudding user response:

You need to supply a planet name to be able to delete. You can then return false, if the planet isn't found:

static bool RemovePlanet(List<Lad> _data, string planet)
{
    var foundPlanet = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPlanet != null)
    {
        _data.Remove(foundPlanet);
        return true;
    }
    return false;
}

Then call it like this:

case "DELETE":
    var planetName = Console.ReadLine();
    if (RemovePlanet(_data, planetName)
    {
        Console.WriteLine($"{planetName} was deleted."
    }
    else
    {
        Console.WriteLine($"Sorry, {planetName} wasn't found."
    }

CodePudding user response:

One way to handle the "notify" is by doing this:

static Lad RemovePerson(List<Lad> _data, string planet)
{
    Lad foundPerson = _data.FirstOrDefault(x => x.PlanetName == planet);
    if (foundPerson != null)
    {
        _data.Remove(foundPerson);
    }

    return foundPerson;
}

if you return a non-null object, it was found and removed. null means "not found".

You could also return a boolean.

Java actually does this more often.

See :

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-int-

public E remove(int index)

public boolean remove(Object o)

thus you'd end up with something like:

           case "DELETE":
                
              Lad removeTry = RemovePerson(blah, blah);
              if (null == removeTry)
              {
                 Console.Writeline("Not found");
              }
              else
              {
                Console.Writeline("lad removed");
               }



                break;
  • Related