dimanche 15 avril 2018

Visual Studio C# generic hash table dictionary wrong if statements?

I am creating a GUI in Visual Studio where a Dictionary collection will be created. This will contain instances of book classes which will contain the string members Title, ISBN and a Boolean variable called Onloan. these members will be entered by the user and submitted into the dictionary. The user can also search books they have entered and change their loan status.

I want the program to be able to search for books even if the user only enters the ISBN or the Title, currently it only works when both a Title and ISBN are searched for together. if the user only searches for an ISBN, the Title in the search results text box is blank and if the user only searches the Title the ISBN search results text box states "temp" which is the temporary value I used in a temporary book I used to search.

I've have been changing the program for some time trying to figure out why this happens. I'm wondering if someone can explain it. Maybe I have written the if statements incorrectly.

Book Class

class Books
    {
        private String isbn;
        private string title;
        private Boolean onloan;

        public Books(string isbn, string title)
        {
            this.isbn = isbn;
            onloan = false;
        }
        public string ISBN
        {
            get { return this.isbn; }
            set { this.isbn = value; }
        }
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }
        public Boolean Onloan
        {
            get { return this.onloan; }
            set { this.onloan = value; }
        }
    }

Form:

public partial class Form1 : Form

     {


       Dictionary<string, Books> Library = new Dictionary<string, Books>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)   //submit button
        {
            if ((String.IsNullOrWhiteSpace(TitleBox.Text) == false && String.IsNullOrWhiteSpace(ISBNBox.Text) == false) || 
                (String.IsNullOrEmpty(TitleBox.Text) == false && String.IsNullOrEmpty(ISBNBox.Text) == false))
            {
                Books LibBook = new Books(ISBNBox.Text, TitleBox.Text);
                if(LoanRadial.Checked == true)
                    {
                    LibBook.Onloan = true;
                    LoanRadial.Checked = false;
                    }

                Library.Add(ISBNBox.Text, LibBook);
                TitleBox.Clear();
                ISBNBox.Clear();
                //int count = Library.Count();
                //ISBNBox.Text = count.ToString();

            }
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            Library.Remove(ISBNBox.Text);
            TitleBox.Clear();
            ISBNBox.Clear();
            if (LoanRadial.Checked == true)
            {
                LoanRadial.Checked = false;
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            string tempstring = "temp";
            Books tempbook = new Books(tempstring, tempstring);
            if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == false && String.IsNullOrWhiteSpace(TitleBox.Text) == true) ||
                (String.IsNullOrEmpty(ISBNBox.Text) == false && String.IsNullOrEmpty(TitleBox.Text) == true))
            {

                tempbook.ISBN = ISBNBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.ISBN == element.Value.ISBN)
                    {
                        tempbook.Title = element.Value.Title;
                        tempbook.Onloan = element.Value.Onloan;
                    }
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }

            }
            else if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == true && String.IsNullOrWhiteSpace(TitleBox.Text) == false) ||
                (String.IsNullOrEmpty(ISBNBox.Text) == true && String.IsNullOrEmpty(TitleBox.Text) == false))
            {
                tempbook.Title = TitleBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.Title == element.Value.Title)
                    {

                        tempbook.ISBN = element.Value.ISBN;
                        tempbook.Onloan = element.Value.Onloan;
                    }
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }
            }
            else if ((String.IsNullOrWhiteSpace(ISBNBox.Text) == false && String.IsNullOrWhiteSpace(TitleBox.Text) == false) ||
                    (String.IsNullOrEmpty(ISBNBox.Text) == false && String.IsNullOrEmpty(TitleBox.Text) == false))
            {

                tempbook.ISBN = ISBNBox.Text;
                tempbook.Title = TitleBox.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if ((tempbook.Title == element.Value.Title) &&  (tempbook.ISBN == element.Value.ISBN) )
                    {
                        tempbook.Onloan = element.Value.Onloan;
                    }                   
                }
                Titlebox2.Text = tempbook.Title;
                ISBN2.Text = tempbook.ISBN;
                if (tempbook.Onloan == true)
                {
                    LoanBox.Text = tempbook.Title + " Is on loan";
                }
                else
                {
                    LoanBox.Text = tempbook.Title + " Is not on loan";
                }

            }
        }

        private void Changeloan_Click(object sender, EventArgs e)
        {
            string tempstring = "temp";
            Books tempbook = new Books(tempstring, tempstring);
            if ((String.IsNullOrWhiteSpace(ISBN2.Text) == false && String.IsNullOrWhiteSpace(Titlebox2.Text) == false) ||
                (String.IsNullOrEmpty(ISBN2.Text) == false && String.IsNullOrEmpty(Titlebox2.Text) == false))
            {
                tempbook.ISBN = ISBN2.Text;
                tempbook.Title = Titlebox2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if ((tempbook.ISBN == element.Value.ISBN) && (tempbook.Title == element.Value.Title))
                    {

                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = tempbook.Title + " Is not on loan";
                        }
                        else
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = tempbook.Title + " Is not on loan";
                        }
                    }
                }
            }                  
            else if ((String.IsNullOrWhiteSpace(ISBN2.Text) == true && String.IsNullOrWhiteSpace(Titlebox2.Text) == false) ||
                (String.IsNullOrEmpty(ISBN2.Text) == true) && (String.IsNullOrEmpty(Titlebox2.Text) == false))
            {
                tempbook.Title = Titlebox2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if(tempbook.Title == element.Value.Title)
                    {
                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = element.Value.Title + " Is not on loan";

                        }
                        else if (element.Value.Onloan == false)
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = element.Value.Title + " Is on loan";
                        }
                    }
                }

                    }
            else if((String.IsNullOrWhiteSpace(ISBN2.Text) == false) && (String.IsNullOrWhiteSpace(Titlebox2.Text) == true) || 
                (String.IsNullOrEmpty(ISBN2.Text) == false) && (String.IsNullOrEmpty(Titlebox2.Text) == true))
            {
                tempbook.ISBN = ISBN2.Text;
                foreach (KeyValuePair<string, Books> element in Library)
                {
                    if (tempbook.ISBN == element.Value.ISBN)
                    {
                        if (element.Value.Onloan == true)
                        {
                            element.Value.Onloan = false;
                            LoanBox.Text = element.Value.Title + " Is not on loan";

                        }
                        else if (element.Value.Onloan == false)
                        {
                            element.Value.Onloan = true;
                            LoanBox.Text = element.Value.Title + " Is on loan";
                        }
                    }
                }
            }

            }
        }

Books form picture

Aucun commentaire:

Enregistrer un commentaire