程序功能如下:
1.当单击“创建VIP账户”按钮时,显示如图5-11所示的信息,其中卡号为随机生成的一个在500000到999999之间的一个值,余额初始化为10000元。
2.在“取款”文本框中输入取款金额后,单击“取款”按钮,显示如图5-12所示的信息。如果余额不足,VIP用户可以透支1000元,如取款800,而余额是400,则显示如图5-13所示的信息。如透支超过1000元,如取款1600,而余额是400,则显示如图5-14所示的信息。
3.要求:在上机实验4-3的基础上,通过继承和多态实现上述操作。
C#界面如下:
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 WindowsFormsApp8 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Account account; //定义一个账户类对象 private void btnCrtSavingAc_Click(object sender, EventArgs e) { account = new Account();//实例化储蓄卡用户账户 string message = string.Format("创建账户成功,用户卡号为:{0}", account.CreditNo); lblShow.Text = "\n" + message +"\n"; } private void btnWithDraw_Click(object sender, EventArgs e)//取款 { string message; if (account == null) message = "请先创建账户!"; //帐户不存在 else if (txtWithDraw.Text == "") message = "请输入取款金额"; //未输入取款金额 else { decimal money = decimal.Parse(txtWithDraw.Text); account.WithDraw(money, out message); //message存放取款的金额信息 } lblShow.Text = "\n" + message + "\n"; } private void btnDeposit_Click(object sender, EventArgs e) { string message; if (account == null) message = "请先创建账户!"; //帐户是否存在 else if (txtDeposit.Text == "") message = "请输入存款金额"; //输入存款金额 else { decimal money = decimal.Parse(txtDeposit.Text); account.Deposit(money, out message); } lblShow.Text = "\n" + message + "\n";//输出存款金额 } private void button2_Click(object sender, EventArgs e)//查询余额操作 { string message; if (account == null) message = "请先创建账户!"; else { account.show( out message); } lblShow.Text = "\n" + message + "\n"; //输出余额 } private void btnCrtVipAc_Click(object sender, EventArgs e) { account = new VipAccount(); //实例化VIP用户账户 int accountNo = account.CreditNo; string message = string.Format("创建VIP账户成功,用户卡号为:{0}",accountNo); lblShow.Text = "\n" + message + "\n"; } } public class Account { //成员字段的属性改为protected protected int creditNo; protected decimal balance; public Account() { Random r = new Random(); creditNo = r.Next(100000, 500000); //产生一个100000到500000的随机数 balance = 100; //余额初始化为100 } public decimal Balance //只读属性,读取余额 { get { return this.balance; } } public int CreditNo //只读属性,读取储蓄卡号 { get { return this.creditNo; } } //改写Account类的WithDraw为虚方法 public virtual bool WithDraw(decimal money, out string message) //取款 { if (money < 0) //取款金额小于0 { message = "操作失败:\n输入金额不正确!"; return false; } else if (balance >= money) //取款金额大于余额 { balance -= money; message = "操作成功!\n取款" + money + "元"; return true; } else { message = "操作失败!\n余额不足!"; return false; } } public bool Deposit(decimal money, out string message) { if (money < 0) { message = "操作失败:\n输入金额不正确!"; return false; } else { balance += money; message = "操作成功!\n存款" + money + "元"; return true; } } public bool show(out string message) { message = "您的余额为:\n" + balance + "元"; return true; } } public class VipAccount : Account { public VipAccount() { Random r = new Random(); creditNo = r.Next(500000, 1000000); balance = 10000; } public override bool WithDraw(decimal money, out string message) { if (money < 0) { message = "操作失败:\n输入金额不正确!"; return false; } else if (balance >= money) { balance -= money; message = "操作成功!\n取款" + money + "元"; return true; } else if (balance+1000 >= money) //取款金额大于于可透支余额 { balance -= money; message = "操作成功!\n取款" + money + "元,透支"+(-balance)+"元"; return true; } else { message = "操作失败!\n余额不足!"; return false; } } } }