public bool CreateNewSeasonalProduct(SeasonalProduct newSeasonalProduct) { SeasonProductCatalog seasonalProductList = new SeasonProductCatalog(); newSeasonalProduct.ProductID = seasonalProductList.NewSeasonalProductID(); return(seasonalProductList.AddNewSeasonalProduct(newSeasonalProduct)); }
public SeasonalProduct DisplaySeasonalProductCreation() { SeasonalProduct newProduct = new SeasonalProduct(); string name, price, startMonth, endMonth; bool done = false; do { Console.Clear(); Console.WriteLine("Please write new product information:"); Console.Write("Name: "); name = Console.ReadLine(); Console.Write("Price: "); price = Console.ReadLine(); Console.Write("StartMonth (mm): "); startMonth = Console.ReadLine(); Console.Write("EndMonth (mm): "); endMonth = Console.ReadLine(); try { newProduct.newSeasonalProduct(name, price, startMonth, endMonth); done = true; } catch (Exception) { Console.WriteLine("\nIncorrectly entered!, press any to try again or q for quit"); } if (done == false) { ConsoleKeyInfo cki = Console.ReadKey(); if (cki.Key.ToString() == "Q") { throw new System.ArgumentException("Quit"); } } } while (done == false); Console.WriteLine("\nSeasonalProduct {0} is succesfully created!\nStarts on the {1}. Month and ends on the {2}. Month\n", newProduct.Name, newProduct.SeasonStartDate.Month, newProduct.SeasonEndDate.Month); return(newProduct); }
//Adds new SeasonalProduct public bool AddNewSeasonalProduct(SeasonalProduct newSeasonalProduct) { string delimiter = ";"; string[][] output = new string[][] { new string[] { newSeasonalProduct.ProductID.ToString(), newSeasonalProduct.Name, newSeasonalProduct.Price.ToString(), newSeasonalProduct.SeasonStartDate.ToString(), newSeasonalProduct.SeasonEndDate.ToString(), newSeasonalProduct.Active.ToString() } /*add the values that you want inside a csv file. Mostly this function can be used in a foreach loop.*/ }; int length = output.GetLength(0); StringBuilder sb = new StringBuilder(); for (int index = 0; index < length; index++) { sb.AppendLine(string.Join(delimiter, output[index])); } File.AppendAllText(filePath, sb.ToString()); return(true); }
//Updates seasonalproduct - Changes to its flag for credit public bool UpdateProduct(SeasonalProduct updatedSeasonalProduct) { List <string> seasonalProducts = new List <string>(); using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)) { string seasonalProduct; while ((seasonalProduct = reader.ReadLine()) != null) { if (seasonalProduct.Contains(";")) { string[] values = seasonalProduct.Split(';'); if (values[0] == updatedSeasonalProduct.ProductID.ToString()) { values[1] = updatedSeasonalProduct.Name; values[2] = updatedSeasonalProduct.Price.ToString(); values[3] = updatedSeasonalProduct.SeasonStartDate.ToString(); values[4] = updatedSeasonalProduct.SeasonEndDate.ToString(); values[5] = updatedSeasonalProduct.Active.ToString(); seasonalProduct = string.Join(";", values); } } seasonalProducts.Add(seasonalProduct); } } using (StreamWriter writer = new StreamWriter(filePath, false)) { foreach (string seasonalProduct in seasonalProducts) { writer.WriteLine(seasonalProduct); } } return(true); }
public List <SeasonalProduct> GetList() { List <SeasonalProduct> seasonalProductList = new List <SeasonalProduct>(); int i = 0; checkCreateProductFile(); var reader = new StreamReader(File.OpenRead(filePath), Encoding.Default); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); // Skipping first line of the file, because thats the ID Name Price Active part. if (i == 1) { SeasonalProduct seasonProduct = new SeasonalProduct(); seasonProduct.ProductID = Convert.ToInt32(values[0]); seasonProduct.Name = values[1]; seasonProduct.Price = Convert.ToDouble(values[2]); seasonProduct.SeasonStartDate = Convert.ToDateTime(values[3]); seasonProduct.SeasonEndDate = Convert.ToDateTime(values[4]); seasonProduct.Activate(); seasonalProductList.Add(seasonProduct); } else { i = 1; } } reader.Close(); return(seasonalProductList); }