• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

WinFormC#实现商场收银软件,从面向过程到面向对象,设计模式的应用

武飞扬头像
JosieBook
帮助1


前言

实现商场收银系统从简单的面向过程到面向对象的演变。


一、收银系统版本1

最容易想到的:
单价*数量=总价

根据输入的单价和数量,直接计算,将结果显示在listbox控件中。
重置按钮可以清零。

1、运行效果

学新通

2、界面设计

学新通

3、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 商场收银软件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public double total = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            double price, number,totalPrice;
            price = Convert.ToDouble(this.txtPrice.Text);
            number = Convert.ToDouble(this.txtNumber.Text);
            totalPrice = price * number;
            total  = totalPrice;

            this.lbxList.Items.Add("单价:"   price   "元    数量:"   number   "    合计:"   totalPrice   "元");
            this.labTotal.Text ="共计:"  total   "元";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.txtNumber.Text = "";
            this.txtPrice.Text = "";
            this.lbxList.Items.Clear();
            this.labTotal.Text = "共计:";
        }
    }
}

二、收银系统版本2

版本2在版本1的基础上增加了打折优惠。

1、运行效果

打折下拉框可选择正常收费、三折优惠、五折优惠、八折优惠。
学新通

2、界面设计

在版本1的界面基础上增加一个Combobox,并增加下拉选项。
学新通

3、代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 商场收银软件V2._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.cbxType.SelectedIndex = 0;
        }

        public double total = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double price, number, totalPrice = 0;
                price = Convert.ToDouble(this.txtPrice.Text);
                number = Convert.ToDouble(this.txtNumber.Text);

                switch (this.cbxType.SelectedIndex)
                {
                    case 0:
                        {
                            totalPrice = price * number;
                            break;
                        }
                    case 1:
                        {
                            totalPrice = price * number * 0.3;
                            break;
                        }
                    case 2:
                        {
                            totalPrice = price * number * 0.5;
                            break;
                        }
                    case 3:
                        {
                            totalPrice = price * number * 0.8;
                            break;
                        }
                }

                total  = totalPrice;

                this.lbxList.Items.Add("单价:"   price   "元    数量:"   number   "    合计:"   totalPrice   "元");
                this.labTotal.Text = "共计:"   total   "元";
            }
            catch
            {
                MessageBox.Show("出现异常!","注意!",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.txtNumber.Text = "";
            this.txtPrice.Text = "";
            this.lbxList.Items.Clear();
            this.labTotal.Text = "共计:";
        }
    }
}

三、收银系统版本3

版本3中收费方式增加了满减选项。

采用面向对象设计中的简单工厂模式。

1、运行效果

学新通

2、界面设计

界面收费方式Combobox中增加满300返100选项。
学新通

类图关系如下:

抽象工厂模式:创建相关或依赖对象的家族,而无需明确指定具体类。

  1. 先写一个现金抽象类CashSuper,其中有一个方法acceptCash();
  2. 其次正常收费、打折、满减三种情况分为三个类,对抽象类CashSuper进行实现。
  3. 打折类和满减类中增加了一些新的字段、函数。
  4. 在工厂类CashFactory中将正常收费、打折类、满减类以类对象为单位进行分类传输数据,计算,然后返回不同的类对象。将单价直接传入各个类中的函数acceptCash,然后根据不同的优惠方式对单价进行不同的计算,最后在按钮里根据数量计算总价。
  5. 体现了封装思想:将不同的优惠封装起来,只在单价的基础上计算优惠后的单价,在封装之外根据数量计算总价。

学新通

