patterncsharpMinor
Product Manager managing product listings and prices
Viewed 0 times
managingandlistingsproductpricesmanager
Problem
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
Controller/ProductList.cs
Model/Product.cs
Utility/Validate.cs
```
using System.Text.RegularExpressions;
namespace Ferienaufgabe_2.Utility
{
class Validate
{
public bool IsValidPriceFormat(string value)
{
string strRegex =
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
{
///
/// Der Haupteinstiegspunkt für die Anwendung.
///
[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 =
Solution
To help you get started, here's how I would write the
I used public to denote the visibility of the class. By default,
I suggest you consider using the camel case naming convention for locals, parameters, fields e.g. productName
In this case, I used auto properties e.g.
public string ProductName { get; set; }
No need to declare your own private fields (
Product classpublic class Product
{
public Product(string productName, decimal price)
{
this.ProductName = productName;
this.Price = price;
}
public string ProductName { get; set; }
public decimal Price { get; set; }
}I used public to denote the visibility of the class. By default,
C# will mark classes as internal which may not be obvious to other people. I specifically state the visibility to avoid any confusion.I suggest you consider using the camel case naming convention for locals, parameters, fields e.g. productName
In this case, I used auto properties e.g.
public string ProductName { get; set; }
No need to declare your own private fields (
_productname), if you are not using them for anything. Consider using auto properties instead.Code Snippets
public class Product
{
public Product(string productName, decimal price)
{
this.ProductName = productName;
this.Price = price;
}
public string ProductName { get; set; }
public decimal Price { get; set; }
}Context
StackExchange Code Review Q#96012, answer score: 6
Revisions (0)
No revisions yet.