private void Form1_Load(object sender, EventArgs e) { //Create RetailItem objects. Add to CurrentInventory RetailItem item1 = new RetailItem(1, 12, 59.95, "Jacket"); RetailItem item2 = new RetailItem(2, 40, 34.95, "Jeans"); RetailItem item3 = new RetailItem(3, 20, 24.95, "Shirt"); Program.CurrentInventory.AddItem(item1); Program.CurrentInventory.AddItem(item2); Program.CurrentInventory.AddItem(item3); DisplayInventory(); }
// gets item in list with specified id public RetailItem GetItem(int id) { RetailItem item = null; foreach (RetailItem it in _items) { if (it.IdNumber == id) { item = new RetailItem(it.IdNumber, it.Units, it.Price, it.Description); } } return(item); }
// updates item in list public void UpdateItem(RetailItem item) { bool found = false; foreach (RetailItem it in _items) { if (it.IdNumber == item.IdNumber) { var indexOf = _items.IndexOf(it); _items.RemoveAt(indexOf); _items.Add(item); found = true; LastStatus = "Item Updated"; } } if (!found) { LastStatus = "No matching item with id as a parameter " + item.IdNumber; } }
// adds item to list public void AddItem(RetailItem item) { bool exists = false; foreach (RetailItem it in _items) { // check if the id number of the parameter is already in the list if (it.IdNumber == item.IdNumber) { exists = true; } } if (exists) { LastStatus = "ID number already exists"; } else { // add item to list _items.Add(item); LastStatus = "Item added to inventory"; } }