3、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 商场收银软件V3._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.cbxType.SelectedIndex = 0;
        }

        public double total = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double price, number, totalPrice = 0;
                string type;
                price = Convert.ToDouble(this.txtPrice.Text);
                number = Convert.ToDouble(this.nudNumber.Text);
                type = this.cbxType.SelectedItem.ToString();

                CashSuper cs = null;
                cs = CashFactory.createCashAccept(type);
                totalPrice = cs.acceptCash(price) * number;
                total  = totalPrice;
                this.lbxList.Items.Add("单价:"   price   "元    数量:"   number   "    合计:"   totalPrice   "元" "(" type ")");
                this.labTotal.Text = "共计:"   total   "元";
            }
            catch
            {
                MessageBox.Show("出现异常!", "注意!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.nudNumber.Text = "0";
            this.txtPrice.Text = "";
            this.lbxList.Items.Clear();
            this.labTotal.Text = "共计:";
        }
    }
    
    /// <summary>
    /// 具体实施方法
    /// </summary>
    abstract class CashSuper            //抽象类
    {
        public abstract double acceptCash(double monery);           //抽象方法
    }

    class CashNormal : CashSuper
    {
        public override double acceptCash(double monery)
        {
            return monery;
        }
    }

    class CashRebate : CashSuper
    {
        public double moneryRebate;
        public CashRebate(string moneryRebate)
        {
            this.moneryRebate = double.Parse(moneryRebate);
        }

        public override double acceptCash(double monery)
        {
            return monery*moneryRebate;
        }
    }

    class CashReturn : CashSuper
    {
        public double moneryCondition;
        public double moneryReturn;
        public CashReturn(string moneryCondition, string moneryReturn)
        {
            this.moneryCondition = double.Parse(moneryCondition);
            this.moneryReturn = double.Parse(moneryReturn);
        }

        public override double acceptCash(double monery)
        {
            double result;
            result = monery;
            if (monery >= moneryCondition)
            {
                result= monery - (monery / moneryCondition) * moneryReturn;
            }
            return result;
        }
    }

    /// <summary>
    /// 现金收取工厂
    /// </summary>
    class CashFactory
    {
        public static CashSuper createCashAccept(string type)
        {
            CashSuper cs = null;

            switch (type)
            {
                case "正常收费":
                    cs = new CashNormal();
                    break;
                case "五折优惠":
                    cs = new CashRebate("0.5");
                    break;
                case "八折优惠":
                    cs = new CashRebate("0.8");
                    break;
                case "满300返100":
                    cs = new CashReturn("300","100");
                    break;
            }
            return cs;
        }
    }
}

四、收银系统版本4

1、运行效果

学新通

2、界面设计

界面设计与之前版本差不多。
学新通
类图:
学新通

策略模式:定义一系列算法,把他们封装起来,并且使它们可以相互替换。

以价格和数量为参数,传入对应的优惠类中,并在各自类中完成计算总价的算法。
在外部通过传入参数进行计算总价。

