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); } else if (screenOption == ScreenOption.MODIFY) { screenLabel.Text = "Modify Part"; PopulatePartData(_selectedPart); } ValidateInputs(true); } 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.CreateNewPartId(); 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(bool initial = false) { bool valid = false; string err = string.Empty; err += ValidateName(); err += ValidateInventory(); err += ValidatePrice(); err += ValidateMax(); err += ValidateMin(); err += ValidateMachineOrCompany(); valid = _isNameValid && _isInventoryValid && _isPriceValid && _isMaxValid && _isMinValid && _isMachineOrCompanyValid; if (!valid && !initial) { Helpers.ShowErrorMessage(err, "Validation error"); } return valid; } private string ValidateName() { string err = string.Empty; if (nameTextBox.Text == string.Empty) { err = "Part must have a name\n"; errorProvider1.SetError(nameTextBox, "Please enter a part name"); _isNameValid = false; } else { errorProvider1.SetError(nameTextBox, ""); _isNameValid = true; } Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid); return err; } private string ValidateInventory() { string err = string.Empty; int inventory = 0; if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory)) { if (_isMinValid && _isMaxValid) { if (inventory > _validatedMax) { err = $"Inventory must be less than Max: {_validatedMax}\n"; errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}"); _isInventoryValid = false; } else if (inventory < _validatedMin) { err = $"Inventory must be more than Min: {_validatedMin}\n"; errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}"); _isInventoryValid = false; } else { errorProvider1.SetError(inventoryTextBox, ""); _isInventoryValid = true; } } else { err = "Inventory can't be set without valid Min and Max values\n"; errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set"); _isInventoryValid = false; } } else { err = "Inventory must be an integer value\n"; _isInventoryValid = false; errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value"); } Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid); return err; } private string ValidatePrice() { string err = string.Empty; 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"); err = "Price must be a decimal value\n"; } Helpers.SetTextBoxValidationState(priceCostTextBox, _isPriceValid); return err; } private string ValidateMax() { string err = string.Empty; 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"); err = "Max must be an integer value\n"; } if (_isMinValid && _isMaxValid && _validatedMax < _validatedMin) { _isMaxValid = false; err = $"Max cannot be less than Min: {_validatedMin}\n"; errorProvider1.SetError(maxTextBox, $"Max cannot be less than Min: {_validatedMin}"); } ValidateInventory(); Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid); return err; } private string ValidateMin() { string err = string.Empty; 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"); err = "Min must be an integer value\n"; } if (_isMinValid && _isMaxValid && _validatedMin > _validatedMax) { _isMinValid = false; err = $"Min cannot be more than Max: {_validatedMax}\n"; errorProvider1.SetError(maxTextBox, $"Min cannot be more than Max: {_validatedMax}"); } ValidateInventory(); Helpers.SetTextBoxValidationState(minTextBox, _isMinValid); return err; } private string ValidateMachineOrCompany() { string err = string.Empty; if (machineCompanyTextBox.Text == string.Empty) { _isMachineOrCompanyValid = false; if (_mode is Mode.INHOUSE) { errorProvider1.SetError(machineCompanyTextBox, "Field must contain an integer value"); err = "Machine ID must have an integer value\n"; } if (_mode is Mode.OUTSOURCED) { errorProvider1.SetError(machineCompanyTextBox, "Field must contain a value"); err = "Company Name must have a value\n"; } } 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"); err = "Machine ID must be an integer value\n"; } } if (_mode is Mode.OUTSOURCED) { if (machineCompanyTextBox.Text != string.Empty) { _isMachineOrCompanyValid = true; errorProvider1.SetError(machineCompanyTextBox, ""); } } } Helpers.SetTextBoxValidationState(machineCompanyTextBox, _isMachineOrCompanyValid); return err; } private void machineCompanyTextBox_Validating(object sender, CancelEventArgs e) { ValidateMachineOrCompany(); } private void nameTextBox_TextChanged(object sender, EventArgs e) { ValidateName(); } private void inventoryTextBox_TextChanged(object sender, EventArgs e) { ValidateInventory(); } private void priceCostTextBox_TextChanged(object sender, EventArgs e) { ValidatePrice(); } private void maxTextBox_TextChanged(object sender, EventArgs e) { ValidateMax(); } private void minTextBox_TextChanged(object sender, EventArgs e) { ValidateMin(); } private void machineCompanyTextBox_TextChanged(object sender, EventArgs e) { ValidateMachineOrCompany(); } } }