码迷,mamicode.com
首页 > 其他好文 > 详细

Mybank

时间:2018-12-07 12:07:39      阅读:12      评论:0      收藏:0      [点我收藏+]

标签:bool   返回   code   com   clear   def   无效   创建   服务   

MyBank

 

1.首先分析出顾客类,还有银行类。

2.顾客类具有:用户名,账户,密码,余额,身份证号等属性

3.银行类具有:存钱,取钱,转账,查询,更改密码,注册办理等等功能

4.顾客类:(最好运用属性封装字段)

技术分享图片
namespace autoBank
{
    class Person
    {
        public string name;//用户名
        public double money;//余额
        public string password;//密码
        public string idNumber;//身份证号
        public string number; //账户
    }
}
技术分享图片

银行类:

 

技术分享图片
Person[] user = new Person[30];//创建顾客类对象数组
        public void CreatAccount()//初始化并添加顾客的方法
        {
            bool result;
              for(int i=0;i<user.Length;i++)
              {
                  if(user[i]==null)
                  {
                    user[i]=new Person();
                    Console.WriteLine("请输入用户名:");
                    user[i].name = Console.ReadLine();
                    user[i].number = user[i].name;
                    do{
                    Console.WriteLine("请输入密码");
                    user[i].password = Console.ReadLine(); 
                    Console.WriteLine("请再次输入密码:");
                    string passwords = Console.ReadLine();
                    result=IsSame(user[i].password,passwords);
                        if(!result)
                        {
                        Console.WriteLine("两次密码不一致,重新输入:");
                        }
                    }while(!result);
                    Console.WriteLine("请输入身份证号:");
                    user[i].idNumber = Console.ReadLine();
                    Console.WriteLine("请输入存款金额:");
                    user[i].money = int.Parse(Console.ReadLine());
                    Console.WriteLine("账户:{0},用户名:{1},存款金额:{2},创建成功!", user[i].number, user[i].name, user[i].money);
                    break;
                  }
                  
              }
       
        }
        private bool IsSame(string password,string passwords) //判断两次密码是否相同
        {
            if (password==passwords) 
            {
                return true;
            }
            return false;
        }
        public void WithDraw()//取款方法
        {
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string pwd = Console.ReadLine();
            Person a = checkOutAccount(account, pwd);
            if (a != null)
            {
                Console.WriteLine("请输入取款金额");
                double outmoney = double.Parse(Console.ReadLine());
                double result = UserMoney(outmoney, a);
                if (result == -1)
                {
                    Console.WriteLine("取款失败");
                }
                else
                {
                    Console.WriteLine("取款成功,当前金额{0}", a.money);
                }

            }
            else
            {
                Console.WriteLine("账号或密码不存在");
            }
        }     
        private double UserMoney(double outmoney,Person people05)//取款条件的判断
        {
            if (outmoney > 0)
            {
                if (outmoney <= people05.money)
                {
                    people05.money -= outmoney;
                    return people05.money;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }
        }
        public void Show()//显示余额的方法
        {
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string password = Console.ReadLine();
            Person checkIn = checkOutAccount(account, password);
            if (checkIn == null)
            {
                Console.WriteLine("账号或密码错误");
            }
            else
            {
                Console.WriteLine("账户余额是{0}", string.Format("{0:F3}", checkIn.money.ToString()));
            }

        }
        public void Theme()//主页菜单的方法
        {
            Console.WriteLine("=================欢迎使用自动银行服务============================");
            Console.WriteLine("1.存款2.取款3.转账4.查询余额5.退出6.修改密码 7.继续注册账户");
            Console.WriteLine("================================================================");
            do
            {
                Console.WriteLine("请选择:");
                int choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        AddMoney();
                        continue;
                    case 2:
                        WithDraw();
                        continue;
                    case 3:
                        Transfer();
                        continue;
                    case 4:
                        Show();
                        continue;
                    case 5:
                        break;
                    case 6:
                        Change();
                        continue;
                    case 7:
                        CreatAccount();
                        continue;
                    default:
                        Console.WriteLine("输入无效");
                        continue;
                }
                break;
            } while (true);

        }
        public void Transfer()//转账的方法
        {
            Console.WriteLine("请输入转出账号:");
            string outAccount = Console.ReadLine();
            Console.WriteLine("请输入转出账户密码");
            string outPassword = Console.ReadLine();
            Console.WriteLine("请输入转入账号");
            string inAccount = Console.ReadLine();
            Console.WriteLine("请输入转账金额");
            double tranMoney = double.Parse(Console.ReadLine());
            double outMoney = 0, inMoney = 0;
            int result = Back(outAccount, outPassword, inAccount, tranMoney, ref outMoney, ref inMoney);
            if (result == 1)
            {
                Console.WriteLine("转账成功,转出账号{0}余额为:{1},转入账号{2}余额为:{3}", outAccount, outMoney, inAccount, inMoney);
            }
            else if (result == -1)
            {
                Console.WriteLine("转出账户账号或密码错误");
            }
            else if (result == -2)
            {
                Console.WriteLine("转入账号不正确");
            }
            else if (result == -3)
            {
                Console.WriteLine("转账操作失败");
            }
        }
        public void AddMoney()//存款的方法
        {
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();
            Person a = InAccount(account);
            if (a != null)
            {
                Console.WriteLine("请输入存款:");
                int addMoney = int.Parse(Console.ReadLine());
                a.money += addMoney;
                Console.WriteLine("存款成功:余额{0}", a.money);
            }
            else 
            {
                Console.WriteLine("账号不存在");
            }
        } 
        public void Change()//修改密码的方法
        {
            Console.WriteLine("请输入账号:");
            string isAccount = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string isPassword = Console.ReadLine();
            Person c = AChange(isAccount, isPassword);
            if (c != null)
            {
                Console.WriteLine("请输入新密码:");
                string password1 = Console.ReadLine();
                Console.WriteLine("请再次输入密码:");
                string password2 = Console.ReadLine();
                if (PChange(password1, password2, ref c) == null)
                {
                    Console.WriteLine("两次密码不一致");
                }
                else
                {
                    Console.WriteLine("密码修改成功");
                }
            }
            else
            {
                Console.WriteLine("账号或密码错误");
            }
        }
        private Person InAccount(string inAccount)//判断账户是否存在,返回顾客对象
        {
            foreach (Person temp in user)
            {
                if (inAccount == temp.number)
                {
                    return temp;
                }

            }
            return null;
        }
        private int Back(string outAccount, string outPassword, string inAccount, double tranMoney, ref double outMoney, ref double inMoney)
        //判断转账条件 {
            Person a = checkOutAccount(outAccount, outPassword);
            if (a == null)
            {
                return -1;
            }
            Person b = checkInAccount(inAccount, outAccount);
            if (b == null)
            {
                return -2;
            }
            outMoney = checkOutMoney(tranMoney, ref a);
            if (outMoney <= 0)
            {
                return -3;
            }
            inMoney = checkInMoney(ref b, tranMoney);
            if (inMoney < b.money)
            {
                return -3;
            }

            return 1;
        }
        private Person checkOutAccount(string outAccount, string outPassword)//判断转账用户是否存在
        {
            foreach (Person temp in user)
            {
                if (outAccount == temp.number && outPassword == temp.password)
                {
                    return temp;
                }
            }
            return null;
        }
        private Person checkInAccount(string inAccount, string outAccount)//判断收账用户是否存在
        {
            foreach (Person temp in user)
            {
                if (inAccount == temp.number && outAccount != inAccount)
                {
                    return temp;
                }

            }
            return null;
        }
        private double checkOutMoney(double tranMoney, ref Person people01)//转账用户余额的改变
        {
            if (people01 != null)
            {
                if (tranMoney <= people01.money)
                {
                    people01.money -= tranMoney;
                    return people01.money;
                }
                return people01.money;
            }
            return people01.money;
        }
        private double checkInMoney(ref Person people02, double tranMoney)//收账用户余额的改变
        {
            people02.money += tranMoney;
            return people02.money;
        }
        private Person AChange(string account, string oldPassword)//修改密码的用户判断
        {
            foreach (Person temp in user)
            {
                if (account == temp.number && oldPassword == temp.password)
                {
                    return temp;
                }
                return null;
            }
            return null;
        }
        private string PChange(string num1, string num2, ref Person people03)//判断修改密码两次是否相同
        {
            if (num1 == num2)
            {
                people03.password = num1;
                return people03.password;
            }
            return null;
        }

Mybank

标签:bool   返回   code   com   clear   def   无效   创建   服务   

原文地址:https://www.cnblogs.com/mayuan01/p/10080973.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!