3、代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 商场收银软件V4._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.cbxType.SelectedIndex = 0;
        }

        public double total = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double price, number, totalPrice = 0;
                string type;
                price = Convert.ToDouble(this.txtPrice.Text);
                number = Convert.ToDouble(this.nudNumber.Text);
                type = this.cbxType.SelectedItem.ToString();

                CashContext cc = new CashContext() ;
                switch (type)
                {
                    case "正常收费":
                        cc.setBehavior( new CashNormal());
                        break;
                    case "五折优惠":
                        cc.setBehavior(new CashRebate("0.5"));
                        break;
                    case "八折优惠":
                        cc.setBehavior(new CashRebate("0.8"));
                        break;
                    case "满300返100":
                        cc.setBehavior( new CashReturn("300", "100"));
                        break;
                }

                totalPrice = cc.GetResult(price) * number;
                total  = totalPrice;
                this.lbxList.Items.Add("单价:"   price   "元    数量:"   number   "    合计:"   totalPrice   "元"   "("   type   ")");
                this.labTotal.Text = "共计:"   total   "元";
            }
            catch
            {
                MessageBox.Show("出现异常!", "注意!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.nudNumber.Text = "0";
            this.txtPrice.Text = "";
            this.lbxList.Items.Clear();
            this.labTotal.Text = "共计:";
        }
    }

    /// <summary>
    /// 具体实施方法
    /// </summary>
    abstract class CashSuper            //抽象类
    {
        public abstract double acceptCash(double monery);           //抽象方法
    }

    class CashNormal : CashSuper
    {
        public override double acceptCash(double monery)
        {
            return monery;
        }
    }

    class CashRebate : CashSuper
    {
        public double moneryRebate;
        public CashRebate(string moneryRebate)
        {
            this.moneryRebate = double.Parse(moneryRebate);
        }

        public override double acceptCash(double monery)
        {
            return monery * moneryRebate;
        }
    }

    class CashReturn : CashSuper
    {
        public double moneryCondition;
        public double moneryReturn;
        public CashReturn(string moneryCondition, string moneryReturn)
        {
            this.moneryCondition = double.Parse(moneryCondition);
            this.moneryReturn = double.Parse(moneryReturn);
        }

        public override double acceptCash(double monery)
        {
            double result;
            result = monery;
            if (monery >= moneryCondition)
            {
                result = monery - (monery / moneryCondition) * moneryReturn;
            }
            return result;
        }
    }

    /// <summary>
    /// 收费策略Context
    /// </summary>
    class CashContext
    {
        public CashSuper cs; //声明一个现金收费父类对象

        public void setBehavior(CashSuper csuper) //设置策略行为,参数为具体的现金收费子类(正常,打折或返利)
        {
            this.cs = csuper;
        }

        public double GetResult(double money) //得到现金促销计算结果(利用了多态机制,不同的策略行为导致不同的结果)
        {
            return cs.acceptCash(money);
        }
    }
}


总结

使用设计模式可以增强代码的可重用性、可扩充性、 可维护性、灵活性好。我们使用设计模式最终的目的是实现代码的高内聚和低耦合

面向对象23中设计模式总结

面向对象23中设计模式总结如下:

学新通

1、创建型模式

对象实例化的模式,创建型模式用于解耦对象的实例化过程。

单例模式:某个类智能有一个实例,提供一个全局的访问点。
工厂方法模式:一个工厂类根据传入的参量决定创建出哪一种产品类的实例。
抽象工厂模式:创建相关或依赖对象的家族,而无需明确指定具体类。
建造者模式:封装一个复杂对象的创建过程,并可以按步骤构造。
原型模式:通过复制现有的实例来创建新的实例。

2、结构型模式

把类或对象结合在一起形成一个更大的结构。

装饰器模式:动态的给对象添加新的功能。
代理模式:为其它对象提供一个代理以便控制这个对象的访问。
桥接模式:将抽象部分和它的实现部分分离,使它们都可以独立的变化。
适配器模式:将一个类的方法接口转换成客户希望的另一个接口。
组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。
外观模式:对外提供一个统一的方法,来访问子系统中的一群接口。
享元模式:通过共享技术来有效的支持大量细粒度的对象。

3、行为型模式

类和对象如何交互,及划分责任和算法。

策略模式:定义一系列算法,把他们封装起来,并且使它们可以相互替换。
模板方法模式:定义一个算法结构,而将一些步骤延迟到子类实现。
命令模式:将命令请求封装为一个对象,使得可以用不同的请求来进行参数化。
迭代器模式:一种遍历访问聚合对象中各个元素的方法,不暴露该对象的内部结构。
观察者模式:对象间的一对多的依赖关系。
仲裁者模式:用一个中介对象来封装一系列的对象交互。
备忘录模式:在不破坏封装的前提下,保持对象的内部状态。
解释器模式:给定一个语言,定义它的文法的一种表示,并定义一个解释器。
建造者模式:允许一个对象在其对象内部状态改变时改变它的行为。
责任链模式:将请求的发送者和接收者解耦,使的多个对象都有处理这个请求的机会。
访问者模式:不改变数据结构的前提下,增加作用于一组对象元素的新功能。

设计模式关系图

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkbgg
系列文章
更多 icon
同类精品
更多 icon
继续加载