0

Hi this is my first time here on StackOverflow asking questions. Just started learning how to code. I am making a Ordering System with three objects Customer, Inventory and OrderForm.

I have to be able to add/save, Customer information (the below fiels) to a .csv file. via StreamWriter. Also be able to edit and delete and specific information anytime I want.

Now from what I have researched it seems Lists are the best way to go about holding the information till they have been written into the file, also they are much easier to edit and delete the information

I've declared a list (is that the proper way to do it?) in the customer class. How would I go about adding all those variables to the List now? Then be able able to edit or delete them as I want?

Appreciate the help.

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
Ali
  • 17
  • 5

1 Answers1

0

First usually you do not publish fields

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

You would create an property.

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

But you have to instantiate the Property!

CustInfo = new List<Customer>();

Also you dont use getter and setter methods in c# (like in java), here you use also properties.

   // 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

would be

public string CustomerName
{
get{
return custName;
}

set{
custName = value;
}

Read here and here for more information.

Now to your question. The list of Customers should not be in Customer. There should be a class which handles your data.

Then you could do someting like:

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

Your list holds Customers so you dont need to add properties. Read here for more information.

I hope this helps.

Community
  • 1
  • 1
Lucas
  • 3,142
  • 5
  • 26
  • 44
  • Well I have a form called CustomerInfo with TextBoxes which will recieve that data. I was thinking in the Class customer I could do the lists, the properties, pass the CustomerInfo through them and into Customer List and also do streamWriter and StreamReader via Customer Class? Or should I put the Customer List into the CustomerInfo Form, because thats where I put the Save & Exit Button. Thank you it does help, appreciate the extra reading. – Ali Jun 28 '13 at 00:26
  • Put the list into the form. The list hold n objects of the class Customer. And a Customer already have properties. You are welcome. – Lucas Jun 28 '13 at 00:34
  • CustInfo = new List(); does not seem to instantiating in the CustomerForm. I get a red error line under CustInfo? any ideas? – Ali Jun 28 '13 at 01:47
  • Error Message? You shoul see it with mouseover aaboe the red line – Lucas Jun 28 '13 at 06:53