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

34 Extra exercises for Murack\'s Visual Basie 201 Use inheritance with the Inven

ID: 3741015 • Letter: 3

Question

34 Extra exercises for Murack's Visual Basie 201 Use inheritance with the Inventory Maintenance application Extra 18-1 In this exercise, you'll add two classes to the Inventory Maintenance application that inherit the Invltem class. Then, you'll add code to the forms to provide for these new classes New Inventory Hem O Plant Supply 13.35 I. Open the InventoryMaintenance project in the Extra Exercises Chapter 18UnventoryMaintenance directory. Then, review the code for the New Item form to see that the items in the combo box and the label for the combo box depend on which radio button is selected. 2. Display the Invltem Class and modify the GetDisplayText method so it's overridable 3. Add a class named Plant that inherits the Invltem class. This new class should a constructor that accepts four parameters (item number, description, price, and constructor to initialize the properties defined by that class. Finally, this class add a string property named Size. It should also provide a default constructor and size) to initialize the class properties. This constructor should call the base class should override the GetDisplayText method to add the size in front of the description, as in this example: 3245649 1 gallon Agapanthus ($7.95) 4. Add another class named Supply that inherits the Invltem class and adds a string property named Manufacturer. Like the Plant class, the Supply class should provide a default constructor and a constructor that accepts four parameters, and it should override the GetDisplayText method so the manufacturer is added in front of the description like this 9210584 Ortho Snail pellets ($12.95)

Explanation / Answer

