User 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
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 double yue;
public double yu//余额
{
get { return yue; }
set { yue = value; }
}
private string hao1;
public String hao//账号
{
get { return hao1; }
set { hao1 = value; }
}
private string sh1;
public string sh//身份证号
{
get { return sh1; }
set { sh1 = value; }
}
/// <summary>
/// 取款操作
/// </summary>
public double MinusMoney(double money)
{
if (money > 0)
{
if (money <= yu)
{
yu -= money;
return yu;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
/// <summary>
/// 存款
/// </summary>
/// <param name="money">存款金额</param>
public double SaveMoney(double money)
{
if (money > 0)
{
yu += money;
return yu;
}
else
{
return -1;
}
}
}
}
Bank类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Bank
{
User user = new User();
User[] _userGroup = new User[3];
const string MESSAGE = "操作成功!";
public void zong()
{
_userGroup[0] = new User();
_userGroup[0].hao = "12";
_userGroup[0]._name = "王丽丽";
_userGroup[0]._password = "1234";
_userGroup[0].sh = "210050619890808185";
_userGroup[0].yu = 1000;
_userGroup[1] = new User();
_userGroup[1].hao = "13";
_userGroup[1]._name = "张颖颖";
_userGroup[1]._password = "4321";
_userGroup[1].sh = "510010619891231127";
_userGroup[1].yu = 2000;
_userGroup[2] = new User();
_userGroup[2].hao = "179708064368";
_userGroup[2]._name = "刘华";
_userGroup[2]._password = "4567";
_userGroup[2].sh = "410207198904051271";
_userGroup[2].yu = 8000;
}
public void show1()
{
foreach (User userItem in _userGroup)
{
Console.WriteLine("帐户姓名:" + userItem._name + " 帐号:" + userItem.hao + " 存款余额:" + userItem.yu);
//Console.WriteLine("帐户姓名:" + userItem._name);
}
}
public void show2()
{
string option = "";
do
{
Console.WriteLine("==================欢迎使用自动银行服务==================");
Console.WriteLine("1:开户 2:存款 3:取款 4:转账 5:查询余额 6:修改密码 0:退出");
Console.WriteLine("========================================================");
option = Console.ReadLine();
switch (option)
{
case "1":
CreateAccount();
continue;
case "2":
cunkuan();
continue;
case "3":
qukuan();
continue;
case "4":
Indklhyeibn();
continue;
case "5":
continue;
case "6":
continue;
case "0":
break;
default:
Console.WriteLine("输入无效!");
continue;
}
break;
} while (true);
}
//开户
public void CreateAccount()
{
Console.WriteLine("请输入帐户姓名");
user._name = Console.ReadLine();
user.hao = "179708064356";
Console.WriteLine("请输入帐户密码");
user._password = Console.ReadLine();
Console.WriteLine("请输入帐户身份证号");
user.sh = Console.ReadLine();
Console.WriteLine("请输入帐户存款金额");
user.yu = double.Parse(Console.ReadLine());
Console.WriteLine("帐号:{0},帐户姓名:{1},存款金额:{2} {3}", user.hao, user._name, user.yu, MESSAGE);
}
//取款
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 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.yu);
}
else
{
Console.WriteLine("存款失败!");
}
}
//
public void Indklhyeibn()
{
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("转账失败!");
}
}
public User checkUser(string account, string pwd)
{
foreach (User s in _userGroup)
{
if (account.Equals(s.hao) && pwd.Equals(s._password))
{
return s;
}
}
return null;
}
//用户是否存在
private User CheckUserByAccount(string account)
{
foreach (User user in _userGroup)
{
if (user.hao == account)
{
return user;
}
}
return null;
}
public User checkUserbyaccount(string toaccount)
{
foreach (User s in _userGroup)
{
if (toaccount.Equals(s.hao))
{
return s;
}
}
return null;
}
//访问修饰符 返回值 方法名 (参数类型 参数列表)
public int Transfer(string fromaccount, string frompwd, string toaccount, double money, ref double fbalance, ref double tbalance)
{
//检查转出账号和密码]
User userfrom = checkUser(fromaccount, frompwd);
if (userfrom == null)
{//转出账号或密码不正确
return -1;
}
//检查转入账号
User userto = checkUserbyaccount(toaccount);
if (userto == null)
{//转入账号不正确
return -2;
}
//取款
if (userfrom.MinusMoney(money) == 1)
{
return -3;
}
fbalance = userfrom.yu;
if (userto.SaveMoney( money) == 1)
{
return -3;
}
tbalance = userto.yu;
return 1;
}
}
}
调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Bank u = new Bank();
u.zong();
u.show1();
u.show2();
Console.ReadLine();
}
}
}