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

Use inheritance with the Inventory Maintenance application Can somone please hel

ID: 3591750 • Letter: U

Question

Use inheritance with the Inventory Maintenance application

Can somone please help me with this simple code?

I am new to programming and I do not know how to complete this assignment

The program must be in C# and must be extreamly easy to understand as I am new to programming.

Must be a Form application/ GUI

The directions to the code along witht the starting code are below

--------------------------------------------------------------------------------------------------------------------

In this exercise, you’ll add two classes to the Inventory Maintenance application that inherit the InvItem class. Then, you’ll add code to the forms to provide for these new classes.

Open the InventoryMaintenance project in the Assignments3InventoryMaintenance 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.

Display the InvItem Class and modify the GetDisplayText method so it’s overridable.

Add a class named Plant that inherits the InvItem class. This new class should add a string property named Size. It should also provide a default constructor and a constructor that accepts four parameters (item number, description, price, and size) to initialize the class properties. This constructor should call the base class constructor to initialize the properties defined by that class. Finally, this 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)

Add another class named Supply that inherits the InvItem 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)

Modify the event handler for the Click event of the Save button on New Item form so it creates a new item of the appropriate type using the data entered by the user.

Test the application by adding at least one of each type of inventory item.

-------------------------------------------------------------------------------------------------------------------

And here is the starting code that I need to work with

frmInvMaint.cs (The win form that will display the information)

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, EventArgs e)

{

invItems.Changed += new InvItemList.ChangeHandler(HandleChange);

invItems.Fill();

FillItemListBox();

}

private void FillItemListBox()

{

InvItem item;

lstItems.Items.Clear();

for (int i = 0; i < invItems.Count; i++)

{

item = invItems[i];

lstItems.Items.Add(item.GetDisplayText());

}

}

private void btnAdd_Click(object sender, EventArgs e)

{

frmNewItem newItemForm = new frmNewItem();

InvItem invItem = newItemForm.GetNewItem();

if (invItem != null)

{

invItems += invItem;

}

}

private void btnDelete_Click(object sender, EventArgs e)

{

int i = lstItems.SelectedIndex;

if (i != -1)

{

InvItem invItem = invItems[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 -= invItem;

}

}

}

private void btnExit_Click(object sender, EventArgs e)

{

this.Close();

}

private void HandleChange(InvItemList invItems)

{

invItems.Save();

FillItemListBox();

}

}

}

frmNewItem.cs (The win from that allows the user to enter the new information)

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()
{
LoadComboBox();
this.ShowDialog();
return invItem;
}

private void LoadComboBox()
{
cboSizeOrManufacturer.Items.Clear();
if (rdoPlant.Checked)
{
cboSizeOrManufacturer.Items.Add("1 gallon");
cboSizeOrManufacturer.Items.Add("5 gallon");
cboSizeOrManufacturer.Items.Add("15 gallon");
cboSizeOrManufacturer.Items.Add("24-inch box");
cboSizeOrManufacturer.Items.Add("36-inch box");
}
else
{
cboSizeOrManufacturer.Items.Add("Bayer");
cboSizeOrManufacturer.Items.Add("Jobe's");
cboSizeOrManufacturer.Items.Add("Ortho");
cboSizeOrManufacturer.Items.Add("Roundup");
cboSizeOrManufacturer.Items.Add("Scotts");
}
}

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();
}

private void rdoPlant_CheckedChanged(object sender, EventArgs e)
{
if (rdoPlant.Checked)
{
lblSizeOrManufacturer.Text = "Size:";
}
else
{
lblSizeOrManufacturer.Text = "Manufacturer:";
}
LoadComboBox();
}
}
}

InvItem.cs (class)

sing 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 virtual string GetDisplayText() => itemNo + " " + description + " (" + price.ToString("c") + ")";
}
}

