前言
實現(xiàn)商場收銀系統(tǒng)從簡單的面向過程到面向?qū)ο蟮难葑儭?/p>
一、收銀系統(tǒng)版本1
最容易想到的:
單價*數(shù)量=總價
根據(jù)輸入的單價和數(shù)量,直接計算,將結(jié)果顯示在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 + "元 數(shù)量:" + 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 = "共計:";
}
}
}
二、收銀系統(tǒng)版本2
版本2在版本1的基礎上增加了打折優(yōu)惠。
1、運行效果
打折下拉框可選擇正常收費、三折優(yōu)惠、五折優(yōu)惠、八折優(yōu)惠。
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 + "元 數(shù)量:" + number + " 合計:" + totalPrice + "元");
this.labTotal.Text = "共計:" + total + "元";
}
catch
{
MessageBox.Show("出現(xiàn)異常!","注意!",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 = "共計:";
}
}
}
三、收銀系統(tǒng)版本3
版本3中收費方式增加了滿減選項。
采用面向?qū)ο笤O計中的簡單工廠模式。
1、運行效果
2、界面設計
界面收費方式Combobox中增加滿300返100選項。
類圖關(guān)系如下:
抽象工廠模式:創(chuàng)建相關(guān)或依賴對象的家族,而無需明確指定具體類。
- 先寫一個現(xiàn)金抽象類CashSuper,其中有一個方法acceptCash();
- 其次正常收費、打折、滿減三種情況分為三個類,對抽象類CashSuper進行實現(xiàn)。
- 打折類和滿減類中增加了一些新的字段、函數(shù)。
- 在工廠類CashFactory中將正常收費、打折類、滿減類以類對象為單位進行分類傳輸數(shù)據(jù),計算,然后返回不同的類對象。將單價直接傳入各個類中的函數(shù)acceptCash,然后根據(jù)不同的優(yōu)惠方式對單價進行不同的計算,最后在按鈕里根據(jù)數(shù)量計算總價。
- 體現(xiàn)了封裝思想:將不同的優(yōu)惠封裝起來,只在單價的基礎上計算優(yōu)惠后的單價,在封裝之外根據(jù)數(shù)量計算總價。
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 + "元 數(shù)量:" + number + " 合計:" + totalPrice + "元"+"("+type+")");
this.labTotal.Text = "共計:" + total + "元";
}
catch
{
MessageBox.Show("出現(xiàn)異常!", "注意!", 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>
/// 現(xiàn)金收取工廠
/// </summary>
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常收費":
cs = new CashNormal();
break;
case "五折優(yōu)惠":
cs = new CashRebate("0.5");
break;
case "八折優(yōu)惠":
cs = new CashRebate("0.8");
break;
case "滿300返100":
cs = new CashReturn("300","100");
break;
}
return cs;
}
}
}
四、收銀系統(tǒng)版本4
1、運行效果
2、界面設計
界面設計與之前版本差不多。
類圖:
策略模式:定義一系列算法,把他們封裝起來,并且使它們可以相互替換。
以價格和數(shù)量為參數(shù),傳入對應的優(yōu)惠類中,并在各自類中完成計算總價的算法。
在外部通過傳入?yún)?shù)進行計算總價。
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 "五折優(yōu)惠":
cc.setBehavior(new CashRebate("0.5"));
break;
case "八折優(yōu)惠":
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 + "元 數(shù)量:" + number + " 合計:" + totalPrice + "元" + "(" + type + ")");
this.labTotal.Text = "共計:" + total + "元";
}
catch
{
MessageBox.Show("出現(xiàn)異常!", "注意!", 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; //聲明一個現(xiàn)金收費父類對象
public void setBehavior(CashSuper csuper) //設置策略行為,參數(shù)為具體的現(xiàn)金收費子類(正常,打折或返利)
{
this.cs = csuper;
}
public double GetResult(double money) //得到現(xiàn)金促銷計算結(jié)果(利用了多態(tài)機制,不同的策略行為導致不同的結(jié)果)
{
return cs.acceptCash(money);
}
}
}
總結(jié)
使用設計模式可以增強代碼的可重用性、可擴充性、 可維護性、靈活性好。我們使用設計模式最終的目的是實現(xiàn)代碼的高內(nèi)聚和低耦合。
面向?qū)ο?3中設計模式總結(jié)
面向?qū)ο?3中設計模式總結(jié)如下:
1、創(chuàng)建型模式
對象實例化的模式,創(chuàng)建型模式用于解耦對象的實例化過程。
單例模式:某個類智能有一個實例,提供一個全局的訪問點。
工廠方法模式:一個工廠類根據(jù)傳入的參量決定創(chuàng)建出哪一種產(chǎn)品類的實例。
抽象工廠模式:創(chuàng)建相關(guān)或依賴對象的家族,而無需明確指定具體類。
建造者模式:封裝一個復雜對象的創(chuàng)建過程,并可以按步驟構(gòu)造。
原型模式:通過復制現(xiàn)有的實例來創(chuàng)建新的實例。
2、結(jié)構(gòu)型模式
把類或?qū)ο蠼Y(jié)合在一起形成一個更大的結(jié)構(gòu)。
裝飾器模式:動態(tài)的給對象添加新的功能。
代理模式:為其它對象提供一個代理以便控制這個對象的訪問。
橋接模式:將抽象部分和它的實現(xiàn)部分分離,使它們都可以獨立的變化。
適配器模式:將一個類的方法接口轉(zhuǎn)換成客戶希望的另一個接口。
組合模式:將對象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。
外觀模式:對外提供一個統(tǒng)一的方法,來訪問子系統(tǒng)中的一群接口。
享元模式:通過共享技術(shù)來有效的支持大量細粒度的對象。
3、行為型模式
類和對象如何交互,及劃分責任和算法。
策略模式:定義一系列算法,把他們封裝起來,并且使它們可以相互替換。
模板方法模式:定義一個算法結(jié)構(gòu),而將一些步驟延遲到子類實現(xiàn)。
命令模式:將命令請求封裝為一個對象,使得可以用不同的請求來進行參數(shù)化。
迭代器模式:一種遍歷訪問聚合對象中各個元素的方法,不暴露該對象的內(nèi)部結(jié)構(gòu)。
觀察者模式:對象間的一對多的依賴關(guān)系。
仲裁者模式:用一個中介對象來封裝一系列的對象交互。
備忘錄模式:在不破壞封裝的前提下,保持對象的內(nèi)部狀態(tài)。
解釋器模式:給定一個語言,定義它的文法的一種表示,并定義一個解釋器。
建造者模式:允許一個對象在其對象內(nèi)部狀態(tài)改變時改變它的行為。
責任鏈模式:將請求的發(fā)送者和接收者解耦,使的多個對象都有處理這個請求的機會。
訪問者模式:不改變數(shù)據(jù)結(jié)構(gòu)的前提下,增加作用于一組對象元素的新功能。文章來源:http://www.zghlxwxcb.cn/news/detail-484563.html
設計模式關(guān)系圖
文章來源地址http://www.zghlxwxcb.cn/news/detail-484563.html
到了這里,關(guān)于【W(wǎng)inForm】C#實現(xiàn)商場收銀軟件,從面向過程到面向?qū)ο?,設計模式的應用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!