Ejemplo n.º 1
0
        public override int GetHashCode()
        {
            int hash = 13;

            hash = hash * 7 + Title.GetHashCode();
            hash = hash * 7 + Author.GetHashCode();
            hash = hash * 7 + ISBN.GetHashCode();
            hash = hash * 7 + Publisher.GetHashCode();
            hash = hash * 7 + PublishYear.GetHashCode();
            hash = hash * 7 + Price.GetHashCode();
            hash = hash * 7 + Pages.GetHashCode();

            return(hash);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convering book to string with provided format
        /// </summary>
        public string ToString(string format)
        {
            if (string.IsNullOrEmpty(format))
            {
                format = "F";
            }
            format = format.ToUpper();

            switch (format)
            {
            case "AT":
                return(Author + ", " + Title);

            case "ATPPY":
                return(Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear.ToString());

            case "IAT":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title);

            case "IATPPYPA":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear + ", " +
                       Pages.ToString());

            case "IATPPYPAPR":
                return("ISBN: " + ISBN + ", " +
                       Author + ", " +
                       Title + ", " +
                       Publisher + ", " +
                       PublishYear.ToString() + ", " +
                       Pages.ToString() + ", " +
                       Price.ToString());

            case "F":
                goto case "IATPPYPAPR";

            default:
                log.Warn("Wrong format type");
                throw new FormatException("Invalid format");
            }
        }
Ejemplo n.º 3
0
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (SerialNumber.Text == "" || PublishYear.Text == "" || BookName.Text == "")
            {
                MessageBox.Show("Please enter the required data.");
            }
            else
            {
                bool         b  = false;
                FileStream   fs = new FileStream("Books.txt", FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(fs);
                string[]     fields;
                string[]     records;

                while (sr.Peek() != -1)
                {
                    records = sr.ReadLine().Split('#');
                    for (int i = 0; i < records.Length - 1; i++)
                    {
                        fields = records[i].Split('@');
                        if (fields[1] == BookName.Text)
                        {
                            b = true;
                        }
                    }
                }
                sr.Close();
                fs.Close();
                if (b == false && AutherIDs.SelectedIndex != -1)
                {
                    FileStream   File = new FileStream("Books.txt", FileMode.Append, FileAccess.Write);
                    StreamWriter sw   = new StreamWriter(File);
                    // gets the string value of the combobox
                    string AuthorId = AutherIDs.SelectedItem.ToString().Trim('\0');
                    //writes the book using '@' dilimter only if the auther id is valid
                    sw.Write(SerialNumber.Text);
                    sw.Write('@');
                    sw.Write(BookName.Text);
                    sw.Write('@');
                    sw.Write(PublishYear.Text);
                    sw.Write('@');
                    sw.Write(dic[AuthorId]);
                    sw.Write('#');

                    //resets the text boxes
                    SerialNumber.Clear();
                    BookName.Clear();
                    PublishYear.Clear();
                    AutherIDs.SelectedText = " ";

                    // Confirms the addition to the user
                    MessageBox.Show("Book added sucssefully.");


                    sw.Close();
                    File.Close();
                }
                else
                {
                    MessageBox.Show("Book already exists.");
                }
            }
        }
 public override int GetHashCode()
 {
     return(PublishYear.GetHashCode());
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a string representation of the book, using the specified format.
        /// </summary>
        /// <param name="format">Avaliable formats:
        ///                     "G" - general
        ///                     "S" - short
        ///                     You can enumerate the components of the book you want to get, separeted by ','.
        ///                     In the result the components values will also be separeted by ','.
        ///                     Components shortcuts:
        ///                     "ISBN" - Books ISBN number
        ///                     "AUT" - Books authors
        ///                     "TT" - Books Title
        ///                     "PB" - Books publisher
        ///                     "PBY" - Books publish year
        ///                     "PG" - Books number of pages
        ///                     "PR" - Books Price
        ///                     Example:
        ///                     The general format looks like this "ISBN,AUT,TT,PB,PBY,PG,PR".
        ///                     The short format "AUT,TT".
        ///                     </param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            string defaultFormat = "G";

            if (format == null)
            {
                format = defaultFormat;
            }

            if (formatProvider == null)
            {
                formatProvider = CultureInfo.CurrentCulture;
            }

            format = format.ToUpper();

            string generalFormat =
                "ISBN,AUT,TT,PB,PBY,PG,PR";
            string shortFormat =
                "AUT,TT";

            if (format == "G")
            {
                format = generalFormat;
            }

            if (format == "S")
            {
                format = shortFormat;
            }


            string[] fmtParts = format.Split(',');

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < fmtParts.Length; i++)
            {
                if (i != 0)
                {
                    result.Append(", ");
                }

                switch (fmtParts[i])
                {
                case "ISBN":
                    result.Append(ISBN.ToString());
                    break;

                case "AUT":
                    result.Append(Author.ToString(formatProvider));
                    break;

                case "TT":
                    result.Append(Title.ToString(formatProvider));
                    break;

                case "PB":
                    result.Append(Publisher.ToString(formatProvider));
                    break;

                case "PBY":
                    result.Append(PublishYear.ToString(formatProvider));
                    break;

                case "PG":
                    result.Append($"P.{NumberOfPages.ToString(formatProvider)}");
                    break;

                case "PR":
                    result.Append(Price.ToString(formatProvider));
                    break;

                default:
                    throw new FormatException($"{fmtParts[i]} format is not supported");
                }
            }

            return(result.ToString());
        }