diff options
Diffstat (limited to 'customer_maintenance/CustomerMaintenance/CustomerDB.cs')
-rw-r--r-- | customer_maintenance/CustomerMaintenance/CustomerDB.cs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/customer_maintenance/CustomerMaintenance/CustomerDB.cs b/customer_maintenance/CustomerMaintenance/CustomerDB.cs new file mode 100644 index 0000000..a1a3fc6 --- /dev/null +++ b/customer_maintenance/CustomerMaintenance/CustomerDB.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace CustomerMaintenance +{ + public static class CustomerDB + { + private const string dir = @"C:\C#\Files\"; + private const string path = dir + "Customers.txt"; + + + public static void SaveCustomers(List<Customer> customers) + { + // create the output stream for a text file that exists + StreamWriter textOut = + new StreamWriter( + new FileStream(path, FileMode.Create, FileAccess.Write)); + + // write each customer + foreach (Customer customer in customers) + { + textOut.Write(customer.FirstName + "|"); + textOut.Write(customer.LastName + "|"); + textOut.WriteLine(customer.Email); + } + + // write the end of the document + textOut.Close(); + } + + public static List<Customer> GetCustomers() + { + // if the directory doesn't exist, create it + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + // create the object for the input stream for a text file + StreamReader textIn = + new StreamReader( + new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); + + // create the array list for customers + List<Customer> customers = new List<Customer>(); + + // read the data from the file and store it in the ArrayList + while (textIn.Peek() != -1) + { + string row = textIn.ReadLine(); + string[] columns = row.Split('|'); + Customer customer = new Customer(); + customer.FirstName = columns[0]; + customer.LastName = columns[1]; + customer.Email = columns[2]; + customers.Add(customer); + } + + textIn.Close(); + + return customers; + } + } +} |