Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will extend the Library application that you developed f

ID: 3706245 • Letter: I

Question

In this assignment, you will extend the Library application that you developed for Assignment 3.

Add a subclass of Book class called TextBook with an integer type attribute called edition.

Add another subclass of Book class called GeneralBook with no extra attributes.

Add a protected attribute called dueDate of type DateTime in the Book class and make the Customer c attribute of Book class as protected.

Add the following public abstract method to the Book class: DateTime findDueDate()

Change the CheckOut() method in the Book class so that it not only sets the Customer c attribute but also sets the dueDate attribute to the value returned by the findDueDate() method.

Override the findDueDate() method in the TextBook class so that it computes and returns a date 30 days from the date of checkout. Override the findDueDate() method in the GeneralBook class so that it computes and returns a date 7 days from the date of checkout. Refer to the DateTime class documentation on MSDN website to see how to achieve this.

Change the ToString() method in the Book class so that it also returns the dueDate information in the format shown in the sample output.

Override the ToString() method in the TextBook class so that it also returns the edition information.

Add an interface called IRenewable to the project with a method: bool Renew().

Make the TextBook class implement IRenewable to allow a textbook to be renewed so that it is made due 15 days beyond the current due date. If the book is not checked out, the renew() should return false.

Change the AddNewBook(..) method in the Library class so that it creates a GeneralBook object instead of Book object. Everything else remains the same.

This method is supposed to calculate and return the due date of a book when it is checked out.

12. Add the following public methods in the Library class:

public bool AddNewBook(string bTitle, string bAuthor, int edition): if the number of

books already added is less than the size of the bookArray, this method will create a new TextBook object with the given title, author, edition, and unique catalogNumber, and add it to the bookArray. This method will return true if the textbook was successfully added else it will return false.

bool IssueBook(int custId, int bookCatalogNum): which will issue the book with catalogNumber = bookCatalogNum to the customer with id = custId if the book was not already issued to somebody else and return true; otherwise it will return false. This method should call the CheckOut( ) method of the Book class.

bool ReturnBook(int bookCatalogNum): which will return the book with catalogNumber = bookCatalogNum to the library if the book was in checked-out status and return true; otherwise it will return false. This method should call the CheckIn( ) method of the Book class.

boolean RenewBook(int bookCatalogNum): Returns a false if the product with the given catalog number is not renewable or if the product with the given catalog number is not checked out. Otherwise renews the product and returns true.

13. Use the Main method given in the attached file Program.cs. The sample output for this Main method is given below. Note that the TA may change the Main method when he is testing your code.

Important

DO NOT MAKE any attribute public.

DO NOT ADD any attributes other than what has been asked for.

DO NOT ADD any extra public methods or properties.

You may add private methods if you like to make your code modular.

DO NOT DUPLICATE code.

THERE SHOULD BE NO PRINT STATEMENTS IN ANY OTHER CLASS BUT

THE Main CLASS.

Explanation / Answer

Book.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A5_JyothiLokesh
{
    abstract class Book
    {
        private int catalogNumber;
        private string title;
        private string authors;
        protected Customer c;
        protected DateTime dueDate;

        public abstract DateTime FindDueDate();

        public int CatalogNumber
        {
            get { return catalogNumber; }
        }

        public Book(string title, string authors, int catalogNo)
        {
            this.title = title;
            this.authors = authors;
            this.catalogNumber = catalogNo;
        }

        public bool CheckOut(Customer c)
        {
            if (this.c == null)
            {
                this.c = c;
                this.dueDate = FindDueDate();
                return true;
            }
            return false;
        }

        public bool CheckIn()
        {
            if (this.c != null)
            {
                c = null;
                return true;
            }
            return false;
        }

        public override string ToString()
        {
            string s = String.Format("{0} {1}, {2}   ", catalogNumber, title, authors);
            if (this.c != null)
            {
                s += String.Format("{0}{1} {2} {3}", " Checked out to Customer ", c.Id," Due on ", dueDate);
            }
            else
            {
                s += String.Format("{0}"," Available");
            }
            return s;
        }

    }
}


