For school we have to develop a product manager. The basic task is to add new products. A product has a name and a price.
I know the one or other class is currently pretty empty but its planned to add more methods.
The code is developed in MVC-format and comments plus texts are in German.
I would like to know what's good and what's bad (also why) and how I can improve the code in design, structure, variable/method naming, performance.
Program.cs
using System;
using Ferienaufgabe_2.Controller;
namespace Ferienaufgabe_2
{
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
ProductList productList = new ProductList();
productList.RenderView();
}
}
}
Controller/ProductList.cs
using System.Windows.Forms;
namespace Ferienaufgabe_2.Controller
{
class ProductList
{
public void RenderView()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new View.ProductList());
}
}
}
Model/Product.cs
namespace Ferienaufgabe_2.Model
{
class Product
{
private string productname;
private decimal price;
public Product(string productname, decimal price)
{
this.Productname = productname;
this.Price = price;
}
private string Productname
{
set
{
this.productname = value;
}
get
{
return this.productname;
}
}
private decimal Price
{
set
{
this.price = value;
}
get
{
return this.price;
}
}
}
}
Utility/Validate.cs
using System.Text.RegularExpressions;
namespace Ferienaufgabe_2.Utility
{
class Validate
{
public bool IsValidPriceFormat(string value)
{
string strRegex = @"^[0-9]{1,}([\,]{0,1}[0-9]{1,2}){0,1}$";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
Match myMatch = myRegex.Match(value);
if (myMatch.Success)
{
return true;
}
return false;
}
}
}
View/ProductList.cs
Build with MS VS 2015 RC - form designer
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Ferienaufgabe_2.View
{
public partial class ProductList : Form
{
private string defaultTextAddProductTextBox;
private string messageMissingProductname;
public ProductList()
{
InitializeComponent();
// Translations
this.Text = "Produkteingabe";
this.defaultTextAddProductTextBox = "Produktname";
this.messageMissingProductname = "Bitte einen Produktnamen angeben.";
}
private void textBoxAddProduct_Enter(object sender, EventArgs e)
{
if (this.textBoxAddProduct.Text == this.defaultTextAddProductTextBox)
{
this.textBoxAddProduct.ForeColor = Color.Black;
this.textBoxAddProduct.Text = "";
}
}
private void textBoxAddProduct_Leave(object sender, EventArgs e)
{
if (this.textBoxAddProduct.Text.Trim().Length == 0)
{
this.textBoxAddProduct.Text = this.defaultTextAddProductTextBox;
this.textBoxAddProduct.ForeColor = Color.Gray;
}
}
private void textBoxAddProduct_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.AddProductToList();
}
}
private void buttonAddProduct_Click(object sender, EventArgs e)
{
this.AddProductToList();
}
protected bool AddProductToList()
{
if (this.textBoxAddProduct.Text.Trim().Length == 0)
{
MessageBox.Show(this.messageMissingProductname);
return false;
}
decimal productPrice = Prompt.ShowPriceDialog();
this.productBindingSource.Add(new Model.Product(this.textBoxAddProduct.Text, productPrice));
this.textBoxAddProduct.Text = this.defaultTextAddProductTextBox;
this.textBoxAddProduct.ForeColor = Color.Gray;
this.dataGridViewProductList.Focus();
return true;
}
}
}
View/Prompt.cs
using System;
using System.Windows.Forms;
using Ferienaufgabe_2.Utility;
using System.Drawing;
namespace Ferienaufgabe_2.View
{
static class Prompt
{
public static decimal ShowPriceDialog()
{
Validate validator = new Validate();
// Create form and its fields
Form prompt = new Form();
Label textLabel = new Label();
TextBox inputBox = new TextBox();
Button confirmation = new Button();
// Translations
string defaultTextInputBox = "z.B.: 10,00";
string messageInvalidPriceFormat = "Ungültige Preiseingabe! Z.B.: 10,00";
string textOfLabel = "Preis";
string textOfConfirmButton = "Prozess abschliessen";
// Design form and its fields
prompt.Width = 300;
prompt.Height = 120;
prompt.StartPosition = FormStartPosition.CenterParent;
prompt.FormBorderStyle = FormBorderStyle.None;
textLabel.Location = new Point(110, 15);
textLabel.Text = textOfLabel;
textLabel.Font = new Font("Arial", 18);
inputBox.Location = new Point(40, (textLabel.Location.Y + textLabel.Height + 10));
inputBox.Width = 205;
inputBox.Text = defaultTextInputBox;
inputBox.ForeColor = Color.Gray;
confirmation.Location = new Point(40, (inputBox.Location.Y + inputBox.Height + 10));
confirmation.Width = inputBox.Width;
confirmation.Text = textOfConfirmButton;
// Add events
inputBox.KeyUp += (sender, e) => {
if (e.KeyCode == Keys.Enter && validator.IsValidPriceFormat(inputBox.Text))
{
prompt.Close();
}
else if (e.KeyCode == Keys.Enter)
{
textLabel.Focus();
MessageBox.Show(messageInvalidPriceFormat);
}
};
confirmation.Click += (sender, e) => {
if (validator.IsValidPriceFormat(inputBox.Text))
{
prompt.Close();
}
else
{
textLabel.Focus();
MessageBox.Show(messageInvalidPriceFormat);
}
};
inputBox.GotFocus += (sender, e) =>
{
if (inputBox.Text == defaultTextInputBox)
{
inputBox.Text = "";
inputBox.ForeColor = Color.Black;
}
};
inputBox.LostFocus += (sender, e) =>
{
if (inputBox.Text.Trim().Length == 0)
{
inputBox.Text = defaultTextInputBox;
inputBox.ForeColor = Color.Gray;
}
};
// Add fields to form
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.Controls.Add(confirmation);
// Show dialog
prompt.ShowDialog();
// Return converted input. This goes well anytime due to a check in the dispose actions.
return Convert.ToDecimal(inputBox.Text);
}
}
}