C#语言和数据库技术基础 MyBank

 
 
 //User类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class User
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string ident;     //身份证号

        public string Ident
        {
            get { return ident; }
            set { ident = value; }
        }

        private double blance;     //余额

        public double Blance
        {
            get { return blance; }
            set { blance = value; }
        }

        private string account;   //账号

        public string Account
        {
            get { return account; }
            set { account = value; }
        }



        public double Minu(double money)         //取款方法
        {
            if (money > 0)
            {
                if (money <= blance)
                {
                    blance -= money;
                    return blance;
                }
                else
                {
                    return -1;
                }
            }
            else
            {

                return -1;

            }
        }

        public double Snmmbobey(double money)
        {
            if (money > 0)
            {
                blance += money;
                return blance;

            }
            else
            {
                return -1;
            }

        }


    }
}

 
  
 
 
//Bank类
 
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        User[] fell = new User[3];
        public void Initial()
        {
            fell[0] = new User();
            fell[0].Account = "112233";
            fell[0].Name = "王丽丽";
            fell[0].Password = "111111";
            fell[0].Ident = "4561231254";
            fell[0].Blance = 5000;

            fell[1] = new User();
            fell[1].Account = "445566";
            fell[1].Name = "小轩";
            fell[1].Password = "222222";
            fell[1].Ident = "4561231254";
            fell[1].Blance = 4000;

            fell[2] = new User();
            fell[2].Account = "778899";
            fell[2].Name = "小华";
            fell[2].Password = "333333";
            fell[2].Ident = "4561231254";
            fell[2].Blance = 3000;
        }
        public void Shu()
        {
            foreach(User userIe in fell)
            {
                Console.WriteLine("账户姓名{0} , 账号{1} , 身份证号{2} , 余额{3}",userIe.Name,userIe.Account,userIe.Ident,userIe.Blance);
            }

        }



        public void Show()
        {
            string option = "";
            do{
            Console.WriteLine("=============欢迎使用自动银行服务==============");
            Console.WriteLine("0:开户  1:存款  2:取款  3:转账  4:查询余额  5:修改密码  6:退出!");
            Console.WriteLine("===========================================");
            Console.WriteLine("请输入数字:");
            option = Console.ReadLine();
            switch(option)
            {
                case "0":
                    Cear();
                    continue;
                case "1":
                    ck();
                    continue;
                case "2":
                    With();
                    continue;
                case "3":
                    Intput();
                    continue;
                case "4":
                    sufor();
                    continue;
                    case "5":
                    Modif();
                    continue;
                case "6":
                    pout();
                    break;
                default:
                    Console.WriteLine("输入无效!");
                   continue;
            } 
              break;

            }while(true);
        }


        public void Cear()
        {
            //接收输入的数据
            Console.WriteLine("请输入用户名:");
            user.Name = Console.ReadLine();
            user.Account = "123";
            Console.WriteLine("请输入用户密码:");
            user.Password = Console.ReadLine();
            Console.WriteLine("请输入身份证号:");
            user.Ident = Console.ReadLine();
            Console.WriteLine("请输入用户余额:");
            user.Blance = double.Parse(Console.ReadLine());

            Console.WriteLine("账户:{0},用户名:{1},存款余额:{2} 创建成功!",user.Account,user.Name,user.Blance);

        }

        public void ck()         //存款方法
        {

            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.Snmmbobey(money) > 0)
            {
                Console.WriteLine("当前余额为:" + user.Blance);
            }
            else
            {
                Console.WriteLine("存款失败!");
            }
        }
        private User CheckUserByAccount(string account)
        {
            foreach(User user in fell)
            {
                if(user.Account.Equals(account))
                {
                    return user;
                }
            }
            return null;
        }

        private User CheckUser(string account, string pwd)
        {
foreach(User user in fell )
{
    if(user.Account.Equals(account)&&user.Password.Equals(pwd))
    {
        return user;

    }
}
return null;
            
     }


        public  void Modif()    //修改密码
        {
            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;
      }

     }


        //取款方法
        public void With()
        {
            string account = "";  //账号
            string pwd;            //密码
            Console.WriteLine("请输入账号:");
            account = Console.ReadLine();
            if(account.Length == 0)
            {
                Console.WriteLine("输入的账号不正确!");
                return;

            }
    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.Minu(money);
            if (result == -1)
            {
                Console.WriteLine("取款失败!");
            }
            else
            {
                Console.WriteLine("取款成功!当前余额:"+result);
            }

        }

     
           //转账
        public void Intput()
        {
            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("转账操作失败!");
            }

        }

        ///转出账号
        ///转出账号密码
        ///转入账号
        ///转出账户存款余额
        ///转入账号
        ///1:转账成功; -1:转出账号或密码不正确! ;
        ///-2;转入账号不正确!; -3:转账操作失败
        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.Minu(money) == -1)
            {
                return -3;
            }
            //取得余额
            fBalance = userfrom.Blance;
            //存款
            if (userTO.Snmmbobey(money) == -1)
            {
                return -3;
            }

            tBalance = userTO.Blance;

            return 1;

        }

        //查询余额
        public void sufor()
        {              
        
            Console.WriteLine("请输入账号:");
            string account = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string password = Console.ReadLine();
            for (int i = 0; i < fell.Length;i++ )
            {
                if (fell[i].Account.Equals(account) && fell[i].Password.Equals(password))
                {
                    Console.WriteLine("账户的余额为:" +string.Format("{0,-8:F2}", fell[i].Blance));
                    break;
                }
                else if(i==fell.Length-1)
                {
                    Console.WriteLine("账号不存在! 请重新输入!");
                    return;
                }


            }


        }


public void pout()
    {
        Console.WriteLine("退出!");

    }



    }
}


 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Bank y = new Bank();
            y.Initial();
            Console.WriteLine("初始化账号信息成功!");
            y.Shu();
            y.Show();

            Console.ReadLine();




        }
    }
}

 
  
 
  
 
  
 
 

你可能感兴趣的:(C#语言和数据库技术基础 MyBank)