InvItemDB.cs (class)

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;
xmlIn.ReadStartElement("Item");
string type = xmlIn.ReadElementContentAsString();
if (type == "Plant")
{
Plant p = new Plant();
ReadBase(xmlIn, p);
p.Size = xmlIn.ReadElementContentAsString();
item = p;
}
else
{
Supply s = new Supply();
ReadBase(xmlIn, s);
s.Manufacturer = xmlIn.ReadElementContentAsString();
item = s;
}
items.Add(item);
}
while (xmlIn.ReadToNextSibling("Item"));
}
  
// close the XmlReader object
xmlIn.Close();

return items;
}

private static void ReadBase(XmlReader xmlIn, InvItem i)
{
i.ItemNo = xmlIn.ReadElementContentAsInt();
i.Description = xmlIn.ReadElementContentAsString();
i.Price = xmlIn.ReadElementContentAsDecimal();
}
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");
if (item.GetType().Name == "Plant")
{
Plant p = (Plant)item;
xmlOut.WriteElementString("Type", "Plant");
WriteBase(p, xmlOut);
xmlOut.WriteElementString("Size", p.Size);
}
else
{
Supply s = (Supply)item;
xmlOut.WriteElementString("Type", "Supply");
WriteBase(s, xmlOut);
xmlOut.WriteElementString("Manufacturer", s.Manufacturer);
}
xmlOut.WriteEndElement();
}

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

// close the xmlWriter object
xmlOut.Close();
}

private static void WriteBase(InvItem item, XmlWriter xmlOut)
{
xmlOut.WriteElementString("ItemNo", Convert.ToString(item.ItemNo));
xmlOut.WriteElementString("Description", item.Description);
xmlOut.WriteElementString("Price", Convert.ToString(item.Price));
}
}
}

InvItemList.cs (class)

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 delegate void ChangeHandler(InvItemList invItems);
public event ChangeHandler Changed;

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

public int Count => invItems.Count;

public InvItem this[int i]
{
get
{
if (i < 0)
{
throw new ArgumentOutOfRangeException(i.ToString());
}
else if (i > invItems.Count)
{
throw new ArgumentOutOfRangeException(i.ToString());
}
return invItems[i];
}
set
{
invItems[i] = value;
Changed(this);
}
}

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

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

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

public static InvItemList operator +(InvItemList il, InvItem i)
{
il.Add(i);
return il;
}

public static InvItemList operator -(InvItemList il, InvItem i)
{
il.Remove(i);
return il;
}

public void Fill() => invItems = InvItemDB.GetItems();

public void Save() => InvItemDB.SaveItems(invItems);
}
}

Validator.cs (class)

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;

}

}

}

-----------------------------------------------------------------------

Thank you so much for all of your help!

Let me know if you have any questions

Inventory Maintenance 3245649 3762592 9210584 4738459 1 gallon Agapanthus ($7.95) 1 gallon Limonium ($6.95) Ortho Snail pellets ($12.95) 15 gallon Japanese Red Maple (S89.95) Add Item Delete Item Exit

Explanation / Answer

frmInvMaint.cs (The win form that will display the information)
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, EventArgs e)
{
invItems.Changed += new InvItemList.ChangeHandler(HandleChange);
invItems.Fill();
FillItemListBox();
}

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

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

private void btnDelete_Click(object sender, EventArgs e)
{
int i = lstItems.SelectedIndex;
if (i != -1)
{
InvItem invItem = invItems[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 -= invItem;
}
}
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}

