ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计的银行账户管理系统,增加一个VIP账户的管理

2021-04-10 17:57:45  阅读:229  来源: 互联网

标签:return 管理系统 money 账户 VIP message balance public 取款


程序功能如下:
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;
            }
        }
    }
}


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

标签:return,管理系统,money,账户,VIP,message,balance,public,取款
来源: https://blog.csdn.net/The_Handsome_Sir/article/details/115580889

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有