Program.cs
----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InventoryMaintenance
{
    static class Program
    {
        ///< summary>
        /// The main entry point for the application.
        ///< /summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmInvMaint());
        }
    }
}
-----------------------------------
frmNewItem.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 InventoryMaintenance
{
    public partial class frmNewItem : Form
    {
        public frmNewItem()
        {
            InitializeComponent();
        }

        private InvItem invItem = null;

        public InvItem GetNewItem()
        {
            this.ShowDialog();
            return invItem;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (IsValidData())
            {
                invItem = new InvItem(Convert.ToInt32(txtItemNo.Text),
                    txtDescription.Text, Convert.ToDecimal(txtPrice.Text));
                this.Close();
            }
        }

        private bool IsValidData()
        {
            return Validator.IsPresent(txtItemNo) &&
                   Validator.IsInt32(txtItemNo) &&
                   Validator.IsPresent(txtDescription) &&
                   Validator.IsPresent(txtPrice) &&
                   Validator.IsDecimal(txtPrice);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
--------------------------------------------------
frmNewItem.designer.cs
------------------------

namespace InventoryMaintenance
{
    partial class frmNewItem
    {
        ///< summary>
        /// Required designer variable.
        ///< /summary>
        private System.ComponentModel.IContainer components = null;

        ///< summary>
        /// Clean up any resources being used.
        ///< /summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        ///< summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///< /summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.txtItemNo = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.txtPrice = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(15, 17);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Item no:";
            //
            // txtItemNo
            //
            this.txtItemNo.Location = new System.Drawing.Point(84, 17);
            this.txtItemNo.Name = "txtItemNo";
            this.txtItemNo.Size = new System.Drawing.Size(76, 20);
            this.txtItemNo.TabIndex = 1;
            this.txtItemNo.Tag = "Item no";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(15, 47);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(63, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Description:";
            //
            // txtDescription
            //
            this.txtDescription.Location = new System.Drawing.Point(84, 43);
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(200, 20);
            this.txtDescription.TabIndex = 3;
            this.txtDescription.Tag = "Description";
            //
            // btnSave
            //
            this.btnSave.Location = new System.Drawing.Point(84, 107);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 6;
            this.btnSave.Text = "Save";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            //
            // btnCancel
            //
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(209, 107);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 7;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(15, 73);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(34, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Price:";
            //
            // txtPrice
            //
            this.txtPrice.Location = new System.Drawing.Point(85, 70);
            this.txtPrice.Name = "txtPrice";
            this.txtPrice.Size = new System.Drawing.Size(75, 20);
            this.txtPrice.TabIndex = 5;
            this.txtPrice.Tag = "Price";
            //
            // frmNewItem
            //
            this.AcceptButton = this.btnSave;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(311, 150);
            this.ControlBox = false;
            this.Controls.Add(this.txtPrice);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtItemNo);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmNewItem";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "New Inventory Item";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtItemNo;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtDescription;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtPrice;
    }
}
---------------------------------------------------
frmInvMaint.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 InventoryMaintenance
{
    public partial class frmInvMaint : Form
   {
       public frmInvMaint()
       {
           InitializeComponent();
       }

       private InvItemList invItems = new InvItemList();

       private void frmInvMaint_Load(object sender, System.EventArgs e)
       {
           invItems.Fill();
           FillItemListBox();
       }

       private void FillItemListBox()
       {
            InvItem item;
           lstItems.Items.Clear();
            for (int i = 0; i < invItems.Count; i++)
            {
                item = invItems.GetItemByIndex(i);
                lstItems.Items.Add(item.GetDisplayText());
            }
       }

       private void btnAdd_Click(object sender, System.EventArgs e)
       {
           frmNewItem newItemForm = new frmNewItem();
           InvItem invItem = newItemForm.GetNewItem();
           if (invItem != null)
           {
               invItems.Add(invItem);
               invItems.Save();
               FillItemListBox();
           }
       }

       private void btnDelete_Click(object sender, System.EventArgs e)
       {
           int i = lstItems.SelectedIndex;
           if (i != -1)
           {
               InvItem invItem = invItems.GetItemByIndex(i);
               string message = "Are you sure you want to delete "
                   + invItem.Description + "?";
               DialogResult button =
                   MessageBox.Show(message, "Confirm Delete",
                   MessageBoxButtons.YesNo);
               if (button == DialogResult.Yes)
               {
                   invItems.Remove(invItem);
                   invItems.Save();
                   FillItemListBox();
               }
           }
       }

       private void btnExit_Click(object sender, EventArgs e)
       {
           this.Close();
       }
   }
}
-----------------------------------
frmInvMaint.designer.cs
-------------
namespace InventoryMaintenance
{
   partial class frmInvMaint
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ComponentModel.IContainer components = null;

       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
       protected override void Dispose(bool disposing)
       {
           if (disposing&& (components != null))
           {
               components.Dispose();
           }
           base.Dispose(disposing);
       }

       #region Windows Form Designer generated code

       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
            this.lstItems = new System.Windows.Forms.ListBox();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnDelete = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // lstItems
            //
            this.lstItems.FormattingEnabled = true;
            this.lstItems.Location = new System.Drawing.Point(12, 12);
            this.lstItems.Name = "lstItems";
            this.lstItems.Size = new System.Drawing.Size(325, 160);
            this.lstItems.TabIndex = 10;
            //
            // btnExit
            //
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(353, 72);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(104, 24);
            this.btnExit.TabIndex = 9;
            this.btnExit.Text = "E&xit";
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // btnDelete
            //
            this.btnDelete.Location = new System.Drawing.Point(353, 42);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(104, 24);
            this.btnDelete.TabIndex = 8;
            this.btnDelete.Text = "Delete Item...";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            //
            // btnAdd
            //
            this.btnAdd.Location = new System.Drawing.Point(353, 12);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(104, 24);
            this.btnAdd.TabIndex = 7;
            this.btnAdd.Text = "Add Item...";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            //
            // frmInvMaint
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnExit;
            this.ClientSize = new System.Drawing.Size(475, 181);
            this.Controls.Add(this.lstItems);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnDelete);
            this.Controls.Add(this.btnAdd);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "frmInvMaint";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Inventory Maintenance";
            this.Load += new System.EventHandler(this.frmInvMaint_Load);
            this.ResumeLayout(false);

       }

       #endregion

       private System.Windows.Forms.ListBox lstItems;
       private System.Windows.Forms.Button btnExit;
       private System.Windows.Forms.Button btnDelete;
       private System.Windows.Forms.Button btnAdd;
   }
}
---------------------------------------
InvItemList.cs
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InventoryMaintenance
{
    public class InvItemList
    {
        private List<InvItem> invItems;

        public InvItemList()
        {
            invItems = new List<InvItem>();
        }

        public int Count
        {
            get
            {
                return invItems.Count;
            }
        }

        public InvItem GetItemByIndex(int i)
        {
            return invItems[i];
        }

        public void Add(InvItem invItem)
        {
            invItems.Add(invItem);
        }

        public void Add(int itemNo, string description, decimal price)
        {
            InvItem i = new InvItem(itemNo, description, price);
            invItems.Add(i);
        }

        public void Remove(InvItem invItem)
        {
            invItems.Remove(invItem);
        }

        public void Fill()
        {
            invItems = InvItemDB.GetItems();
        }

        public void Save()
        {
            InvItemDB.SaveItems(invItems);
        }
    }
}
------------------------------------------------------
InvItemDB.cs
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace InventoryMaintenance
{
    public static class InvItemDB
    {
        private const string Path = @"....InventoryItems.xml";

        public static List<InvItem> GetItems()
        {
            // create the list
            List<InvItem> items = new List<InvItem>();

            // create the XmlReaderSettings object
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;

            // create the XmlReader object
            XmlReader xmlIn = XmlReader.Create(Path, settings);

            // read past all nodes to the first Book node
            if (xmlIn.ReadToDescendant("Item"))
            {
                // create one Product object for each Product node
                do
                {
                    InvItem item = new InvItem();
                    xmlIn.ReadStartElement("Item");
                    item.ItemNo = xmlIn.ReadElementContentAsInt();
                    item.Description = xmlIn.ReadElementContentAsString();
                    item.Price = xmlIn.ReadElementContentAsDecimal();
                    items.Add(item);
                }
                while (xmlIn.ReadToNextSibling("Item"));
            }
          
            // close the XmlReader object
            xmlIn.Close();

            return items;
        }

        public static void SaveItems(List<InvItem> items)
        {
            // create the XmlWriterSettings object
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");

            // create the XmlWriter object
            XmlWriter xmlOut = XmlWriter.Create(Path, settings);

            // write the start of the document
            xmlOut.WriteStartDocument();
            xmlOut.WriteStartElement("Items");

            // write each product object to the xml file
            foreach (InvItem item in items)
            {
                xmlOut.WriteStartElement("Item");
                xmlOut.WriteElementString("ItemNo", Convert.ToString(item.ItemNo));
                xmlOut.WriteElementString("Description", item.Description);
                xmlOut.WriteElementString("Price", Convert.ToString(item.Price));
                xmlOut.WriteEndElement();
            }

            // write the end tag for the root element
            xmlOut.WriteEndElement();

            // close the xmlWriter object
            xmlOut.Close();
        }
    }
}
----------------------------------------------
InvItem.cs
--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InventoryMaintenance
{
    public class InvItem
    {
        private int itemNo;
        private string description;
        private decimal price;

        public InvItem()
        {
        }

        public InvItem(int itemNo, string description, decimal price)
        {
            this.itemNo = itemNo;
            this.description = description;
            this.price = price;
        }

        public int ItemNo
        {
            get
            {
                return itemNo;
            }
            set
            {
                itemNo = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public decimal Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public string GetDisplayText()
        {
            return itemNo + "    " + description + " (" + price.ToString("c") + ")";
        }
    }
}
---------------------------------------------------
Validator.cs
-------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InventoryMaintenance
{
    public static class Validator
   {
       private static string title = "Entry Error";

       public static string Title
       {
           get
           {
               return title;
           }
           set
           {
               title = value;
           }
       }

       public static bool IsPresent(TextBox textBox)
       {
           if (textBox.Text == "")
           {
               MessageBox.Show(textBox.Tag + " is a required field.", Title);
               textBox.Focus();
               return false;
           }
           return true;
       }

        public static bool IsDecimal(TextBox textBox)
        {
            decimal number = 0m;
            if (Decimal.TryParse(textBox.Text, out number))
            {
                return true;
            }
            else
            {
                MessageBox.Show(textBox.Tag + " must be a decimal value.", Title);
                textBox.Focus();
                return false;
            }
        }

        public static bool IsInt32(TextBox textBox)
        {
            int number = 0;
            if (Int32.TryParse(textBox.Text, out number))
            {
                return true;
            }
            else
            {
                MessageBox.Show(textBox.Tag + " must be an integer.", Title);
                textBox.Focus();
                return false;
            }
        }

       public static bool IsWithinRange(TextBox textBox, decimal min, decimal max)
       {
           decimal number = Convert.ToDecimal(textBox.Text);
           if (number < min || number > max)
           {
               MessageBox.Show(textBox.Tag + " must be between " + min
                   + " and " + max + ".", Title);
               textBox.Focus();
               return false;
           }
           return true;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote