Answered You can hire a professional tutor to get the answer.

QUESTION

In this exercise, you'll modify a class that stores a list of inventory items so it uses an indexer, a delegate, an event, and operators.

In this exercise, you'll modify a class that stores a list of inventory items so it uses an indexer, a delegate, an event, and operators. Then, you'll modify the code for the Inventory Maintenance form so it uses these features. Getting Started • Download the HW3Start project from Canvas. Unzip the compressed folder and place it in your class folder on your jump drive. • Open up the HW3Start solution. At the top of the code, include a comment with your name and class. For example, the comment I would enter for my assignment would be // Sherry Albright - CIT1755. • Review the code for the InvItemList class so you understand how it works. Then, review the code for the Inventory Maintenance form to see how it uses this class. Finally, run the application to see how it works. Add an index to the InvItemList class • Delete the GetItemByIndex method from the InvItemList class, and replace it with an indexer that receives an int value. This indexer should include both get and set accessors, and the get accessor should check that the value that's passed to it is a valid index. If the index isn't valid, the accessor should throw an ArgumentOutOfRangeException with a message that consists of the index value. • Modify the Invoice Maintenance form to use this indexer instead of the GetItemByIndex method. Then, test the application to be sure it still works. Add overloaded operators to the InvItemList class • Add overloaded + and - operators to the InvItemList class that add and remove an inventory item from the inventory item list. • Modify the Inventory Maintenance form to use these operators instead of the Add and Remove methods. Then, test the application to be sure it still works. Add a delegate and an event to the InvItemList class • Add a delegate named ChangeHandler to the InvItemList class. This delegate should specify a method with a void return type and an InvItemList parameter. • Add an event named Changed to the InvItemList class. This event should use the ChangeHandler delegate and should be raised any time the inventory item list changes. • Modify the Inventory Maintenance form to use the Changed event to save the inventory items and refresh the list box any time the list changes. To do that, you'll need to code an event handler that has the signature specified by the delegate, you'll need to wire the event to the event handler, and you'll need to remove any unnecessary code from the event handlers for the Save and Delete buttons. When you're done, test the application to be sure it still works.

Mine works but it only populates zero's in the list box.

Heres my code so far:

frmInvMaint.cs:

namespace InventoryMaintenance

{

   public partial class frmInvMaint : Form

   {

       private InvItemList invItems = new InvItemList();

       public frmInvMaint()

      {

         InitializeComponent();

           // register event handler (connect the event to the Invoiceupdated method)

           invItems.Changed += new InvItemList.InvItemListChanged(ItemUpdated);

       }

       private void ItemUpdated(InvItem ChangedInvItem, string OperationMessage)

       {

           MessageBox.Show(ChangedInvItem.GetDisplayText() +"n" + OperationMessage + "nCurrent Inventory Item count: " + invItems.Count);

       }

       //event handler for the add button

       private void ChangedInvItem_TextChanged(object sender, System.EventArgs e)

       {

       }

       //event handler for the clear button

       private void ClearInvItem(object sender, System.EventArgs e)

       {

       }

       private void FillItemListBox()

      {

           InvItem item = new InvItem();

         lstItems.Items.Clear();

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

           {

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

           }

      }

       private void frmInvMaint_Load(object sender, System.EventArgs e)

      {

         invItems.Fill();

      }

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

           }

           {

               this.lstItems.TextChanged +=

           new System.EventHandler(this.ChangedInvItem_TextChanged);

           }

       }

       private void btnDelete_Click(object sender, System.EventArgs e)

       {

           this.lstItems.TextChanged +=

               new System.EventHandler(this.ClearInvItem);

           InvItem invItem = new InvItem();

           int i = lstItems.SelectedIndex;

           if (i != -1)

           {

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

      }

   }

}

InvItemList.cs:

namespace InventoryMaintenance

{

   public class InvItemList

   {

       private List<InvItem> invItems;

       // delegate for event

       public delegate void InvItemListChanged

       (InvItem ChangedInvItem, string OperationMessage);

       // event declaration tied to inventotyitemListChanged

       public event

       InvItemListChanged Changed;

       //default constructor

       public InvItemList()

       {

           invItems = new List<InvItem>();

       }

       public int Count

       {

           get

           {

               return invItems.Count;

           }

       }

       public object InvItemList1 { get; internal set; }

       // indexer (gets a inventory item by position (index) in the list)

       public InvItem this[int index]

       {

           get

           {

               if

               (index < 0)

               {

                   // lowest index value for an item in the list is 0

                   throw new

                   ArgumentOutOfRangeException(index.ToString());

               }

               else if

               (index >= invItems.Count)

               {

                   // list is 0 based. If the Count is 3, the first item in the list is at index 0

                   // and the last item in the list is at index 2

                   throw new ArgumentOutOfRangeException(index.ToString());

               }

               return

               invItems[index];

           }

           set

           {

               invItems[index] = value

               ;

               Changed(invItems[index],

               "Inventory Items information was updated!"

               );

           }

       }

       // Add method to add invitems to the list

       public void Add(InvItem InvItemIn)

       {

           invItems.Add(InvItemIn);

           // fire change event

           Changed(InvItemIn, "Inventory Item information was added to the list!");

       }

       // Remove method to delete studentgrade from the list

       public

       void

       Remove(InvItem InvItemIn)

       {

           invItems.Remove(InvItemIn);

           // fire change event

           Changed(InvItemIn, "Inventory Item information was removed from the list!");

       }

       // operators for + and -

       public static InvItemList operator +(InvItemList invItem, InvItem item)

       {

           invItem.Add(item);

           return

           invItem;

       }

       public static InvItemList operator -(InvItemList invItem, InvItem item)

       {

           invItem.Remove(item);

           return

          invItem;

       }

       public void Add(int itemNo, string description, decimal price)

       {

           InvItem i = new InvItem(itemNo, description, price);

           invItems.Add(i);

       }

       public void Fill()

       {

           invItems = InvItemDB.GetItems();

       }

       public void Save()

       {

           InvItemDB.SaveItems(invItems);

       }

   }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question