• 根据总用量计算每种包装规格的购买量和总价 Virus


      

      最近有这么一个需求,就是给出客户需要的总量,然后根据数据库记录的包装规格,计算出客户需要购买的包装规格种类和个数,而且要保证客户的花费最小。

      示例图片效果

      示例代码实现如下。欢迎大家一起讨论。

      

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.
    Text;
    using System.Web.Security;
    using System.Reflection;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string
    [] args)
            {

                
    decimal area = 110;
                Console.WriteLine("你的土地面积是:{
    0}", area);
                
    decimal amount = 10m;
                Console.WriteLine("你的每亩用量是是:{
    0}", amount);
                
    decimal budget = 0;

                
    decimal budgetMin = 0;

                List
    <product> products = new List<product>();
                product product1 
    = new product()
                {
                    Id 
    = 1,
                    Name 
    = "土豆",
                    PkgSpecs 
    = new List<pkgspec>(){
                      new pkgspec() { amount 
    = 202, price = 120, id = 2 },
                      new pkgspec() { amount 
    = 300, price = 40, id = 1 },
                      new pkgspec() { amount 
    = 250, price = 80, id = 3 }}
                };
                product1.PkgSpecs.Sort(new Comparison
    <pkgspec>(compareAmountDesc));
                products.
    Add(product1);

                product product2
    = new product (){ Id=1, Name="白菜", PkgSpecs =new List<pkgspec>(){
                      new pkgspec() { amount 
    = 200, price = 180, id = 2 },
                      new pkgspec() { amount 
    = 100, price = 100, id = 1 },
                      new pkgspec() { amount 
    = 250, price = 250, id = 3 }}};
                product2.PkgSpecs.Sort(new Comparison
    <pkgspec>(compareAmountDesc));
                products.
    Add(product2);

                product  product3
    = new product (){ Id=1, Name="萝卜", PkgSpecs =new List<pkgspec>(){
                      new pkgspec() { amount 
    = 200, price = 210, id = 2 },
                      new pkgspec() { amount 
    = 100, price = 150, id = 1 },
                      new pkgspec() { amount 
    = 250, price = 220, id = 3 }}};
                product3.PkgSpecs.Sort(new Comparison
    <pkgspec>(compareAmountDesc));
                products.
    Add(product3);

                
    //全部商品需要的购买规格和预算
                List
    <productSelect> proselects = new List<productSelect>();
                
    //最终选中的商品
                productSelect proselect 
    = null;
                
    //从一个商品选中的包装
                List
    <pkgselect> pkgselect = null;
                
    //总共的用量
                
    decimal totalAmount = area * amount;
               
                Console.WriteLine("你的总共用量是:{
    0}", totalAmount);
                
    //选中的包装个数
                
    int num = 0;
               
                foreach (product p 
    in products)
                {
                    
    //计算一个商品包装规格及用量
                    budget
    =0;
                    totalAmount 
    = area * amount;
                    pkgselect 
    = new List<pkgselect>();
                    proselect 
    = new productSelect()
                    {
                        Id 
    = p.Id,
                        PkgSpecs 
    = new List<pkgselect>(),
                        Budget 
    = budget,
                        Name 
    = p.Name
                    };
                                        
                    foreach (pkgspec pkg 
    in p.PkgSpecs )
                    {
                        
    if (totalAmount % pkg.amount == totalAmount)
                        {
                            num 
    = 1;
                            budget 
    += num * pkg.price;
                            pkgselect.
    Add(new pkgselect() { id = pkg.id, num = num, price = pkg.price });
                            
    break;
                        }
                        
    else if (totalAmount % pkg.amount == 0)
                        {
                            num 
    = (int)(totalAmount / pkg.amount);
                            budget 
    += num * pkg.price;
                            pkgselect.
    Add(new pkgselect() { id = pkg.id, num = (int)(totalAmount / pkg.amount), price = pkg.price });
                            
    break;
                        }
                        
    else
                        {
                            num 
    = (int)(totalAmount / pkg.amount);
                            budget 
    += num * pkg.price;
                            pkgselect.
    Add(new pkgselect() { id = pkg.id, num = num, price = pkg.price });
                            totalAmount 
    = totalAmount - num * pkg.amount;
                            
    continue;
                        }
                    }
                    proselect.PkgSpecs 
    = pkgselect;
                    proselect.Budget 
    = budget;
                    proselects.
    Add(proselect);
                }
                proselects.Sort(compareBudgetAsc);


                foreach (productSelect s 
    in proselects)
                {
                    Console.WriteLine("商品 {
    0} 需要 {1} 个包装规格,总价是 {2}", s.Name, s.PkgSpecs.Count, s.Budget);
                }
                Console.WriteLine("最便宜的是商品 {
    0} ", proselects[0].Name);

                foreach (pkgselect s 
    in proselects[0].PkgSpecs)
                {
                    Console.WriteLine("\t规格 {
    0} , 单价 {1}, 需要购买的数量 {2}", s.id, s.price, s.num);
                }
                Console.ReadLine();
            }
            
    /// <summary>
            
    /// 从低到高
            
    /// </summary>
            
    /// <param name="pkg1"></param>
            
    /// <param name="pkg2"></param>
            
    /// <returns></returns>
            private static 
    int compareAmountAsc(pkgspec pkg1, pkgspec pkg2)
            {
                
    if (pkg1.amount > pkg2.amount)
                    
    return 1;
                
    if (pkg1.amount < pkg2.amount)
                    
    return -1;
                
    else
                    
    return 0;
            }
            
    /// <summary>
            
    /// 从高到低
            
    /// </summary>
            
    /// <param name="pkg1"></param>
            
    /// <param name="pkg2"></param>
            
    /// <returns></returns>
            private static 
    int compareAmountDesc(pkgspec pkg1, pkgspec pkg2)
            {
                
    if (pkg1.amount < pkg2.amount)
                    
    return 1;
                
    if (pkg1.amount > pkg2.amount)
                    
    return -1;
                
    else
                    
    return 0;
            }
            
    /// <summary>
            
    /// 从低到高
            
    /// </summary>
            
    /// <param name="pkg1"></param>
            
    /// <param name="pkg2"></param>
            
    /// <returns></returns>
            private static 
    int compareBudgetAsc(productSelect pro1, productSelect pro2)
            {
                
    if (pro1.Budget > pro2.Budget) return 1;
                
    if (pro1.Budget < pro2.Budget)
                    
    return -1;
                
    else
                    
    return 0;
            }
            
    /// <summary>
            
    /// 从高到低
            
    /// </summary>
            
    /// <param name="pkg1"></param>
            
    /// <param name="pkg2"></param>
            
    /// <returns></returns>
            private static 
    int compareBudgetDesc(productSelect pro1, productSelect pro2)
            {
                
    if (pro1.Budget < pro2.Budget) return 1;
                
    if (pro1.Budget > pro2.Budget)
                    
    return -1;
                
    else
                    
    return 0;
            }
        }
        
    /// <summary>
        
    /// 选中的包装
        
    /// </summary>
        class pkgselect
        {
            
    /// <summary>
            
    /// 选中ID
            
    /// </summary>
            
    public int id;
            
    /// <summary>
            
    /// 购买数量
            
    /// </summary>
            
    public int num;
            
    /// <summary>
            
    /// 单价
            
    /// </summary>
            
    public decimal price;
        }
        
    /// <summary>
        
    /// 包装类型
        
    /// </summary>
        class pkgspec : IComparable
    <pkgspec>
        {
            
    /// <summary>
            
    /// 编号
            
    /// </summary>
            
    public int id;
            
    /// <summary>
            
    /// 包装量
            
    /// </summary>
            
    public decimal amount;
            
    /// <summary>
            
    /// 价格
            
    /// </summary>
            
    public decimal price;

            #region IComparable
    <pkgspec> Members
            
    /// <summary>
            
    /// List<pkgspec>.Sort()默认从低到高
            
    /// </summary>
            
    /// <param name="other"></param>
            
    /// <returns></returns>
            
    public int CompareTo(pkgspec other)
            {
                
    if (this.amount > other.amount)
                    
    return 1;
                
    if (this.amount == other.amount)
                    
    return 0;
                
    else
                    
    return -1;
            }

            #endregion
        }
        
    /// <summary>
        
    /// 代售商品
        
    /// </summary>
        class product
        {
            
    /// <summary>
            
    /// 商品ID
            
    /// </summary>
            
    public int Id { set; get; }
            
    /// <summary>
            
    /// 商品名称
            
    /// </summary>
            
    public string Name { set; get; }
            
    /// <summary>
            
    /// 商品包装规格
            
    /// </summary>
            
    public List<pkgspec> PkgSpecs { set; get; }
        }
        
    /// <summary>
        
    /// 选中的商品
        
    /// </summary>
         class productSelect : IComparable
    <productSelect>
        {
    /// <summary>
            
    /// 商品ID
            
    /// </summary>
            
    public int Id { set; get; }
            
    /// <summary>
            
    /// 商品名称
            
    /// </summary>
            
    public string Name { set; get; }
            
    /// <summary>
            
    /// 需要购买的包装规格
            
    /// </summary>
            
    public List<pkgselect> PkgSpecs { set; get; }
            
    /// <summary>
            
    /// 总价
            
    /// </summary>
            
    public decimal Budget { get; set; }

            #region IComparable
    <productSelect> Members
             
    /// <summary>
            
    /// List<productSelect>.Sort()默认从低到高
             
    /// </summary>
             
    /// <param name="other"></param>
             
    /// <returns></returns>
            
    public int CompareTo(productSelect other)
            {
                
    if (this.Budget > other.Budget)
                    
    return 1;
                
    if (this.Budget < other.Budget)
                    
    return -1;
                
    else
                    
    return 0;
            }

            #endregion
        }
    }
  • 相关阅读:
    python内建eval, exec 和exec函数的用法
    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEditor为例)
    wxPython控件学习之wx.ComboBox
    tar 解压缩命令
    ios是否安装了某应用
    iPhone IOS中DEB如何安装
    一种简便获取iPhone IMEI 的方法
    iOSOpenDevtroubleshoot
    Mac&iOS Socket编程
    Debian Binary Package Building HOWTO
  • 原文地址:https://www.cnblogs.com/virusswb/p/1692441.html
Copyright © 2020-2023  润新知