POS模拟器实训
POS模拟器1.实训目的 本节通过模拟各种银行卡在POS机上消费的操作,来了解各种银行卡消费的特点,掌握面向对象程序的类、继承、接口的综合应用,掌握C# Windows编程中简单窗体控件的使用及事件的编程。 2.实训内容 (1) 创建Windows应用程序,将启动窗体命名为FrmPOS。 (2) 窗体设计:在窗体上添加1个Button控件、3个Label控件、2个RadioButton控件、2个ComboBox控件以及l个NumericUpDown控件。其中设置ComboBox的DropDownStyle属性值为DropDownList。控件具体的位置如图14.3所示。 图14.3 POS模拟器窗口设计 ???? (3) 在应用程序中添加相应的类和接口。程序文档结构如图14.4所示,其类图如14.5所示。 ? ? 图14.4 程序文档结构 图14.5 应用程序类图 (4) 选择不同卡种,在不同地区的POS机上消费演示,其运行结果如图14.6所示。 ? 图14.6 消费运行结果图 3.程序代码 (1) 普通支付接口代码IPayable.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //POS机支付接口
interface IPayable { void Pay(decimal money,POS pos); } }
(2) 国际支付接口代码IGlobalPayable.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //支持POS机上国际支付
interface IGlobalPayable { void Pay(decimal money,Common.Currency currency,POS pos); } }
(3) 公共类代码Common.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { class Common { public enum Currency { 人民币,美元,英镑,欧元 } } }
(4) 普通银行卡类代码BankCard.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //普通银行卡,没有支付功能,作为其他卡的基类
class BankCard { protected string id,region; protected decimal balance = 0; public decimal Balance { get { return balance; } } public BankCard(string id,string region) { this.id = id; this.region = region; this.balance=balance; } } }
(5) 银联卡类代码GeneralCard.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //银联卡
class GeneralCard : BankCard,IPayable { public GeneralCard(string id,string region,decimal balance) : base(id,region,balance) { } //银联卡具有支付功能,但必修保证卡上有余额 //银联卡如果跨地区支付需要收1%的手续费
public void Pay(decimal money,POS pos) { decimal cost = 0; if (this.region != pos.Region) cost = money * 0.01M; if (balance >= money + cost) balance -= (money + cost); else
throw new InvalidOperationException("余额不足"); } public override string ToString() { return string.Format("银联卡{0}",id); } } }
(6) 信用卡类代码CreditCard.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //信用卡
class CreditCard : BankCard,IPayable { private decimal credit; public CreditCard(string id,decimal balance,decimal credit) : base(id,balance) { this.credit = credit; } //信用卡可以透支支付,但不能超出其信用额度
public void Pay(decimal money,POS pos) { if (money <= credit + balance) balance -= money; else
throw new InvalidOperationException("超出额度"); } public override string ToString() { return string.Format("信用卡{0}",id); } } }
(7) Visa卡类代码VisaCard.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { //国际信用卡Visa class VisaCard: CreditCard,IGlobalPayable { public VisaCard(string id,balance,credit) { } //Visa卡支付时,支持国际货币支付,可以透支支付 public void Pay(decimal money,POS pos) { decimal rate = 1; if (currency == Common.Currency.美元) rate = 7; else if (currency == Common.Currency.英镑) rate = 13; else if (currency == Common.Currency.欧元) rate = 10; base.Pay(rate * money,pos); } public override string ToString() { return string.Format("Visa卡{0}",id); } } } (8) POS机类代码POS.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS { class POS { private string region; public string Region { get { return region; } } public POS(string region) { this.region = region; } } }
(9) FrmPOS窗体后台代码FrmPOS.cs 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 POS { public partial class FrmPOS : Form { BankCard[] cards; POS pos1,pos2; public FrmPOS() { cards = new BankCard[4]; cards[0] = new GeneralCard("g_bj_001","Beijing",1000); cards[1] = new GeneralCard("g_sh_002","Shanghai",1000); cards[2] = new CreditCard("c_bj_001",1000); cards[3] = new VisaCard("v_sh_001",1000); pos1 = new POS("Beijing"); pos2 = new POS("Shanghai"); InitializeComponent(); } private void Form1_Load(object sender,EventArgs e) { foreach (BankCard card in cards) //将银行卡添加到下拉菜单 cmbCard.Items.Add(card); for (int i = 0; i < 4; i++) //将枚举中的货币类型添加到下拉菜单 cmbCurrency.Items.Add((Common.Currency)i); cmbCard.SelectedIndex = cmbCurrency.SelectedIndex = 0; } //付款按钮事件
private void button1_Click(object sender,EventArgs e) { //确定支付的POS机 POS pos = radioButton1.Checked ? pos1 : pos2; BankCard card = (BankCard)cmbCard.SelectedItem; //获取选择支付的货币种类 Common.Currency cur = (Common.Currency)cmbCurrency.SelectedIndex; try { if (cur == Common.Currency.人民币) ((IPayable)card).Pay(nudMoney.Value,pos); else ((IGlobalPayable)card).Pay(nudMoney.Value,cur,pos); MessageBox.Show("消费成功,余额" + card.Balance); } catch (InvalidCastException) { MessageBox.Show("该卡不支持此项消费"); } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } catch (Exception exp) { MessageBox.Show("消费失败: " + exp.Message); } } } }
4.实训总结 本节的上机实训,是一个面向对象程序设计中类、继承、接口知识以及C# Windows编程的一个综合应用。通过本实训,更深刻体会类、继承、接口在编程中应用。运用C#面向对象编程实现基本功能;运用和windows UI设计(Windows编程)设计POS机的人机交互界面。使用户能够方便的对Windows窗口的界面控件进行选择和操作,实现对程序中数据的输入和显示,通过按钮的事件驱动完成指定的操作来控制程序的运行。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在stderr的Windows上更改文本输出颜色
- 联想win10专业版64位简体中文原版光盘镜像
- 如何在Qt中设置按钮的背景颜色后保留Windows Aero主题?
- windows-server-2003 – 不存在的服务获得启动控制?
- 如何在Windows 10上使用远程桌面执行文件传输?
- 仅限Windows 7上的.NET 4串行端口ObjectDisposedException
- windows-server-2003 – 如何连接到特定的域控制器?
- windows-server-2008 – Windows模块安装程序延迟登录,服务
- windows – Python 3.2 Multiprocessing.Process没有运行目
- windows – 单元测试并启动MongoDb服务器
- 将Uint8Array转换为JavaScript中的数组
- 是什么原因导致WIX安装程序中找不到1723 DLL错误
- windows-server-2008 – 如何在Windows Server 2
- windows-8 – Metro Style应用程序:WinJS必须?
- windows-server-2008-r2 – DHCP服务器只记住一个
- 在Wix中创建自定义操作以用于静默安装
- windows-server-2012 – Windows Server 2012上的
- windows-server-2012-r2 – 将Google Chrome配置
- 在window下安装Redis数据库,并用python链接Redi
- windows – 将NTLM身份验证从本地代理服务器转发