c968-wgu-project/C968Project/Views/AddModifyPartScreen.cs

175 lines
4.8 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;
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);
}
}
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);
return;
}
var part = Program.Inventory.Parts[partIndex];
var partId = part.PartID;
Program.Inventory.Parts[partIndex] = CreatePart(partId);
}
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)
{
AddPart();
}
if (_screenOption is ScreenOption.MODIFY)
{
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";
}
}
enum Mode
{
INHOUSE,
OUTSOURCED
}
}
}