Customer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A5_JyothiLokesh
{
    class Customer
    {
        private int id;
        private string name;

        public int Id
        {
            get { return id; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Customer(string name, int id)
        {
            this.id = id; this.name = name;
        }

        public override string ToString()
        {
            return String.Format("{0} {1}", id, name);
        }
    }
}


Form1.Designer.cs

namespace A5_JyothiLokesh
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.listBox2 = new System.Windows.Forms.ListBox();
            this.label5 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            this.radioButton1.AutoSize = true;
            this.radioButton1.Checked = true;
            this.radioButton1.Location = new System.Drawing.Point(29, 20);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(69, 17);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "Customer";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(172, 20);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(90, 17);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.Text = "General Book";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            //
            // radioButton3
            //
            this.radioButton3.AutoSize = true;
            this.radioButton3.Location = new System.Drawing.Point(317, 20);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(74, 17);
            this.radioButton3.TabIndex = 2;
            this.radioButton3.Text = "Text Book";
            this.radioButton3.UseVisualStyleBackColor = true;
            this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
            this.label1.Location = new System.Drawing.Point(-2, 63);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(100, 23);
            this.label1.TabIndex = 3;
            this.label1.Text = "Customer Name";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            this.label1.Click += new System.EventHandler(this.label1_Click);
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(26, 105);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(38, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "Author";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(26, 148);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(39, 13);
            this.label3.TabIndex = 5;
            this.label3.Text = "Edition";
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(101, 60);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(320, 20);
            this.textBox1.TabIndex = 6;
            //
            // textBox2
            //
            this.textBox2.Enabled = false;
            this.textBox2.Location = new System.Drawing.Point(101, 102);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(320, 20);
            this.textBox2.TabIndex = 7;
            //
            // textBox3
            //
            this.textBox3.Enabled = false;
            this.textBox3.Location = new System.Drawing.Point(101, 141);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(320, 20);
            this.textBox3.TabIndex = 8;
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(26, 209);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(84, 17);
            this.label4.TabIndex = 9;
            this.label4.Text = "Customers";
            //
            // listBox1
            //
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(26, 234);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(220, 134);
            this.listBox1.TabIndex = 10;
            //
            // listBox2
            //
            this.listBox2.FormattingEnabled = true;
            this.listBox2.Location = new System.Drawing.Point(267, 234);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(510, 134);
            this.listBox2.TabIndex = 11;
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(264, 209);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(52, 17);
            this.label5.TabIndex = 12;
            this.label5.Text = "Books";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(617, 35);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 13;
            this.button1.Text = "Add";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(617, 78);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 14;
            this.button2.Text = "Issue";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(617, 165);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 15;
            this.button3.Text = "Renew";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(617, 120);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 16;
            this.button4.Text = "Return Book";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(789, 391);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.listBox2);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.radioButton3);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.radioButton1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.ListBox listBox2;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
    }
}


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A5_JyothiLokesh
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Library l = new Library();
        private string[] custList = new string[5];
        private string[] bookList = new string[5];
        int count_cust = 0, count_book = 0;

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label1.Text = "Customer Name";
                textBox3.Enabled = false;
                textBox2.Enabled = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                label1.Text = "Title";
                textBox3.Enabled = false;
                textBox2.Enabled = true;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                label1.Text = "Title";
                textBox3.Enabled = true;
                textBox2.Enabled = true;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = listBox1.SelectedIndex;
            int j = listBox2.SelectedIndex;
            if (i < 0)
            {
                MessageBox.Show("You have to select a customer");
            }
            else if (j < 0)
            {
                MessageBox.Show("You have to select a book");
            }
            else
            {
                int id = i + 1;
                int catNum = j + 1 + 100;
                if (l.IssueBook(id, catNum))
                {
                    listBox2.Items.RemoveAt(j);
                    bookList = l.GetBookList();
                    listBox2.Items.Insert(j, bookList[j]);
                }
                else
                {
                    MessageBox.Show("This book is already checked out.");
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = listBox2.SelectedIndex;
            if (i < 0)
            {
                MessageBox.Show("You have to select a book");
            }
            else
            {
                int catNum = 100 + i + 1;
                if (l.RenewBook(catNum))
                {
                    listBox2.Items.RemoveAt(i);
                    bookList = l.GetBookList();
                    listBox2.Items.Insert(i, bookList[i]);
                }
                else
                {
                    MessageBox.Show("This book cannot be renewed.");
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i = listBox2.SelectedIndex;
            if (i < 0)
            {
                MessageBox.Show("You have to select a book");
            }
            else
            {
                int catNum = i + 100 + 1;
                if (l.ReturnBook(catNum))
                {
                    listBox2.Items.RemoveAt(i);
                    bookList = l.GetBookList();
                    listBox2.Items.Insert(i, bookList[i]);
                }
                else
                {
                    MessageBox.Show("This book is not checked out");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("The information provided is incomplete.");
                }
                else if (count_cust == custList.Length)
                {
                    MessageBox.Show("Cannot add more customers.");
                }

                else if (l.AddNewCustomer(textBox1.Text))
                {
                    custList = l.GetCustomerList();
                    listBox1.Items.Add(custList[count_cust]);
                    count_cust++;
                }
            }

            else if (radioButton2.Checked == true)
            {
                if (textBox1.Text == "" || textBox2.Text == "")
                {
                    MessageBox.Show("The information provided is incomplete.");
                }
                else if (count_book == bookList.Length)
                {
                    MessageBox.Show("Cannot add more books");
                }
                else if (l.AddNewBook(textBox1.Text, textBox2.Text))
                {
                    bookList = l.GetBookList();
                    listBox2.Items.Add(bookList[count_book]);
                    count_book++;
                }

            }

            else if (radioButton3.Checked == true)
            {
                if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                {
                    MessageBox.Show("The information provided is incomplete.");
                }
                else if (count_book == bookList.Length)
                {
                    MessageBox.Show("Cannot add more books");
                }
                else
                {
                    if (l.AddNewBook(textBox1.Text, textBox2.Text, int.Parse(textBox3.Text)))
                    {
                        bookList = l.GetBookList();
                        listBox2.Items.Add(bookList[count_book]);
                        count_book++;
                    }
                }
            }
        }
    }
}

GeneralBook.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A5_JyothiLokesh
{
    class GeneralBook : Book
    {
        public GeneralBook(string title, string authors, int catalogNo) : base(title, authors, catalogNo)
        {
         
        }

        public override DateTime FindDueDate()
        {
            DateTime today = DateTime.Now;
            today = today.AddDays(7);
            return today;
        }

      
    }
}

IRenewable.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A5_JyothiLokesh
{
    interface IRenewable
    {
         bool Renew();
    }
}

Library.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A5_JyothiLokesh
{
    class Library
    {

        private Customer[] customerArray = new Customer[5];
        private Book[] bookArray = new Book[5];

        public bool AddNewCustomer(string customerName)
        {
            for (int i = 0; i < customerArray.Length; i++)
            {
                if (customerArray[i] == null)
                {
                    customerArray[i] = new Customer(customerName, ++i);
                    return true;
                }
            }
            return false;
        }

        public bool AddNewBook(string bookTitle, string bookAuthor)
        {
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] == null)
                {
                    int id = 100 + i + 1;
                    bookArray[i] = new GeneralBook(bookTitle, bookAuthor, id);
                    return true;
                }
            }
            return false;
        }

        public bool AddNewBook(string bTitle, string bAuthor, int edition)
        {
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] == null)
                {
                    int id = 100 + i + 1;
                    bookArray[i] = new TextBook(bTitle, bAuthor, edition, id);
                    return true;
                }
            }
            return false;
        }

        public bool IssueBook(int custID, int bookCatalogNum)
        {
            int temp = 0, temp1 = 0;
            bool b = false, c = false;
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] != null)
                    if (bookArray[i].CatalogNumber == bookCatalogNum)
                    {
                        temp = i;
                        b = true;
                    }
            }
            for (int i = 0; i < customerArray.Length; i++)
            {
                if (customerArray[i] != null)
                    if (customerArray[i].Id == custID)
                    {
                        temp1 = i;
                        c = true;
                    }
            }

            if (b && c)
                return bookArray[temp].CheckOut(customerArray[temp1]);
            else
                return false;

        }

        public bool RenewBook(int bookCatalogNum)
        {
       
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] != null)
                    if (bookArray[i].CatalogNumber == bookCatalogNum && bookArray[i] is IRenewable)
                    {
                        return ((IRenewable)bookArray[i]).Renew();
                    }
            }
            return false;

        }

        public bool ReturnBook(int bookCatalogNum)
        {
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] != null)
                    if (bookArray[i].CatalogNumber == bookCatalogNum)
                    {
                        return bookArray[i].CheckIn();
                    }
            }

                return false;

        }

        public string[] GetCustomerList()
        {
            string[] s = new string[customerArray.Length];
            for (int i = 0; i < s.Length; i++)
            {
                if (customerArray[i] != null && s[i] == null)
                    s[i] = customerArray[i].ToString();
            }
            return s;
        }

        public string[] GetBookList()
        {
            string[] s = new string[bookArray.Length];
            for (int i = 0; i < bookArray.Length; i++)
            {
                if (bookArray[i] != null && s[i]==null)
                    s[i]= bookArray[i].ToString();
            }
            return s;
        }
    }
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A5_JyothiLokesh
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}