I want to understand different ways to object design classes. I have three different classes. Generally I am creating a file parser.
The classes:
- CustomerData - which shows text Data Model.
- A File Parser, which will take data from file and place into Generic List
- And Folder reader, which will run FileParse for All Files in a Directory
Should I separate everything into different classes, combine class 1 and 2, or have everything in one united class per below. What is the software architect method? If there is no One answer, what business requirements or principles should I use to make this decision? I would think 'Single Responsibility Principle' states they should be in own class, or is that only for Functions?
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ParseTest
{
public class Customer
{
public class CustomerData
{
// These are the column names in customerdata txt:
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustomerState { get; set; }
public int ProductId { get; set; }
public int QuantityBought { get; set; }
}
public List<CustomerData> GetCustomer(string filename)
{
List<CustomerData> customerdata = new List<CustomerData>();
//const String CustomerBase = @"C:\Users\Desktop\ParseFile\sample.txt";
string CustomerBase = filename;
String fileToLoad = String.Format(CustomerBase);
using (StreamReader r = new StreamReader(fileToLoad))
{
string line;
while ((line = r.ReadLine()) != null)
{
string[] parts = line.Split(',');
// Skip the column names row
if (parts[0] == "id") continue;
CustomerData dbp = new CustomerData();
dbp.CustomerId = Convert.ToInt32(parts[0]);
dbp.CustomerName = parts[1];
dbp.CustomerState = parts[2];
dbp.ProductId = Convert.ToInt32(parts[3]);
dbp.QuantityBought = Convert.ToInt32(parts[4]);
customerdata.Add(dbp);
}
}
return customerdata;
}
public List<CustomerData> GetAllCustomer(string directoryname)
{
List<CustomerData> AllFileCustomerData = new List<CustomerData>();
foreach (string filename in Directory.EnumerateFiles(directoryname, "*.txt"))
{
List<CustomerData> customerdata = new List<CustomerData>();
customerdata = GetCustomer(filename);
AllFileCustomerData.AddRange(customerdata);
}
return AllFileCustomerData;
}
}
}
