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

Create and use an interface Please don\'t waste my time, and actually help me! I

ID: 3599510 • 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 program needs to be a GUI

The instructions for the assignment are below along with the code that will need to be started with

I have started it a little I just need help finishing it

Thanks

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

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.

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

From1.cs (Form 1)

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 EX1_Create_and_use_an_interface
{
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 (From 2)

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


}
}

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

IDisplayable.cs (Interface)

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

namespace EX1_Create_and_use_an_interface
{
interface IDisplayable
{
string GetDisplayText(string sep);
}
}

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

InvItem.cs (class)

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

namespace EX1_Create_and_use_an_interface
{
public class InvItem : IDisplayable
{
public string ItmeNo { get; set; }
public string Descriptoin { get; set; }
public decimal Price { get; set; }

public InvItem(string ItemNo, string Description, decimal Price)

{
this.ItmeNo = ItemNo;
this.Descriptoin = Descriptoin;
this.Price = Price;

}
public string GetDisplayText(string sep) => this.ItmeNo + sep + this.Descriptoin + sep + this.Price.ToString("c");

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 EX1_Create_and_use_an_interface
{
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 (Class)

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

namespace EX1_Create_and_use_an_interface
{
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 (Inharets Form1)

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

namespace EX1_Create_and_use_an_interface
{
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 (inharets form1)

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

namespace EX1_Create_and_use_an_interface
{
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 (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.

First one to help gets a thumbs up!

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

Explanation / Answer

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 EX1_Create_and_use_an_interface
{
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 (From 2)

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


}
}

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