首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >产品经理管理产品清单和价格

产品经理管理产品清单和价格
EN

Code Review用户
提问于 2015-07-06 20:56:05
回答 2查看 1.9K关注 0票数 3

对于学校,我们必须培养一名产品经理。基本任务是增加新产品。产品有名称和价格。

我知道这个或另一个类目前相当空,但是它计划添加更多的方法。

代码是用MVC格式开发的,注释和文本都是德文。

我想知道什么是好的,什么是坏的(也是为什么),以及如何在设计、结构、变量/方法命名、性能方面改进代码。

Program.cs

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

使用MS VS 2015 RC - form designer构建

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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);

    }
}
}
EN

回答 2

Code Review用户

发布于 2015-07-06 21:18:15

为了帮助您开始学习,下面是我将如何编写Product

代码语言:javascript
运行
复制
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; }    
}

我用public来表示类的可见性。默认情况下,C#会将类标记为internal,这对其他人来说可能并不明显。我特别说明了可见性,以避免任何混淆。

我建议您考虑对本地、参数、字段(例如productName )使用骆驼案例命名约定。

在这种情况下,我使用了自动属性。

公共字符串ProductName { get;set;}

不需要声明您自己的私有字段(_productname),如果您没有将它们用于任何事情。考虑使用自动属性。

票数 6
EN

Code Review用户

发布于 2015-07-06 21:16:35

在你的模型中,你的事情有点混乱。

私有字符串产品名称;私有十进制价格;公共产品(字符串产品名称,十进制价格){ this.Productname =产品名称;this.Price =价格;}私有字符串产品名称{ set { this.productname =值;}获取{返回this.productname;}私有十进制价格{ this.price =值;}获取{返回this.price;}}

您应该拥有private属性和public访问器。

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/96012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档