c968-wgu-project/C968Project/Inventory.cs
chrisbell ef65598a4d Main Screen fixes
Added confimation dialogues for deleting parts/products;
Finished implementing Product class methods;
Propogating part modifications to associated products;
Stopped deleting of items if there is an association;
2025-05-15 13:31:08 -05:00

106 lines
2.8 KiB
C#

using System;
using System.ComponentModel;
namespace C968Project;
public class Inventory
{
public BindingList<Product> Products { get; set; } = new();
public BindingList<Part> Parts { get; set; } = new();
public void AddProduct(Product product)
{
if (!Products.Contains(product))
{
Products.Add(product);
return;
}
MessageBox.Show($"Identical product already exists", "Error", MessageBoxButtons.OK);
}
public bool RemoveProduct(int productIndex)
{
if (productIndex > Products.Count)
{
MessageBox.Show($"Error occured when trying to delete product with index of {productIndex}. Out of bounds.", "Error", MessageBoxButtons.OK);
return false;
}
Products.RemoveAt(productIndex);
return true;
}
public Product? LookupProduct(int productIndex)
{
if (productIndex > Products.Count)
{
MessageBox.Show($"Error occured when trying to find product with index of {productIndex}. Out of bounds.", "Error", MessageBoxButtons.OK);
return null;
}
return Products[productIndex];
}
public void UpdateProduct(int index, Product newProduct)
{
if (index > Products.Count)
{
MessageBox.Show($"Error occured when trying to update product with index of {index}. Out of bounds.", "Error", MessageBoxButtons.OK);
return;
}
Products[index] = newProduct;
}
public void AddPart(Part part)
{
if (!Parts.Contains(part))
{
Parts.Add(part);
return;
}
MessageBox.Show($"Identical part already exists", "Error", MessageBoxButtons.OK);
}
public bool DeletePart(Part part)
{
return Parts.Remove(part);
}
public Part LookupPart(int partIndex)
{
if (partIndex > Parts.Count)
{
MessageBox.Show($"Error occured when trying to find part with index of {partIndex}. Out of bounds.", "Error", MessageBoxButtons.OK);
return null;
}
return Parts[partIndex];
}
public void UpdatePart(int index, Part newPart)
{
if (index > Parts.Count)
{
MessageBox.Show($"Error occured when trying to update part with index of {index}. Out of bounds.", "Error", MessageBoxButtons.OK);
return;
}
Parts[index] = newPart;
}
public void PropogatePartModificationToAssociatedProducts(Part oldPart, Part newPart)
{
foreach (var product in Products)
{
if (product.AssociatedParts.Contains(oldPart))
{
product.RemoveAssociatedPart(product.AssociatedParts.IndexOf(oldPart));
product.AddAssociatedPart(newPart);
}
}
}
}