0

嗨,这是我第一次在 StackOverflow 上提问。刚开始学习如何编码。我正在制作一个包含三个对象 Customer、Inventory 和 OrderForm 的订购系统。

我必须能够将客户信息(以下字段)添加/保存到 .csv 文件中。通过 StreamWriter。还可以随时编辑和删除特定信息。

现在从我的研究来看,列表似乎是保存信息直到它们被写入文件的最佳方式,而且它们更容易编辑和删除信息

我已经在客户类中声明了一个列表(这是正确的方法吗?)。我现在将如何将所有这些变量添加到列表中?然后能够根据需要编辑或删除它们吗?

感谢帮助。

public class Customer
{

    //Holds Customer Info.
    List<Customer> custInfo = new List<Customer>();****THIS ONE*******

    //Instance Variables
    private string custName;
    private string custID;
    private string custAddress;
    private string custState;
    private int custZip;
    private int custAge;
    private decimal totalOrdered;

    public Customer()
    { }//default constructor

    // retrieve the customer name
    public string getCustomerName()
    {
        return custName;
    }//end of getCustomerName method

    // set the customer name
    public void setCustomerName(string customerName)
    {
        custName = customerName;
    }//end of setCustomerName method

    //get customerID
    public string getCustID()
    {
        return custID;
    }//end of getCustID

    //set CustomerID
    public void setCustID(string customerID)
    {
        custID = customerID;
    }//end setCustID

    // retrieve the customer address
    public string getCustomerAddress()
    {
        return custAddress;
    }//end of getCustomerAddress method

    // set the customer address
    public void setCustomerAddress(string customerAddress)
    {
        custAddress = customerAddress;
    }//end of setCustomerAddress method

    //retrieves customer state
    public string getCustState()
    {
        return custState;
    }//end getCustState

    //sets customer state
    public void setCustState(string customerState)
    {
        custState = customerState;
    }//end setCustState

    // retrieve the customer zip
    public int getCustomerZip()
    {
        return custZip;
    }//end of getCustomerZip method

    // set the customer Zip
    public void setCustomerZip(int customerZip)
    {
        custZip = customerZip;
    }//end of setCustomerZip method

    //Gets customer age
    public int getCustomerAge()
    {
        return custAge;
    }//end getCustomerAge

    //sets customer age
    public void setCustomerAge(int customerAge)
    {
        custAge = customerAge;
    }//end setCustomerAge

    //gets customers total order
    public decimal getTotalOrdered()
    {
        return totalOrdered;
    }//end getTotalOrdered

    //sets total ordered
    public void setTotalOrdered(decimal customerTotal)
    {
        totalOrdered = customerTotal;
    }//end setTotalOrdered

}//end public class Customer
4

1 回答 1

0

首先通常你不发布字段

//Holds Customer Info.
List<Customer> custInfo = new List<Customer>();****THIS ONE*******

您将创建一个属性。

public List<Customer> CustInfo {get;set;}

但是你必须实例化属性!

CustInfo = new List<Customer>();

你也不要在 c# 中使用 getter 和 setter 方法(就像在 java 中一样),在这里你也使用属性。

   // retrieve the customer name
public string getCustomerName()
{
    return custName;
}//end of getCustomerName method

// set the customer name
public void setCustomerName(string customerName)
{
    custName = customerName;
}//end of setCustomerName method

将会

public string CustomerName
{
get{
return custName;
}

set{
custName = value;
}

阅读此处此处了解更多信息。

现在回答你的问题。客户列表不应客户中。应该有一个类来处理您的数据。

然后你可以做类似的事情:

CustInfo.Add(new Customer{CustomerName = "Jack"});

您的列表包含客户,因此您无需添加属性。阅读此处了解更多信息。

我希望这有帮助。

于 2013-06-27T23:40:05.020 回答