频道栏目
首页 > 程序开发 > 软件开发 > C# > 正文
MyBank
2017-01-11 09:28:00      个评论    来源:李佳乐的博客  
收藏   我要投稿
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.mingdan();
            Console.WriteLine("初始化帐户信息成功!");
            myBank.caidan();
            myBank.zhuye();
            Console.ReadLine();
        }
    }
}
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;
        
        public double MinusMoney(double money)
        {
            if (money > 0)
            {
                if (money <= _balance)
                {
                    _balance -= money;
                    return _balance;
                }
                else
                {
                    return -1;
                }

            }
            else
            {
                return -1;
            }
        }
        public double SaveMoney(double money)
        {
            if (money > 0)
            {

                _balance += money;
                return _balance;
            }
            else
            {
                return -1;
            }
        }
    
    }
}

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

namespace MyBank {
    public class Bank {
        User user = new User(); //实例化User对象并赋值
        User[] _userGroup = new User[3];
        const string MESSAGE = "操作成功!";
        public void mingdan() {
            _userGroup[0] = new User();
            _userGroup[0]._account = "123456789";
            _userGroup[0]._name = "吴亦凡";
            _userGroup[0]._password = "0000";
            _userGroup[0]._identityNum = "210050619890808185";
            _userGroup[0]._balance = 1000;

            _userGroup[1] = new User();
            _userGroup[1]._account = "12345678910";
            _userGroup[1]._name = "韩东君";
            _userGroup[1]._password = "1111";
            _userGroup[1]._identityNum = "510010619891231127";
            _userGroup[1]._balance = 2000;

            _userGroup[2] = new User();
            _userGroup[2]._account = "0123456789";
            _userGroup[2]._name = "陈伟霆";
            _userGroup[2]._password = "2222";
            _userGroup[2]._identityNum = "410207198904051271";
            _userGroup[2]._balance = 8000;
        }
        /// <summary>
        /// 显示所有账户的账号,姓名,密码,身份证号,账户余额
        /// </summary>
        public void caidan() {
            foreach(User userItem in _userGroup) {
                Console.WriteLine("帐户姓名:" + userItem._name + " 帐号:" + userItem._account + " 存款余额:" + userItem._balance + "密码:" + userItem._password + "身份证号:" + userItem._identityNum);

            }
        }
        /// <summary>
        /// 显示菜单
        /// </summary>
        public void zhuye() {
            string option = "";
            do {
                Console.WriteLine("==================欢迎使用自动银行服务==================");
                Console.WriteLine("1:开户 2:存款 3:取款 4:转账 5:查询余额 6:修改密码 0:退出");
                Console.WriteLine("========================================================");
                option = Console.ReadLine();
                switch (option) {
                case "1":
                    kaihu();
                    continue;
                case "2":
                    cunkuan();
                    continue;
                case "3":
                    qukuan();
                    continue;
                case "4":
                    zhuanzhang();
                    continue;
                case "5":
                    chaxunyue();
                    continue;
                case "6":
                    xiugai();
                    continue;
                case "0":
                    break;
                default:
                    Console.WriteLine("输入无效!");
                    continue;
                }
                break;
            } while ( true );
        }
        /// <summary>
        /// 开户
        /// </summary>
        public void kaihu() {
            //接收输入的数据
            Console.WriteLine("请输入帐户姓名");
            user._name = Console.ReadLine();
            user._account = "179708064356";
            Console.WriteLine("请输入帐户密码");
            user._password = Console.ReadLine();
            Console.WriteLine("请输入帐户身份证号");
            user._identityNum = Console.ReadLine();
            Console.WriteLine("请输入帐户存款金额");
            user._balance = double.Parse(Console.ReadLine());

            Console.WriteLine("帐号:{0},帐户姓名:{1},存款金额:{2} {3}", user._account, user._name, user._balance, MESSAGE);
            //Console.ReadLine();
        }
        /// <summary>
        /// 取款
        /// </summary>
        public void qukuan() {
            string account = ""; //账号
            string pwd; //密码
            Console.WriteLine("请输入帐号:");
            account = Console.ReadLine();
            if (account.Length == 0) {
                Console.WriteLine("输入的帐号不正确!");
                return;
            }
            //新增:在3个账户中查找指定的账户
            User user = CheckUserByAccount(account);
            if (user == null) {
                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);
            }
        }
        public void chaxunyue() {
            Console.WriteLine("请输入用户名:");
            user._name = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            user._password = Console.ReadLine();
            for (int i = 0; i < _userGroup.Length; i++) {
                if (_userGroup[i]._name.Equals(user._name)) {
                    Console.WriteLine(_userGroup[i]._balance);
                    break;
                }
            }
        }
        /// <summary>
        /// 存款
        /// </summary>
        public void cunkuan() {
            string account = ""; //账号
            double money = 0; //存款金额
            Console.WriteLine("请输入账号:");
            account = Console.ReadLine();
            Console.WriteLine("请输入存入金额:");
            money = double.Parse(Console.ReadLine());

            User user;
            if ((user = CheckUserByAccount(account)) == null) {
                Console.WriteLine("您输入的账号不存在!");
            }

            if (user.SaveMoney(money) > 0) {
                Console.WriteLine(MESSAGE + "当前余额:" + user._balance);
            } else {
                Console.WriteLine("存款失败!");
            }

        }
        /// <summary>
        /// 通过账号判断用户是否存在
        /// </summary>
        ///<cke:param name="account"></cke:param>
        /// <returns>存在返回return对象,不存在返回值null</returns>
        private User CheckUserByAccount(string account) {
            foreach(User user in _userGroup) {
                if (user._account == account) {
                    return user;
                }
            }
            return null;
        }
        /// <summary>
        /// 根据账号和密码判定账户是否存在
        /// </summary>
        ///<cke:param name="account"></cke:param>
        ///<cke:param name="pwd"></cke:param>
        /// <returns>User&null</returns>
        private User CheckUser(string account, string pwd) {
            foreach(User user in _userGroup) {
                if (user._account == account && user._password == pwd) {
                    return user;
                }
            }
            return null;
        }
        //修改密码
        public void xiugai() {
            string accountName = ""; //当前账号
            string password = ""; //当前帐户密码
            Console.WriteLine("请输入账号:");
            accountName = Console.ReadLine();
            Console.WriteLine("请输入原密码:");
            password = Console.ReadLine();
            string newPwd1,
            newPwd2;
            Console.WriteLine("请输入新密码:");
            newPwd1 = Console.ReadLine();
            Console.WriteLine("请再次输入新密码:");
            newPwd2 = Console.ReadLine();
            if (CheckPwd(accountName, password, newPwd1, newPwd2) == true) Console.WriteLine("账户密码修改成功!");
            else {
                Console.WriteLine("账户密码修改失败!");
            }
        }
        public bool CheckPwd(string accountName, string password, string newPwd1, string newPwd2) {
            //判断转出的用户是否存在
            User user = CheckUser(accountName, password);
            if (user == null) {
                Console.WriteLine("当前账号不存在!");
                return false;
            }
            if (newPwd1 != newPwd2) {
                Console.WriteLine("两次输入的账户密码不一致!");
                return false;
            } else {
                user._password = newPwd1;
                return true;
            }
        }
        /// <summary>
        /// 转账输入输出信息
        /// </summary>
        public void zhuanzhang() {
            Console.WriteLine("请输入转出账号:");
            string fromAccount = Console.ReadLine();
            Console.WriteLine("请输入转出账号密码:");
            string fromPwd = Console.ReadLine();
            Console.WriteLine("请输入转账账号:");
            string toAccount = Console.ReadLine();
            Console.WriteLine("请输入转账金额:");
            double money = double.Parse(Console.ReadLine());
            double fbalance = 0,
            tbalance = 0;
            //执行转账操作,参数分别为转出账号,密码  转入账号 ,密码  初始化两个值
            int iRet = Transfer(fromAccount, fromPwd, toAccount, money, ref fbalance, ref tbalance);
            if (iRet == 1) {
                Console.WriteLine("转账成功!转出账号:{0}余额为:{1},转入账号{2}余额{3}", fromAccount, fbalance, toAccount, tbalance);
            } else if (iRet == -1) {
                Console.WriteLine("转出账户的账号或密码输入错误!");
            } else if (iRet == -2) {
                Console.WriteLine("转入账号不正确!");
            } else if (iRet == -3) {
                Console.WriteLine("转账操作失败!");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        ///<cke:param name="fromAccount"></cke:param>当前账号
        ///<cke:param name="fromPwd"></cke:param>账号密码
        ///<cke:param name="toAccount"></cke:param>转账账号
        ///<cke:param name="money"></cke:param>转账金额
        ///<cke:param name="fbalance"></cke:param>当前账号余额
        ///<cke:param name="tbalance"></cke:param>
        /// <returns></returns>

        /// <summary>
        /// 
        /// </summary>
        ///<cke:param name="fAccount"></cke:param>转出账号
        ///<cke:param name="fPwd"></cke:param>转出账号密码
        ///<cke:param name="tAccount"></cke:param>转入账号
        ///<cke:param name="money"></cke:param>转账金额
        ///<cke:param name="fBalance"></cke:param>转出账号余额
        ///<cke:param name="tBalance"></cke:param>转入账户存款
        /// <returns>1: 转账成功;-1:转出账号或密码不正确;-2:转入账号或密码不正确;-3:转账失败</returns>
        private int Transfer(string fAccount, string fPwd, string tAccount, double money, ref double fBalance, ref double tBalance) {
            //检查转出账号和密码,如果存在则保存当前要转出用户
            User userfrom = CheckUser(fAccount, fPwd);
            if (userfrom == null) //转出账号或密码不正确
            {
                return - 1;
            }
            //根据账号检查转入账号,如果存在则保存当前转入用户
            User userTo = CheckUserByAccount(tAccount);
            if (userTo == null) {
                return - 2;
            }
            //将保存的用户执行存款
            if (userfrom.MinusMoney(money) == -1) {
                return - 3;
            }
            //取得余额
            fBalance = userfrom._balance;
            //保存当前要转入用户执行存款
            if (userTo.SaveMoney(money) == -1) {
                return - 3;
            }
            //取得余额,
            tBalance = userTo._balance;
            return 1;
        }
    }
}
点击复制链接 与好友分享!回本站首页
上一篇:C#中操作Word(7)—— 如何向Word中插入表格
下一篇:C#中操作Word(8)向Word中插入图表的三种方法(一)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站