wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了POS模拟器实训大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

POS模拟器 1.实训目的 本节通过模拟各种银行卡在POS机上消费的操作,来了解各种银行卡消费的特点,掌握面向对象程序的类、继承、接口的综合应用,掌握C# Windows编程中简单窗体控件的使用及事件的编程。 2.实训内容 (1) 创建Windows应用程序,将启动窗体命名为FrmPOS。 (2) 窗体设计:在窗体上添加1个Button控件、3个Label控件、2个RadioButton控件、2个

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所示。

POS模拟器实训

 

图14.3 POS模拟器窗口设计

     (3) 在应用程序中添加相应的类和接口。程序文档结构如图14.4所示,其类图如14.5所示。

 

POS模拟器实训

 

图14.4 程序文档结构

图14.5 应用程序类图

(4) 选择不同卡种,在不同地区的POS机上消费演示,其运行结果如图14.6所示。

 

POS模拟器实训

图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 = @H_262_266@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 = @H_262_266@0; if (this.region != pos.Region) cost = money * @H_262_266@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 = @H_262_266@1; if (currency == Common.Currency.美元) rate = @H_262_266@7; else if (currency == Common.Currency.英镑) rate = @H_262_266@13; else if (currency == Common.Currency.欧元) rate = @H_262_266@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[@H_262_266@4]; cards[@H_262_266@0] = new GeneralCard("g_bj_001","Beijing",@H_262_266@1000); cards[@H_262_266@1] = new GeneralCard("g_sh_002","Shanghai",@H_262_266@1000); cards[@H_262_266@2] = new CreditCard("c_bj_001",@H_262_266@1000); cards[@H_262_266@3] = new VisaCard("v_sh_001",@H_262_266@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 = @H_262_266@0; i < @H_262_266@4; i++) //将枚举中的货币类型添加到下拉菜单 cmbCurrency.Items.Add((Common.Currency)i); cmbCard.SELEctedIndex = cmbCurrency.SELEctedIndex = @H_262_266@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窗口的界面控件进行选择和操作,实现对程序中数据的输入和显示,通过按钮的事件驱动完成指定的操作来控制程序的运行。

大佬总结

以上是大佬教程为你收集整理的POS模拟器实训全部内容,希望文章能够帮你解决POS模拟器实训所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。