Question: Please Answer In C# . Extra 1 5 - 1 Create and use an interface In this exercise, you'll create an IDisplayable interface and then

Please Answer In C#. Extra 15-1 Create and use an interface
In this exercise, you'll create an IDisplayable interface and then use it in the
InventoryItem class of the Inventory Maintenance app.
1.Open the InventoryMaintenance project in the ExtraStarts Chp 15
InventoryMaintIDisplayable 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 InventoryItem class, find the GetDisplayText() method
that this class contains, and comment out 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 code for this method with the code in the original
GetDisplayText() method that you commented out in step 3.
6.Test the app to be sure it still works.
7.Display the code for the Inventory Maintenance form. In the FillItemListBox()
method, change the type of the item variable from InventoryItem to IDisplayable.
8.Test the app to be sure it still works.
9.Modify the event handler for the Click event of the Add button to include the
following debugging code after the code that gets an InventoryItem object named
item from the New Item form:
Debug.Writeline ($"Item type: {item.GetType ()}");
Debug.WriteLine($"Item is InventoryItem: {item is InventoryItem}");
Debug.WriteLine($"Item is IDisplayable: {item is IDisplayable}");
10. Run the app and add a new Plant item and a new Supply item. After adding each item, check the Output window and review the information that's displayed. Here is the code I have so far starting with the main page , then including the Inventory item class. using System.Diagnostics;
namespace InventoryMaintenance
{
public partial class frmInventoryMaint : Form
{
public frmInventoryMaint()
{
InitializeComponent();
}
private InventoryItemList items = new();
private void frmInventoryMaint_Load(object sender, EventArgs e)
{
items.Changed += new InventoryItemList.ChangeHandler(HandleChange);
items.Fill();
FillItemListBox();
}
private void FillItemListBox()
{
IDisplayable item =GetSelectedItem();
ListBox.Items.Add(item.GetDisplayText());
lstItems.Items.Clear();
for (int i =0; i items.Count; i++)
{
item = items[i];
lstItems.Items.Add(item.GetDisplayText());
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
IDisplayable item = GetNewItem();
frmNewItem newItemForm = new();
InventoryItem item = newItemForm.GetNewItem();
if (item != null)
{
items += item;
}
Debug.WriteLine($"Item type: {item.GetType()}");
Debug.WriteLine($"Item is InventoryItem: {item is InventoryItem}");
Debug.WriteLine($"Item is IDisplayable: {item is IDisplayable}");
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i = lstItems.SelectedIndex;
if (i ==-1)
{
MessageBox.Show("Please select an item to delete.", "No item selected");
}
else
{
InventoryItem item = items[i];
string message = $"Are you sure you want to delete {item.Description}?";
DialogResult result =
MessageBox.Show(message, "Confirm Delete",
MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
items -= item;
}
}
}
private void HandleChange(InventoryItemList invItems)
{
invItems.Save();
FillItemListBox();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}// now the inventory item class using System.Reflection.Metadata.Ecma335;
namespace InventoryMaintenance
{
public class InventoryItem : IDisplayable
{
public InventoryItem(){}
public InventoryItem(int itemNo, string description, decimal price)
{
ItemNo = itemNo;
Description = description;
Price = price;
}
public int ItemNo { get; set; }
public string Description { get; set; }="";
public decimal Price { get; set; }
public virtual string GetDisplayText()=> $"{ItemNo}{Description}({Price:c})";
// public virtual string GetDisplayText()
//{
// return "Display text for inventory item"; }
 Please Answer In C#. Extra 15-1 Create and use an interface

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!