C # simple store cash register system v1.1 simple factory implementation (2-2)
- Original:
- C # simple store cash register system v1.0
- just now:
- Before using the factory pattern to store cash register system v1.0 upgrade
Before you can reference C # simple store cash register system v1.0 essays
Add CashSuper class
the using the System; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace store cash register software { abstract class CashSuper // cash charge abstract class { public abstract Double acceptCash ( Double Money) ; // abstract method parameters for the original cash receipt, return the current price } }
Add CasHNormal class, and class references CashSuper
the using the System; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace store cash register software { class CasHNormal: CashSuper // normal fee category { public the override Double acceptCash ( Double Money) { return Money; // normal fee, return List } } }
Add CashRebate class, and class references CashSuper
the using the System; the using the System.Collections.Generic; the using the System.Linq; the using the System.Text; the using System.Threading.Tasks; namespace store cash register software { class CashRebate: CashSuper // discount a charge if the class { Private Double moneyRebate = 1D; public CashRebate ( String moneyRebate) { // discounts costs initialized // must enter the discount rate 20% as the this .moneyRebate = Double .Parse (moneyRebate); } public the override Double acceptCash(double money) { return money * moneyRebate; } } }
Add CashRrturn class, and class references CashSuper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { class CashReturn : CashSuper //返利收费子类 { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CashReturn(string moneyCondition,string moneyReturn) { this.moneyCondition = double.Parse(moneyCondition); this.moneyReturn = double.Parse(moneyReturn); } public override double acceptCash(double money) { double result = money; if (money>=moneyCondition)//若大于返利,就减去返利值 { result = money - Math.Floor(money / moneyCondition) * moneyReturn; } return result; } } }
Add CashFactory class
a using System; a using System.Collections.Generic; a using System.Linq; a using System.Text; a using System.Threading.Tasks; namespace store cash register software { class CashFactory // cash charges factory class { public static CashSuper createCashAccept ( String of the type) { CS CashSuper = null ; Switch (type) { Case " normal charge " : CS = new new CasHNormal (); BREAK ; Case "满300返100": cs = new CashReturn("300","100"); break; case "打八折": cs = new CashRebate("0.8"); break; } return cs } } }
The main client class
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 商城收银软件 { public partial class Form1 : Form //客户端类 { public Form1() { InitializeComponent(); } double total = 0.0d;//Declare a double variable, with a total calculated total Private void the button1_Click ( Object SENDER, EventArgs E) { CashSuper csuper = CashFactory.createCashAccept (cbxType.SelectedItem.ToString ()); double totalPrices = 0D; totalPrices = csuper.acceptCash (the Convert. ToDouble (txtPrice.Text) * Convert.ToDouble (txtNum.Text)); Total = Total + totalPrices; lbxList.Text = " Price: " + + txtPrice.Text " number: " + txtNum.Text + " " + cbxType. + SelectedItem " total:" + TotalPrices.ToString (); lblResult.Text = total.ToString (); } Private void the Form1_Load ( Object SENDER, EventArgs E) { cbxType.Items.AddRange ( new new Object [] { " normal charge " , " 20% discount " , " full 300 back to 100 " }); cbxType.SelectedIndex = 0 ; } } }
Summary object-oriented simple factory instance
Add a number of different types of merchandise discounts
Defined factory class
The client is instantiated out
See, knocking slowly can comprehend the design patterns