Create and use an interface Please don\'t waste my time, and actually help me! I
ID: 3595815 • Letter: C
Question
Create and use an interface
Please don't waste my time, and actually help me! I am reallly struggling with this class
This code needs to be done in the C# programming language and needs to be as easy as possible to understand as I am new (please include screen shots of working code if possible)
The instructions for the assignment are below along with the code that will need to be started with.
----------------------------------------------------------------------------------------------------------------
Assignment Instructions
In this exercise, you’ll create an IDisplayable interface and then use it in the InvItem class of the Inventory Maintenance application from exercise 14-1.
1. Open the InventoryMaintenance project in the Projects Chapter 15InventoryMaint With IDisplayable directory.
2. Create a public interface named IDisplayable. Then, add the declaration for a method named GetDisplayText with no parameters and a string return value.
3. Display the code for the InvItem class, and copy the statement in the GetDisplayText method that this class contains. Then, delete this method.
4. Modify the declaration for the class to indicate that it implements the IDisplayable interface. Then, generate a stub for the GetDisplayText method that this interface defines.
5. Modify the declaration for the GetDisplayText method so it can be overridden. Then, replace the generated throw statement with the statement you copied from the original GetDisplayText method in step 3.
6. Test the application to be sure it still works.
--------------------------------------------------------------------------------
The Starting Code
frmInvMaint.cs (Win Form)
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 (Win Form)
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())
{
if (rdoPlant.Checked)
{
invItem = new Plant(Convert.ToInt32(txtItemNo.Text),
txtDescription.Text, Convert.ToDecimal(txtPrice.Text), cboSizeOrManufacturer.SelectedItem.ToString());
}
else
{
invItem = new Supply(Convert.ToInt32(txtItemNo.Text),
txtDescription.Text, Convert.ToDecimal(txtPrice.Text), cboSizeOrManufacturer.SelectedItem.ToString());
}
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
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
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 Item node
if (xmlIn.ReadToDescendant("Item"))
{
// create a Plant or Supply object for each Item 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
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);
}
}
Plant.cs (Inharance)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
public class Plant : InvItem
{
private string size;
public Plant()
{
}
public Plant(int itemNo, string description, decimal price, string size) :
base(itemNo, description, price)
{
this.size = size;
}
public string Size
{
get
{
return size;
}
set
{
size = value;
}
}
public override string GetDisplayText() =>
this.ItemNo + " " + this.Size + " " + this.Description +
" (" + this.Price.ToString("c") + ")";
}
}
Supply.cs (inharance)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
public class Supply : InvItem
{
private string manufacturer;
public Supply()
{
}
public Supply(int itemNo, string description, decimal price, string manufacturer) :
base(itemNo, description, price)
{
this.manufacturer = manufacturer;
}
public string Manufacturer
{
get
{
return manufacturer;
}
set
{
manufacturer = value;
}
}
public override string GetDisplayText() =>
this.ItemNo + " " + this.Manufacturer + " " + this.Description +
" (" + this.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;
}
}
}
---------------------------------------------------------------------
Thank you so much for all of your help.
Let me know if you have any questions.
First one to help gets a thumbs up!
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 ExitExplanation / 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());
}
}
}
------------------------------------
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.btnAddPlant = 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);
//
// btnAddPlant
//
this.btnAddPlant.Location = new System.Drawing.Point(353, 12);
this.btnAddPlant.Name = "btnAddPlant";
this.btnAddPlant.Size = new System.Drawing.Size(104, 24);
this.btnAddPlant.TabIndex = 7;
this.btnAddPlant.Text = "Add Item...";
this.btnAddPlant.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.btnAddPlant);
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 btnAddPlant;
}
}
--------------------------------
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()
{
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())
{
if (rdoPlant.Checked == true)
{
string size = cboSizeOrManufacturer.SelectedItem.ToString();
invItem = new Plant(Convert.ToInt32(txtItemNo.Text),
txtDescription.Text, Convert.ToDecimal(txtPrice.Text), size);
} else if (rdoSupply.Checked == true){
invItem = new Supply(Convert.ToInt32(txtItemNo.Text),
txtDescription.Text, Convert.ToDecimal(txtPrice.Text), cboSizeOrManufacturer.SelectedItem.ToString());
}
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();
}
}
}
-----------------------------------
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.lblSizeOrManufacturer = new System.Windows.Forms.Label();
this.rdoPlant = new System.Windows.Forms.RadioButton();
this.rdoSupply = new System.Windows.Forms.RadioButton();
this.cboSizeOrManufacturer = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(15, 47);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(45, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Item no:";
//
// txtItemNo
//
this.txtItemNo.Location = new System.Drawing.Point(94, 47);
this.txtItemNo.Name = "txtItemNo";
this.txtItemNo.Size = new System.Drawing.Size(76, 20);
this.txtItemNo.TabIndex = 3;
this.txtItemNo.Tag = "Item no";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(15, 77);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(63, 13);
this.label2.TabIndex = 4;
this.label2.Text = "Description:";
//
// txtDescription
//
this.txtDescription.Location = new System.Drawing.Point(94, 73);
this.txtDescription.Name = "txtDescription";
this.txtDescription.Size = new System.Drawing.Size(200, 20);
this.txtDescription.TabIndex = 5;
this.txtDescription.Tag = "Description";
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(94, 168);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 23);
this.btnSave.TabIndex = 10;
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(219, 168);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 11;
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, 130);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(34, 13);
this.label3.TabIndex = 8;
this.label3.Text = "Price:";
//
// txtPrice
//
this.txtPrice.Location = new System.Drawing.Point(95, 127);
this.txtPrice.Name = "txtPrice";
this.txtPrice.Size = new System.Drawing.Size(75, 20);
this.txtPrice.TabIndex = 9;
this.txtPrice.Tag = "Price";
//
// lblSizeOrManufacturer
//
this.lblSizeOrManufacturer.AutoSize = true;
this.lblSizeOrManufacturer.Location = new System.Drawing.Point(15, 103);
this.lblSizeOrManufacturer.Name = "lblSizeOrManufacturer";
this.lblSizeOrManufacturer.Size = new System.Drawing.Size(30, 13);
this.lblSizeOrManufacturer.TabIndex = 6;
this.lblSizeOrManufacturer.Text = "Size:";
//
// rdoPlant
//
this.rdoPlant.AutoSize = true;
this.rdoPlant.Checked = true;
this.rdoPlant.Location = new System.Drawing.Point(94, 13);
this.rdoPlant.Name = "rdoPlant";
this.rdoPlant.Size = new System.Drawing.Size(49, 17);
this.rdoPlant.TabIndex = 0;
this.rdoPlant.TabStop = true;
this.rdoPlant.Text = "Plant";
this.rdoPlant.UseVisualStyleBackColor = true;
this.rdoPlant.CheckedChanged += new System.EventHandler(this.rdoPlant_CheckedChanged);
//
// rdoSupply
//
this.rdoSupply.AutoSize = true;
this.rdoSupply.Location = new System.Drawing.Point(164, 13);
this.rdoSupply.Name = "rdoSupply";
this.rdoSupply.Size = new System.Drawing.Size(57, 17);
this.rdoSupply.TabIndex = 1;
this.rdoSupply.TabStop = true;
this.rdoSupply.Text = "Supply";
this.rdoSupply.UseVisualStyleBackColor = true;
//
// cboSizeOrManufacturer
//
this.cboSizeOrManufacturer.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboSizeOrManufacturer.FormattingEnabled = true;
this.cboSizeOrManufacturer.Location = new System.Drawing.Point(94, 100);
this.cboSizeOrManufacturer.Name = "cboSizeOrManufacturer";
this.cboSizeOrManufacturer.Size = new System.Drawing.Size(121, 21);
this.cboSizeOrManufacturer.TabIndex = 7;
//
// 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, 203);
this.ControlBox = false;
this.Controls.Add(this.cboSizeOrManufacturer);
this.Controls.Add(this.rdoSupply);
this.Controls.Add(this.rdoPlant);
this.Controls.Add(this.lblSizeOrManufacturer);
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;
private System.Windows.Forms.Label lblSizeOrManufacturer;
private System.Windows.Forms.RadioButton rdoPlant;
private System.Windows.Forms.RadioButton rdoSupply;
private System.Windows.Forms.ComboBox cboSizeOrManufacturer;
}
}
---------------------------------
IDisplayable.cs
---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
interface IDisplayable
{
string GetDisplayText();
}
}
----------------------------------
InvItem.cs
-----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
public class InvItem : IDisplayable
{
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()
{
return itemNo + " " + description + " (" + price.ToString("c") + ")";
}
}
}
-----------------------------------
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;
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
-------------------------------
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);
}
}
-------------------------------
Plant.cs
-------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
class Plant : InvItem
{
private string size;
public Plant()
{
}
public Plant(int itemNo, string description, decimal price, string size) : base(itemNo, description, price)
{
this.size = size;
}
public string Size
{
get
{
return size;
}
set
{
size = value;
}
}
public override string GetDisplayText()
{
return base.ItemNo + " " + size + " " + base.Description + " (" + base.Price.ToString("c") + ")";
}
}
}
--------------------------------
Supply.cs
--------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InventoryMaintenance
{
class Supply : InvItem
{
private string manufacturer;
public Supply() {}
public Supply(int itemNo, string description, decimal price, string manufacturer) : base(itemNo, description, price)
{
this.manufacturer = manufacturer;
}
public string Manufacturer
{
get
{
return manufacturer;
}
set
{
manufacturer = value;
}
}
public override string GetDisplayText()
{
return base.ItemNo + " " + manufacturer + " " + base.Description + " (" + base.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;
}
}
}
-----------------------------
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, 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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.