From 9e83e28d4bd0a2c1bcf5df4510482a16b63e080d Mon Sep 17 00:00:00 2001 From: chrisbell Date: Thu, 15 May 2025 15:07:30 -0500 Subject: [PATCH] Add/Modify product screen implemented, needs validation and search functionality --- C968Project/Program.cs | 12 ++ C968Project/Views/AddModifyPartScreen.cs | 4 +- .../Views/AddModifyProductScreen.Designer.cs | 4 + C968Project/Views/AddModifyProductScreen.cs | 153 +++++++++++++++++- C968Project/Views/MainScreen.cs | 18 ++- 5 files changed, 179 insertions(+), 12 deletions(-) diff --git a/C968Project/Program.cs b/C968Project/Program.cs index 1bcaff6..f406308 100644 --- a/C968Project/Program.cs +++ b/C968Project/Program.cs @@ -57,6 +57,18 @@ static class Program ApplicationConfiguration.Initialize(); Application.Run(new MainScreen()); } + + public static int CreateNewProductId() + { + ProductIdCounter++; + return ProductIdCounter; + } + + public static int CreateNewPartId() + { + PartIdCounter++; + return PartIdCounter; + } } public enum ScreenOption diff --git a/C968Project/Views/AddModifyPartScreen.cs b/C968Project/Views/AddModifyPartScreen.cs index a603d0d..90ec46a 100644 --- a/C968Project/Views/AddModifyPartScreen.cs +++ b/C968Project/Views/AddModifyPartScreen.cs @@ -95,9 +95,7 @@ namespace C968Project.Views private void AddPart() { - int newId = Program.PartIdCounter; - Program.PartIdCounter++; - + int newId = Program.CreateNewPartId(); Program.Inventory.AddPart(CreatePart(newId)); } diff --git a/C968Project/Views/AddModifyProductScreen.Designer.cs b/C968Project/Views/AddModifyProductScreen.Designer.cs index 02adf5b..40b488b 100644 --- a/C968Project/Views/AddModifyProductScreen.Designer.cs +++ b/C968Project/Views/AddModifyProductScreen.Designer.cs @@ -220,6 +220,7 @@ addButton.TabIndex = 27; addButton.Text = "Add"; addButton.UseVisualStyleBackColor = true; + addButton.Click += addButton_Click; // // deleteButton // @@ -229,6 +230,7 @@ deleteButton.TabIndex = 28; deleteButton.Text = "Delete"; deleteButton.UseVisualStyleBackColor = true; + deleteButton.Click += deleteButton_Click; // // cancelButton // @@ -238,6 +240,7 @@ cancelButton.TabIndex = 30; cancelButton.Text = "Cancel"; cancelButton.UseVisualStyleBackColor = true; + cancelButton.Click += cancelButton_Click; // // saveButton // @@ -247,6 +250,7 @@ saveButton.TabIndex = 29; saveButton.Text = "Save"; saveButton.UseVisualStyleBackColor = true; + saveButton.Click += saveButton_Click; // // AddModifyProductScreen // diff --git a/C968Project/Views/AddModifyProductScreen.cs b/C968Project/Views/AddModifyProductScreen.cs index bf5f390..395de4d 100644 --- a/C968Project/Views/AddModifyProductScreen.cs +++ b/C968Project/Views/AddModifyProductScreen.cs @@ -12,14 +12,157 @@ namespace C968Project.Views { public partial class AddModifyProductScreen : Form { - private ScreenOption screenOption; - private Product? _product; + private ScreenOption _screenOption; + private Product? _currentProduct; - public AddModifyProductScreen(ScreenOption screenOption, Product? product) + + public AddModifyProductScreen(ScreenOption screenOption, Product? product = null) { InitializeComponent(); - this.screenOption = screenOption; - _product = product; + this._screenOption = screenOption; + _currentProduct = product; + + if (_screenOption is ScreenOption.ADD) + { + screenLabel.Text = "Add Product"; + _currentProduct = new(); + associatedPartsDataGridView.DataSource = _currentProduct.AssociatedParts; + } + if (_screenOption is ScreenOption.MODIFY) + { + screenLabel.Text = "Modify Product"; + PopulateProductData(_currentProduct); + } + + allPartsDataGridView.DataSource = Program.Inventory.Parts; + } + + private void PopulateProductData(Product product) + { + idTextBox.Text = product.ProductId.ToString(); + nameTextBox.Text = product.Name; + inventoryTextBox.Text = product.InStock.ToString(); + priceTextBox.Text = product.Price.ToString(); + minTextBox.Text = product.Min.ToString(); + maxTextBox.Text = product.Max.ToString(); + associatedPartsDataGridView.DataSource = product.AssociatedParts; + } + + private Product CreateProduct(int productId) + { + Product product = new(); + + product.ProductId = productId; + product.Name = nameTextBox.Text; + product.InStock = int.Parse(inventoryTextBox.Text); + product.Price = float.Parse(priceTextBox.Text); + product.Min = int.Parse(minTextBox.Text); + product.Max = int.Parse(maxTextBox.Text); + + var dataGridViewData = associatedPartsDataGridView.DataSource; + if (dataGridViewData != null) + { + if (dataGridViewData is BindingList partsList) + { + product.AssociatedParts = partsList; + } + else + { + MessageBox.Show("Error generating Associated Parts: DataGridView DataSource is not of type BindingList somehow. Defaulting to an empty list.", "Data Type Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + product.AssociatedParts = new(); + } + } + else + { + product.AssociatedParts = new(); + } + + return product; + } + + private void AddProduct() + { + int newId = Program.CreateNewProductId(); + Program.Inventory.AddProduct(CreateProduct(newId)); + } + + private void ModifyProduct() + { + int productIndex = 0; + if (_currentProduct != null && Program.Inventory.Products.Contains(_currentProduct)) + { + productIndex = Program.Inventory.Products.IndexOf(_currentProduct); + } + else + { + MessageBox.Show("Error modifying product: Could not find product index", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + var oldProduct = Program.Inventory.Products[productIndex]; + var oldProductId = oldProduct.ProductId; + var modifiedProduct = CreateProduct(oldProductId); + Program.Inventory.Products[productIndex] = modifiedProduct; + } + + private void saveButton_Click(object sender, EventArgs e) + { + if (_screenOption is ScreenOption.ADD) + { + AddProduct(); + Close(); + } + + if (_screenOption is ScreenOption.MODIFY) + { + ModifyProduct(); + Close(); + } + } + + private void cancelButton_Click(object sender, EventArgs e) + { + Close(); + } + + private void addButton_Click(object sender, EventArgs e) + { + if (allPartsDataGridView.CurrentCell is null) + { + MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + Part part = Program.Inventory.LookupPart(allPartsDataGridView.CurrentCell.RowIndex); + if (part is null) return; + + + + if (_currentProduct.AssociatedParts.Contains(part)) + { + MessageBox.Show("Part already added", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + _currentProduct.AddAssociatedPart(part); + } + + private void deleteButton_Click(object sender, EventArgs e) + { + if (associatedPartsDataGridView.CurrentCell is null) + { + MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + Part part = _currentProduct.LookupAssociatedPart(associatedPartsDataGridView.CurrentCell.RowIndex); + + if (part is null || !_currentProduct.AssociatedParts.Contains(part)) + { + MessageBox.Show("Error finding associated part from selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + _currentProduct.RemoveAssociatedPart(_currentProduct.AssociatedParts.IndexOf(part)); } } } diff --git a/C968Project/Views/MainScreen.cs b/C968Project/Views/MainScreen.cs index 6d29f3f..6726587 100644 --- a/C968Project/Views/MainScreen.cs +++ b/C968Project/Views/MainScreen.cs @@ -29,8 +29,10 @@ public partial class MainScreen : Form MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } + Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex); if (part is null) return; + AddModifyPartScreen partScreen = new AddModifyPartScreen(ScreenOption.MODIFY, part); partScreen.Show(); } @@ -42,8 +44,10 @@ public partial class MainScreen : Form MessageBox.Show("No part selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } + Part part = Program.Inventory.LookupPart(partsDataGridView.CurrentCell.RowIndex); if (part is null) return; + if (CheckPartForProductAssociation(part)) return; if (Helpers.ConfirmDelete()) Program.Inventory.DeletePart(part); } @@ -70,8 +74,8 @@ public partial class MainScreen : Form // --- Products private void productsAddButton_Click(object sender, EventArgs e) { - //AddModifyProductScreen productScreen = new AddModifyProductScreen(); - //productScreen.Show(); + AddModifyProductScreen productScreen = new AddModifyProductScreen(ScreenOption.ADD); + productScreen.Show(); } private void productsModifyButton_Click(object sender, EventArgs e) @@ -81,8 +85,12 @@ public partial class MainScreen : Form MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } - //AddModifyProductScreen productScreen = new AddModifyProductScreen(); - //productScreen.Show(); + + Product product = Program.Inventory.LookupProduct(productsDataGridView.CurrentCell.RowIndex); + if (product is null) return; + + AddModifyProductScreen productScreen = new AddModifyProductScreen(ScreenOption.MODIFY, product); + productScreen.Show(); } private void productsDeleteButton_Click(object sender, EventArgs e) @@ -92,8 +100,10 @@ public partial class MainScreen : Form MessageBox.Show("No product selected", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } + Product product = Program.Inventory.LookupProduct(productsDataGridView.CurrentCell.RowIndex); if (product is null) return; + if (CheckProductForPartAssociations(product)) return; if (Helpers.ConfirmDelete()) Program.Inventory.RemoveProduct(Program.Inventory.Products.IndexOf(product)); }