超市收银系统之——3

---恢复内容开始---

超市收银系统

前言:在我们之前的仓库类和超市类的编程完成之后,这时候我们需要考虑打折的方案。

1.不打折 2.打9折  3.打8.5者  4.买300送50  5.买500送100

解决方案:我们可以提供一个打折的抽象的方法。在各个不同的打折类中进行重写打折的方法。

首先我们先定义打折的父类,代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 打折的父类
    /// </summary>
    public abstract class CalFather
    {
        /// <summary>
        /// 计算打折后应付多少钱
        /// </summary>
        /// <param name="realMoney">打折前应付的价钱</param>
        /// <returns>打折后应付的前</returns>
        public abstract double GetTotalMoney(double realMoney);
    }
}

 

不进行打折的类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 不打折 该多少钱就多少钱
    /// </summary>
    class CalNormal:CalFather
    {
        public override double GetTotalMoney(double realMoney)
        {
            return realMoney;
        }
    }
}

按折扣率进行打折,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{
    /// <summary>
    /// 按折扣率打折
    /// </summary>
    class CalRate:CalFather
    {
        /// <summary>
        /// 折扣率
        /// </summary>
        public double Rate
        {
            get;
            set;
        }
        /// <summary>
        /// 打折类的构造函数
        /// </summary>
        /// <param name="rate">折扣率</param>
        public CalRate(double rate)
        {
            this.Rate = rate;
        }
        /// <summary>
        /// 按折扣率进行打折
        /// </summary>
        /// <param name="realMoney">原来的总的价钱</param>
        /// <returns>打折后的价钱</returns>
        public override double GetTotalMoney(double realMoney)
        {
            return realMoney * this.Rate;
        }
    }
}

买300送50和买500送100,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 买M元 送N元
    /// </summary>
    class CalMN : CalFather
    {
        //买500送100
        public double M
        {
            get;
            set;
        }

        public double N
        {
            get;
            set;
        }

        public CalMN(double m, double n)
        {
            this.M = m;
            this.N = n;
        }
        /// <summary>
        /// 得到打折之后的价格
        /// </summary>
        /// <param name="realMoney"></param>
        /// <returns></returns>
        public override double GetTotalMoney(double realMoney)
        {
            //600 -100
            //1000-200
            //1200 
            if (realMoney >= this.M)
            {
                //realMoney / this.M :表示有几个500或者300
                //this.N:送的价格
                return realMoney - (int)(realMoney / this.M) * this.N;
            }
            else
            {
                return realMoney;
            }
        }
    }
}

接着就是在跟用户交互的方法中进行调用了。好的打折的方案的方法就编写完了。

 

---恢复内容结束---

超市收银系统

前言:在我们之前的仓库类和超市类的编程完成之后,这时候我们需要考虑打折的方案。

1.不打折 2.打9折  3.打8.5者  4.买300送50  5.买500送100

解决方案:我们可以提供一个打折的抽象的方法。在各个不同的打折类中进行重写打折的方法。

首先我们先定义打折的父类,代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 打折的父类
    /// </summary>
    public abstract class CalFather
    {
        /// <summary>
        /// 计算打折后应付多少钱
        /// </summary>
        /// <param name="realMoney">打折前应付的价钱</param>
        /// <returns>打折后应付的前</returns>
        public abstract double GetTotalMoney(double realMoney);
    }
}

 

不进行打折的类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 不打折 该多少钱就多少钱
    /// </summary>
    class CalNormal:CalFather
    {
        public override double GetTotalMoney(double realMoney)
        {
            return realMoney;
        }
    }
}

按折扣率进行打折,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{
    /// <summary>
    /// 按折扣率打折
    /// </summary>
    class CalRate:CalFather
    {
        /// <summary>
        /// 折扣率
        /// </summary>
        public double Rate
        {
            get;
            set;
        }
        /// <summary>
        /// 打折类的构造函数
        /// </summary>
        /// <param name="rate">折扣率</param>
        public CalRate(double rate)
        {
            this.Rate = rate;
        }
        /// <summary>
        /// 按折扣率进行打折
        /// </summary>
        /// <param name="realMoney">原来的总的价钱</param>
        /// <returns>打折后的价钱</returns>
        public override double GetTotalMoney(double realMoney)
        {
            return realMoney * this.Rate;
        }
    }
}

买300送50和买500送100,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _16超市收银系统
{

    /// <summary>
    /// 买M元 送N元
    /// </summary>
    class CalMN : CalFather
    {
        //买500送100
        public double M
        {
            get;
            set;
        }

        public double N
        {
            get;
            set;
        }

        public CalMN(double m, double n)
        {
            this.M = m;
            this.N = n;
        }
        /// <summary>
        /// 得到打折之后的价格
        /// </summary>
        /// <param name="realMoney"></param>
        /// <returns></returns>
        public override double GetTotalMoney(double realMoney)
        {
            //600 -100
            //1000-200
            //1200 
            if (realMoney >= this.M)
            {
                //realMoney / this.M :表示有几个500或者300
                //this.N:送的价格
                return realMoney - (int)(realMoney / this.M) * this.N;
            }
            else
            {
                return realMoney;
            }
        }
    }
}

接着就是在跟用户交互的方法中进行调用了。好的打折的方案的方法就编写完了。

 

原文地址:https://www.cnblogs.com/MoRanQianXiao/p/7739854.html