c968-wgu-project/C968Project/Views/AddModifyPartScreen.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

398 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C968Project.Views
{
public partial class AddModifyPartScreen : Form
{
private ScreenOption _screenOption;
private Part? _selectedPart = null;
private Mode _mode = Mode.INHOUSE;
private bool _isNameValid = false;
private bool _isInventoryValid = false;
private bool _isPriceValid = false;
private bool _isMaxValid = false;
private bool _isMinValid = false;
private bool _isMachineOrCompanyValid = false;
private int _validatedMin = 0;
private int _validatedMax = 0;
public AddModifyPartScreen(ScreenOption screenOption, Part? part = null)
{
InitializeComponent();
_screenOption = screenOption;
_selectedPart = part;
if (screenOption == ScreenOption.ADD)
{
screenLabel.Text = "Add Part";
ChangeMode(Mode.INHOUSE);
ValidateInputs();
}
else if (screenOption == ScreenOption.MODIFY)
{
screenLabel.Text = "Modify Part";
PopulatePartData(_selectedPart);
inHouseRadioButton.Enabled = false;
outsourcedRadioButton.Enabled = false;
ValidateInputs();
}
}
private void PopulatePartData(Part part)
{
idTextBox.Text = part.PartID.ToString();
nameTextBox.Text = part.Name;
inventoryTextBox.Text = part.InStock.ToString();
priceCostTextBox.Text = part.Price.ToString();
maxTextBox.Text = part.Max.ToString();
minTextBox.Text = part.Min.ToString();
if (part is Outsourced op)
{
ChangeMode(Mode.OUTSOURCED);
machineCompanyTextBox.Text = op.CompanyName;
}
if (part is Inhouse ip)
{
ChangeMode(Mode.INHOUSE);
machineCompanyTextBox.Text = ip.MachineID.ToString();
}
}
private void ModifyPart()
{
int partIndex;
if (_selectedPart != null && Program.Inventory.Parts.Contains(_selectedPart))
{
partIndex = Program.Inventory.Parts.IndexOf(_selectedPart);
}
else
{
MessageBox.Show("Error modifying part: Could not find part index", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var oldPart = Program.Inventory.Parts[partIndex];
var oldPartId = oldPart.PartID;
var modifiedPart = CreatePart(oldPartId);
Program.Inventory.Parts[partIndex] = modifiedPart;
Program.Inventory.PropogatePartModificationToAssociatedProducts(oldPart, modifiedPart);
}
private void AddPart()
{
int newId = Program.PartIdCounter;
Program.PartIdCounter++;
Program.Inventory.AddPart(CreatePart(newId));
}
private Part CreatePart(int partId)
{
Part part;
if (_mode is Mode.INHOUSE)
{
var inhouse = new Inhouse();
inhouse.MachineID = int.Parse(machineCompanyTextBox.Text);
part = inhouse;
}
else if (_mode is Mode.OUTSOURCED)
{
var outsourced = new Outsourced();
outsourced.CompanyName = machineCompanyTextBox.Text;
part = outsourced;
}
else
{
throw new InvalidOperationException("No part mode selected somehow");
}
part.PartID = partId;
part.Name = nameTextBox.Text;
part.InStock = int.Parse(inventoryTextBox.Text);
part.Price = float.Parse(priceCostTextBox.Text);
part.Max = int.Parse(maxTextBox.Text);
part.Min = int.Parse(minTextBox.Text);
return part;
}
private void saveButton_Click(object sender, EventArgs e)
{
if (_screenOption is ScreenOption.ADD)
{
if (ValidateInputs())
{
AddPart();
Close();
}
}
if (_screenOption is ScreenOption.MODIFY)
{
if (ValidateInputs())
{
ModifyPart();
Close();
}
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void inHouseRadioButton_Click(object sender, EventArgs e)
{
ChangeMode(Mode.INHOUSE);
}
private void outsourcedRadioButton_Click(object sender, EventArgs e)
{
ChangeMode(Mode.OUTSOURCED);
}
private void ChangeMode(Mode mode)
{
_mode = mode;
if (mode is Mode.INHOUSE)
{
outsourcedRadioButton.Checked = false;
inHouseRadioButton.Checked = true;
machineCompanyLabel.Text = "Machine ID";
}
if (mode is Mode.OUTSOURCED)
{
inHouseRadioButton.Checked = false;
outsourcedRadioButton.Checked = true;
machineCompanyLabel.Text = "Company Name";
}
ValidateMachineOrCompany();
}
enum Mode
{
INHOUSE,
OUTSOURCED
}
private bool ValidateInputs()
{
ValidateName();
ValidateInventory();
ValidatePrice();
ValidateMax();
ValidateMin();
ValidateMachineOrCompany();
return _isNameValid && _isInventoryValid && _isPriceValid && _isMaxValid && _isMinValid && _isMachineOrCompanyValid;
}
private void ValidateName()
{
if (nameTextBox.Text == string.Empty)
{
errorProvider1.SetError(nameTextBox, "Please enter a part name");
_isNameValid = false;
}
else
{
errorProvider1.SetError(nameTextBox, "");
_isNameValid = true;
}
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
}
private void nameTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateName();
}
private void ValidateInventory()
{
int inventory = 0;
if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory))
{
if (_isMinValid && _isMaxValid)
{
if (inventory > _validatedMax)
{
errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}");
_isInventoryValid = false;
}
else if (inventory < _validatedMin)
{
errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}");
_isInventoryValid = false;
}
else
{
errorProvider1.SetError(inventoryTextBox, "");
_isInventoryValid = true;
}
}
else
{
errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set");
_isInventoryValid = false;
}
}
else
{
_isInventoryValid = false;
errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value");
}
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
}
private void inventoryTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateInventory();
}
private void ValidatePrice()
{
float price = 0f;
if (priceCostTextBox.Text != string.Empty && float.TryParse(priceCostTextBox.Text, out price))
{
_isPriceValid = true;
errorProvider1.SetError(priceCostTextBox, "");
}
else
{
_isPriceValid = false;
errorProvider1.SetError(priceCostTextBox, "Field must contain a floating point number");
}
Helpers.SetTextBoxValidationState(priceCostTextBox, _isPriceValid);
}
private void priceCostTextBox_Validating(object sender, CancelEventArgs e)
{
ValidatePrice();
}
private void ValidateMax()
{
int max = 0;
if (maxTextBox.Text != string.Empty && int.TryParse(maxTextBox.Text, out max))
{
_isMaxValid = true;
errorProvider1.SetError(maxTextBox, "");
_validatedMax = max;
}
else
{
_isMaxValid = false;
errorProvider1.SetError(maxTextBox, "Field must contain an integer value");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
}
private void maxTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateMax();
}
private void ValidateMin()
{
int min = 0;
if (minTextBox.Text != string.Empty && int.TryParse(minTextBox.Text, out min))
{
_isMinValid = true;
errorProvider1.SetError(minTextBox, "");
_validatedMin = min;
}
else
{
_isMinValid = false;
errorProvider1.SetError(minTextBox, "Field must contain an integer value");
}
ValidateInventory();
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
}
private void minTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateMin();
}
private void ValidateMachineOrCompany()
{
if (machineCompanyTextBox.Text == string.Empty)
{
_isMachineOrCompanyValid = false;
if (_mode is Mode.INHOUSE)
{
errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value");
}
if (_mode is Mode.OUTSOURCED)
{
errorProvider1.SetError(machineCompanyTextBox, "Field must contain a value");
}
}
else
{
if (_mode is Mode.INHOUSE)
{
int machineId = 0;
if (int.TryParse(machineCompanyTextBox.Text, out machineId))
{
_isMachineOrCompanyValid = true;
errorProvider1.SetError(machineCompanyTextBox, "");
}
else
{
_isMachineOrCompanyValid = false;
errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value");
}
}
if (_mode is Mode.OUTSOURCED)
{
if (machineCompanyTextBox.Text != string.Empty)
{
_isMachineOrCompanyValid = true;
errorProvider1.SetError(machineCompanyTextBox, "");
}
}
}
Helpers.SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid);
}
private void machineCompanyTextBox_Validating(object sender, CancelEventArgs e)
{
ValidateMachineOrCompany();
}
}
}