public static List <WeightGroup> GetAllWeightGroupsFromDb() { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = "Server=dbs-eitdk.database.windows.net;Database=db-eitdk;User Id=admin-eitdk;Password=Eastindia4thewin"; conn.Open(); SqlCommand command = new SqlCommand("SELECT * FROM WeightGroup", conn); List <WeightGroup> weightGroups = new List <WeightGroup>(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { WeightGroup weightGroup = new WeightGroup( float.Parse(reader[1].ToString()), Decimal.Parse(reader[2].ToString()) ); weightGroups.Add(weightGroup); } } return(weightGroups); } }
public static decimal CalculatePrice(Section section, float weight, Category category) { List <WeightGroup> weightGroups = DbHelper.GetAllWeightGroupsFromDb(); try { List <WeightGroup> weightGroups2 = weightGroups .OrderBy(i => i.MaxWeight).ToList(); WeightGroup weightGroup = weightGroups2.First(it => it.MaxWeight >= weight); return(weightGroup.Price * (decimal)category.PriceFactor * section.Length); } catch { throw new InvalidDataException("The weight does not fit into any weight group"); } }