示例#1
0
 /// <summary>
 /// Updates file
 /// </summary>
 /// <param name="product">The product to be updated</param>
 public void GBRUpdate(GBRProduct product)
 {
     try
     {
         SaveToFile(product, true);
     }
     catch (Exception ex)
     {
         throw new Exception("Errors during update:\n" + ex.Message);
     }
 }
示例#2
0
 /// <summary>
 /// Remove the product from file
 /// </summary>
 /// <param name="product">Remove from file</param>
 public void GBRDelete(GBRProduct product)
 {
     try
     {
         DeleteAndSaveFile(product);
     }
     catch (Exception ex)
     {
         throw new Exception("Errors during delete:\n" + ex.Message);
     }
 }
示例#3
0
        /// <summary>
        /// Delete the supplied product and save file.
        /// </summary>
        /// <param name="product">The product to be delete.</param>
        private void DeleteAndSaveFile(GBRProduct product)
        {
            // get all
            List <GBRProduct> list = GBRGetProducts();

            // prevent duplicate
            list.RemoveAll(p => p.ProductName.ToLower().Equals(product.ProductName.ToLower()));
            using (writer = new StreamWriter(FILE_NAME, false))
            {
                list.ForEach(p => writer.WriteLine(p.ToString()));
            }
        }
示例#4
0
        /// <summary>
        /// Attempts to write on file
        /// </summary>
        /// <exception cref="Exception">With all errors in the message.</exception>
        public void GBRAdd(GBRProduct product)
        {
            product.isInsert = true;

            try
            {
                product.SaveToFile(product);
            }
            catch (Exception ex)
            {
                throw new Exception("Errors during insertion:\n" + ex.Message);
            }
        }
示例#5
0
        /// <summary>
        /// Save this instance to file
        /// </summary>
        private void SaveToFile(GBRProduct product, bool isUpdate = false)
        {
            Edit(); // try to validate the product
            // get all
            List <GBRProduct> list = GBRGetProducts();

            if (isUpdate)
            {
                // prevent duplicate
                list.RemoveAll(p => p.ProductName.ToLower().Equals(product.ProductName.ToLower()));
            }
            list.Add(product);
            using (writer = new StreamWriter(FILE_NAME, false))
            {
                list.ForEach(p => writer.WriteLine(p.ToString()));
            }
        }