using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyBank
{
public class Bank
{
#region 成员变量的定义
User user = new User(); //实例化User对象并赋值
User[] _userGroup = new User[3];
const string MESSAGE = "操作成功!";
#endregion
#region 初始化三个账户
/// <summary>
/// 初始化三个账户
/// </summary>
public void Initial()
{
_userGroup[0] = new User();
_userGroup[0].Account = "2844662307";
_userGroup[0].Name = "★三无汐★";
_userGroup[0].Password = "1234";
_userGroup[0].IdentityNum = "233333333333333333";
_userGroup[0].Balance = 1000;
_userGroup[1] = new User();
_userGroup[1].Account = "1429353362";
_userGroup[1].Name = "乐正风华";
_userGroup[1].Password = "2345";
_userGroup[1].IdentityNum = "233333333333333334";
_userGroup[1].Balance = 2000;
_userGroup[2] = new User();
_userGroup[2].Account = "2080450938";
_userGroup[2].Name = "松";
_userGroup[2].Password = "3456";
_userGroup[2].IdentityNum = "233333333333333335";
_userGroup[2].Balance = 8000;
}
#endregion
#region 显示所有用户的账户信息
/// <summary>
/// 显示所有账户的账号、姓名、密码、身份证号、账户余额
/// </summary>
public void ShowAllUser()
{
foreach (User userItem in _userGroup)
{
Console.WriteLine("账户姓名:" + userItem.Name + " 账号:" + userItem.Account + " 存款余额:" + userItem.Balance + " 密码:" + userItem.Password + " 身份证号:" + userItem.IdentityNum);
}
}
#endregion
#region 显示菜单
/// <summary>
/// 显示菜单
/// </summary>
public void ShowCustomMenu()
{
string option = "";
do
{
Console.WriteLine();
Console.WriteLine("==================欢迎使用自动银行服务==================");
Console.WriteLine("1:存款 2:取款 3:转账 4:查询余额 5:退出");
Console.WriteLine("========================================================");
option = Console.ReadLine();
switch (option)
{
case "1":
//CreateAccount();
//continue;
//case "2":
Deposit();
continue;
case "2":
WithDraw();
continue;
case "3":
InputTransferData();
continue;
case "4":
continue;
case "5":
// ModifyPwd();
// continue;
//case "0":
break; //结束switch
default:
Console.WriteLine("输入无效!");
continue;
}
break; //结束do-while循环
} while (true);
}
#endregion
#region 开户
/// <summary>
/// 开户
/// </summary>
public void CreateAccount()
{
//接受输入的数据
Console.WriteLine("请输入账户姓名");
user.Name = Console.ReadLine();
user.Account = "1149939228";
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} 账户创建成功!", user.Account, user.Name, user.Balance);
//Console.ReadLine();
}
#endregion
#region 取款
/// <summary>
/// 取款
/// </summary>
public void WithDraw()
{
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);
}
}
#endregion
#region 存款
/// <summary>
/// 存款
/// </summary>
public void Deposit()
{
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("存款失败!");
}
}
#endregion
#region 判断用户是否存在
/// <summary>
/// 通过账号判断用户是否存在
/// </summary>
/// <param name="account">账号</param>
/// <returns>存在返回user对象,不存在返回null</returns>
private User CheckUserByAccount(string account)
{
foreach (User user in _userGroup)
{
if (user.Account == account)
{
return user;
}
}
return null;
}
/// <summary>
/// 根据账号和密码判定账户是否存在
/// </summary>
/// <param name="account">账号</param>
/// <param name="pwd">密码</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;
}
#endregion
#region 修改密码
public void ModifyPwd()
{
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;
}
}
#endregion
#region 转账
/// <summary>
/// 转账输入输出信息
/// </summary>
public void InputTransferData()
{
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
{
Console.WriteLine("转账账号不存在");
}
}
/// <summary>
///
/// </summary>
/// <param name="fAccount">当前账号</param>
/// <param name="fPwd">账号密码</param>
/// <param name="tAccount">转账账号</param>
/// <param name="money">转账金额</param>
/// <param name="fBalance">当前账号余额</param>
/// <returns></returns>
///
/// <summary>
/// 执行转账操作
/// </summary>
/// <param name="fAccount">转出账号</param>
/// <param name="fPwd">转出账户密码</param>
/// <param name="tAccount">转入账号</param>
/// <param name="money">转账金额</param>
/// <param name="fBalance">转出账户存款额</param>
/// <param name="tBalance">转入账户存款额</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;
}
#endregion
}
}