private void HandleChange(InvItemList invItems)
{
invItems.Save();
FillItemListBox();
}
}
}
frmNewItem.cs (The win from that allows the user to enter the new information)

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()
{
LoadComboBox();
this.ShowDialog();
return invItem;
}
private void LoadComboBox()
{
cboSizeOrManufacturer.Items.Clear();
if (rdoPlant.Checked)
{
cboSizeOrManufacturer.Items.Add("1 gallon");
cboSizeOrManufacturer.Items.Add("5 gallon");
cboSizeOrManufacturer.Items.Add("15 gallon");
cboSizeOrManufacturer.Items.Add("24-inch box");
cboSizeOrManufacturer.Items.Add("36-inch box");
}
else
{
cboSizeOrManufacturer.Items.Add("Bayer");
cboSizeOrManufacturer.Items.Add("Jobe's");
cboSizeOrManufacturer.Items.Add("Ortho");
cboSizeOrManufacturer.Items.Add("Roundup");
cboSizeOrManufacturer.Items.Add("Scotts");
}
}
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();
}
private void rdoPlant_CheckedChanged(object sender, EventArgs e)
{
if (rdoPlant.Checked)
{
lblSizeOrManufacturer.Text = "Size:";
}
else
{
lblSizeOrManufacturer.Text = "Manufacturer:";
}
LoadComboBox();
}
}
}

InvItem.cs (class)
sing 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 virtual string GetDisplayText() => itemNo + " " + description + " (" + price.ToString("c") + ")";
}
}
InvItemDB.cs (class)

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;
xmlIn.ReadStartElement("Item");
string type = xmlIn.ReadElementContentAsString();
if (type == "Plant")
{
Plant p = new Plant();
ReadBase(xmlIn, p);
p.Size = xmlIn.ReadElementContentAsString();
item = p;
}
else
{
Supply s = new Supply();
ReadBase(xmlIn, s);
s.Manufacturer = xmlIn.ReadElementContentAsString();
item = s;
}
items.Add(item);
}
while (xmlIn.ReadToNextSibling("Item"));
}
  
// close the XmlReader object
xmlIn.Close();
return items;
}
private static void ReadBase(XmlReader xmlIn, InvItem i)
{
i.ItemNo = xmlIn.ReadElementContentAsInt();
i.Description = xmlIn.ReadElementContentAsString();
i.Price = xmlIn.ReadElementContentAsDecimal();
}
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");
if (item.GetType().Name == "Plant")
{
Plant p = (Plant)item;
xmlOut.WriteElementString("Type", "Plant");
WriteBase(p, xmlOut);
xmlOut.WriteElementString("Size", p.Size);
}
else
{
Supply s = (Supply)item;
xmlOut.WriteElementString("Type", "Supply");
WriteBase(s, xmlOut);
xmlOut.WriteElementString("Manufacturer", s.Manufacturer);
}
xmlOut.WriteEndElement();
}
// write the end tag for the root element
xmlOut.WriteEndElement();
// close the xmlWriter object
xmlOut.Close();
}
private static void WriteBase(InvItem item, XmlWriter xmlOut)
{
xmlOut.WriteElementString("ItemNo", Convert.ToString(item.ItemNo));
xmlOut.WriteElementString("Description", item.Description);
xmlOut.WriteElementString("Price", Convert.ToString(item.Price));
}
}
}
InvItemList.cs (class)

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 delegate void ChangeHandler(InvItemList invItems);
public event ChangeHandler Changed;
public InvItemList()
{
invItems = new List<InvItem>();
}
public int Count => invItems.Count;
public InvItem this[int i]
{
get
{
if (i < 0)
{
throw new ArgumentOutOfRangeException(i.ToString());
}
else if (i > invItems.Count)
{
throw new ArgumentOutOfRangeException(i.ToString());
}
return invItems[i];
}
set
{
invItems[i] = value;
Changed(this);
}
}
public void Add(InvItem invItem)
{
invItems.Add(invItem);
Changed(this);
}
public void Add(int itemNo, string description, decimal price)
{
InvItem i = new InvItem(itemNo, description, price);
invItems.Add(i);
Changed(this);
}
public void Remove(InvItem invItem)
{
invItems.Remove(invItem);
Changed(this);
}
public static InvItemList operator +(InvItemList il, InvItem i)
{
il.Add(i);
return il;
}
public static InvItemList operator -(InvItemList il, InvItem i)
{
il.Remove(i);
return il;
}
public void Fill() => invItems = InvItemDB.GetItems();
public void Save() => InvItemDB.SaveItems(invItems);
}
}
Validator.cs (class)
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;
}
}
}