0

So I'm working on a library management system and I want to be able to delete both authors and book titles. By removing the author the program would remove every book by that author meanwhile the book title would only remove that specific book. However, I am kind of struggling as to how I would do this. Any help would be appreciated. Btw, "namn" = title and "författare" = author.

    class Bok
    {
        public string ID
        { get; set; }
        public int tempID;
        public string Författare
        { get; set; }
        public string namn
        { get; set; }

        public int BokCount;
        public int x;
    }
    class Program
    {
        static List<Bok> BokLista = new List<Bok>();
        static List<BorrowDetails> borrowList = new List<BorrowDetails>();
        static Bok Bok = new Bok();
        static BorrowDetails borrow = new BorrowDetails();

        //Menyn och startsidan till programmet
        static void Main(string[] args)
        {
            using (StreamReader readFile = new StreamReader("bokfil.txt"))
            {
                string s;
                while ((s = readFile.ReadLine()) != null)
                {
                    Bok Bok = new Bok();
                    string[] BokData = s.Split(',');
                    Bok.Författare = BokData[0];
                    Bok.namn = BokData[1];
                    BokLista.Add(Bok);
                }
                readFile.Close();
            }
        public static void RaderaBok()
        {
            Console.Write("Mata in en författare eller boktitel som du vill radera: ");
            string del = Console.ReadLine();
            foreach (Bok b in BokLista)
            {
                if (b.Författare.Equals(del))
                    BokLista.Remove(Bok);
            }
        }
Zeorth
  • 21
  • 4
  • Where in that code are you removing anything from an array? A list is not an array. It's also hard to follow the various names and strings that aren't in English. Can you translate, or post on a different language site? – Zer0 Jan 31 '21 at 21:26
  • I think what you are looking for and you could certainly benefit from is using [Linq](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/). – Rno Jan 31 '21 at 23:01

1 Answers1

1

First of all, I'm assuming the library management system you are working on is a learning project. If it is intended for production, stop now. You Bok class is badly designed.

I am assuming you are aware of that, so let's continue. What you want to achieve is something that is very suitable for LINQ. The following very crude code illustrates how it can make your life easier.

void Main()
{
    List<Bok> l = GetListOfBoks();
    Console.WriteLine($"There are {l.Count()} books in total.");
    Console.WriteLine("Mata in en författare eller boktitel som du vill radera: ");
    string del = Console.ReadLine();
    // get the Bok objects that match del
    // This is the line that is important. 
    // the Where operator is a LINQ thing.
    // calling Tolist() will create the list immediately, which is what we want
    // otherwise we run into the problem that we are manipulating a list while iterating it
    List<Bok> delBoks = l.Where(w => w.Författare.Equals(del)).ToList();
    // delBoks will now be a list of Bok objects that match del 
    Console.WriteLine($"There are {delBoks.Count()} books that match the query {del}. ");
    // remove them from Bok list l
    foreach (var b in delBoks)
    {
        l.Remove(b);
    }
    Console.WriteLine($"We now have {l.Count()} books left over.");
}

List<Bok> GetListOfBoks() {
    // create a few books becaus I don't have a text file
    List<Bok> rv = new List<Bok>();
    rv.Add(new Bok() { ID="0", Författare="Paul", namn="Bible" });
    rv.Add(new Bok() { ID="1", Författare="Zeorth", namn="SO Question" });
    rv.Add(new Bok() { ID="2", Författare="Arno", namn="SO Answer" });
    rv.Add(new Bok() { ID="3", Författare="Microsoft", namn="Linq is handy" });
    return rv;
}

class Bok
{
    public string ID
    { get; set; }
    public int tempID;
    public string Författare
    { get; set; }
    public string namn
    { get; set; }
    public int BokCount;
    public int x;
}

LINQ uses lamba expressions (the => bit) which can be very confusing at first. My way of learning about them was to work first by example (like this one) and then read up on how they actually work.

What I am trying to illustrate is that this example removes books by author, but it can very easily be made into removing by title.

Rno
  • 784
  • 1
  • 6
  • 16