练习1:实现MyBank系统的取款功能 teacher

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyBank
{
    public class Bank
    {
        User user = new User();  //实例化User对象并赋值

        /// 
        /// 开户
        /// 
        public void CreateAccount()
        {
            //接受输入的数据
            user._name = "王丽丽";
            user._account = "179708064356";
            user._password = "1234";
            user._identityNum = "2100506198908081847";
            user._balance = 8000;

            Console.WriteLine("账户:{0},用户名:{1},存款金额:{2}创建成功!", user._account, user._name, user._balance);
            Console.WriteLine();
        }

        #region 取款
        /// 
        /// 取款
        /// 
        public void WithDraw()
        {
            string account = "";             //账号
            string pwd;                      //密码

            Console.WriteLine("请输入账号:");
            account = Console.ReadLine();
            if (account.Length == 0)
            {
                Console.WriteLine("输入的账号不正确!");
                return;
            }

            //接收账户密码,并验证
            Console.WriteLine("请输入账户密码:");
            pwd = Console.ReadLine();

            if (user._password != pwd)
            {
                Console.WriteLine("密码有误!");
                return;
            }

            Console.WriteLine("请输入取款金额");
            double money = double.Parse(Console.ReadLine());
            double result = user.MinusMoney(money);
            if (result == -1)//标识符
            {
                Console.WriteLine("取款失败");
            }
            else
            {
                Console.WriteLine("取款成功!当前余额:" + result);
            }
        }
        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyBank
{
    /// 
    /// 用户类
    /// 
    public class User
    {
        /// 
        /// 用户名
        /// 
        public string _name;

        /// 
        /// 密码
        /// 
        public string _password;

        /// 
        /// 身份证号
        /// 
        public string _identityNum;

        /// 
        /// 账户余额
        /// 
        public double _balance;

        /// 
        /// 账户
        /// 
        public string _account;


        /// 
        /// 取款操作
        /// 
        /// 要取的金额
        /// 返回余额,输入有误返回-1
        public double MinusMoney(double money)
        {
            if (money > 0)
            {
                if (money <= _balance)
                {
                    _balance -= money;
                    return _balance;
                }
                else
                {
                    return -1;
                }

            }
            else
            {
                return -1;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyBank
{
    class Program
    {
        static void Main(string[] args)
        {   
            Bank myBank = new Bank();
            //开户
            myBank.CreateAccount();
            //取款
            myBank.WithDraw();
            
            Console.ReadLine();
        }
    }
}

你可能感兴趣的:(练习1:实现MyBank系统的取款功能 teacher)