欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#模拟银行atm机示例分享

程序员文章站 2024-02-12 16:37:46
账户类account:id:账户号码password:账户密码name:真实姓名personid:身份证号码email:客户的电子邮箱balance:账户余额 depos...

账户类account:
id:账户号码
password:账户密码
name:真实姓名
personid:身份证号码
email:客户的电子邮箱
balance:账户余额

deposit:存款方法,参数是double型的金额
withdraw:取款方法,参数是double型的金额

银行的客户分为两种类型:
储蓄账户(savingaccount)和信用账户(creditaccount)
两者的区别是储蓄账户不许透支,而信用账户可以透支,并允许用户设置自己的透支额度(使用ceiling表示)

bank类,
属性如下
(1)当前所有的账户对象的集合
(2)当前账户数量
构造方法
(1)用户开户:需要的参数包括id,密码,姓名,身份证号码,油箱和账户类型
(2)用户登录:参数包括id,密码,返回account对象
(3)用户存款:参数包括id和存款数额
(4)用户取款:参数包括id和取款数额
(5)设置透支额度:参数包括id和新的额度,这个方法需要哦验证账户是否是信用账户参数
统计方法
(6)统计银行所有账户的余额总数
(7)统计所有信用账户透支额额度总数

源代码:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace atm
{
abstract class account
{
//账户号码
protected long id;
public long id
{
get { return id; }
set { id = value; }
}
//账户密码
protected string password;
public string password
{
get { return password; }
set { password = value; }
}
//户主的姓名
protected string name;
public string name
{
get { return name; }
set { name = value; }
}
//身份证号码
protected string personid;
public string personid
{
get { return personid; }
set { personid = value; }
}
//email
protected string email;
public string email
{
get { return email; }
set { email = value; }
}
//余额
protected double balance;
public double balance
{
get { return balance; }
set { balance = value; }
}

//静态号码生成器
private static long idbuilder = 100000;
public static long idbuilder
{
get { return idbuilder; }
set { idbuilder = value; }
}

public void deposit(double sum)//存款
{
if (sum < 0)
throw new invalidoperationexception("输入的金额为负数");
balance += sum;
}

public abstract void withdraw(double sum);//取款
public account()
{ }
public account(string password, string name, string personid, string email)
{
this.id = ++idbuilder;
this.password = password;
this.name = name;
this.personid = personid;
this.email = email;
}
}
//创建creditaccount类,该类继承抽象类account
class creditaccount : account
{
protected double ceiling;//透支额度
public double ceiling
{
get { return ceiling; }
set { ceiling = value; }
}
public creditaccount(string password, string name, string personid, string email)
: base(password, name, personid, email)
{ }  
//信用账户的取款操作
public override void withdraw(double sum)
{
if (sum < 0)
{
throw new invalidoperationexception("输入的金额为负数!");
}
if (sum > balance + ceiling)
{
throw new invalidoperationexception("金额已经超出余额和透支度的总数了");
}
balance -= sum;
}
}
//创建savingaccount类,该类继承抽象类account
class savingaccount : account
{
public savingaccount(string password, string name, string personid, string email)
: base(password, name, personid, email)
{ }

public override void withdraw(double sum)
{
if (sum < 0)
{
throw new invalidoperationexception("输入的金额为负数!");
}
if(sum>balance)
{
throw new invalidoperationexception("金额已经超出金额!");
}
balance -= sum;
}
}

//bank类,对银行中的所有账户进行管理
class bank
{
//存放账户的集合
private list<account> accounts;
public list<account> accounts
{
get { return accounts; }
set { accounts = value; }
}

//当前银行的账户数量
private int currentaccountnumber;
public int currentaccountnumber
{
get { return currentaccountnumber; }
set { currentaccountnumber = value; }
}

//构造函数
public bank()
{
accounts=new list<account>();
}

//开户
public account openaccount(string password, string confirmationpassword, string name, string personid, string email, int typeofaccount)
{
account newaccount;
if (!password.equals(confirmationpassword))
{
throw new invalidoperationexception("两次密码输入的不一致");
}
switch (typeofaccount)
{
case 1: newaccount = new savingaccount(password, name, personid, email);
break;
case 2: newaccount = new creditaccount(password,name,personid,email);
break;
default: throw new argumentoutofrangeexception("账户类型是1和2之间的整数");
}
//把新开的账号加到集合中
accounts.add(newaccount);
return newaccount;
}
//根据账户id得到账户对象
private account getaccountbyid(long id)
{
foreach (account account in accounts)
{
if (account.id == id)
{
return account;
}
}
return null;
}

//根据账号和密码登陆账户
public account signin(long id, string password)
{
foreach (account account in accounts)
{
if (account.id == id && account.password.equals(password))
{
return account;
}
}
throw new invalidoperationexception("用户名或者密码不正确,请重试");
}

//存款
public account deposit(long id, double sum)
{
account account = getaccountbyid(id);
if (account != null)
{
account.deposit(sum);
return account;
}
throw new invalidoperationexception("非法账户!");
}

//取款
public account withdraw(long id, double sum)
{
account account = getaccountbyid(id);
if (account != null)
{
account.withdraw(sum);
return account;
}
throw new invalidoperationexception("非法账户!");
}

//设置透支额度
public account setceiling(long id, double newceiling)
{
account account = getaccountbyid(id);
try
{
(account as creditaccount).ceiling = newceiling;
return account;
}
catch (exception)
{
throw new invalidoperationexception("次账户不是信用账户!");
}
throw new invalidoperationexception("非法账户");
}

//统计银行所有账户余额
public double gettotalbalance()
{
double totalbalance = 0;
foreach (account account in accounts)
{
totalbalance += account.balance;
}
return totalbalance;
}
//统计所有信用账户透支额度总数
public double gettotalceiling()
{
double totalceiling = 0;
foreach (account account in accounts)
{
if (account is creditaccount)
{
totalceiling += (account as creditaccount).ceiling;
}
}
return totalceiling;
}
}

//进行客户测试
class program
{
static account signin(bank icbc)
{
console.writeline("\nplease input your account id");
long id;
try
{
id = long.parse(console.readline());
}
catch (formatexception)
{
console.writeline("invalid account id!");
return null;
}

console.writeline("please input your password");
string password = console.readline();
account account;
try
{
   account = icbc.signin(id, password);
}
catch (invalidoperationexception ex)
{
console.writeline(ex.message);
return null;
}
return account;
}
static void main(string[] args)
{
bank icbc = new bank();
while (true)
{
console.writeline("please choose the service your need");
console.writeline("(1) open a new account");
console.writeline("(2) desposit");
console.writeline("(3) withdraw");
console.writeline("(4) set ceiling");
console.writeline("(5) get total balance");
console.writeline("(6) get total ceiling");
console.writeline("(0) exit");
string choice;
choice = console.readkey().keychar.tostring();
//consolekey i=console.readkey().key;
switch (choice)
{
case "1":
{
string personid;
int typeofaccount;
string password;
console.writeline("\nwhich kind of account do you want to open?");
while (true)
{
console.writeline("(1)saving account\n(2)credit account\n(3)return to last menu");
try
{
typeofaccount = int.parse(console.readline());
}
catch (formatexception)
{
console.writeline("\ninvalid option,please choose again");
continue;
}
if (typeofaccount < 1 || typeofaccount > 3)
{
console.writeline("\ninvalid option,please choooose again!");
continue;
}
break;
}
if (typeofaccount == 3)
{
break;
}
console.writeline("\nplease input your name:");
string name = console.readline();
while (true)
{
console.writeline("please input your personal id");
personid = console.readline();
if (personid.length != 18)
{
console.writeline("invalid personal id,please input again!");
continue;
}
break;
}
console.writeline("please input your e-mail");
string email = console.readline();
while (true)
{
console.writeline("please input your password");
password = console.readline();
console.writeline("please confirm your password");
if (password != console.readline())
{
console.writeline("the password doesn't math!");
continue;
}
break;
}
account account = icbc.openaccount(password, password, name, personid, email, typeofaccount);
console.writeline("the account opened successfully");
console.writeline("account id:{0}\naccount name;{1}\nperson id:{2}\nemail;{3}\nbalance:{4}",account.id,account.name,account.personid,account.email,account.balance);
}
break;
case "2":
{
account account = signin(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
console.writeline("please input the amount;");
try
{
amount = int.parse(console.readline());
}
catch (formatexception)
{
console.writeline("invalid input!");
continue;
}
break;
}
try
{
icbc.deposit(account.id, amount);
}
catch (invalidoperationexception ex)
{
console.writeline(ex.message);
break;
}
console.writeline("deposit successfully,our balance is{0}元",account.balance);
}
break;
case "3":
{
account account = signin(icbc);
if (account == null)
{
break;
}
int amount;
while (true)
{
console.writeline("please input the amount");
try
{
amount = int.parse(console.readline());
}
catch (formatexception)
{
console.writeline("invalid input!");
continue;
}
break;
}
try
{
icbc.withdraw(account.id, amount);
}
catch (invalidoperationexception ex)
{
console.writeline(ex.message);
break;
}
console.writeline("deposit successfully,your balance is{0}yuan",account.balance);
}
break;
case "4":
{
account account = signin(icbc);
if (account == null)
{
break;
}
double newceiling;
while (true)
{
console.writeline("please input the new ceiling");
try
{
newceiling = double.parse(console.readline());
}
catch (formatexception)
{
console.writeline("invalid input!");
continue;
}
break;
}
try
{
icbc.setceiling(account.id, newceiling);
}
catch (invalidoperationexception ex)
{
console.writeline(ex.message);
break;
}
console.writeline("set ceiling successfully,your new ceiling is{0} yuan",(account as creditaccount).ceiling);

}
break;
case "5":
console.writeline("\nthe total balance is:"+icbc.gettotalbalance());
break;
case "6":
console.writeline("\nthe total ceiling is:" + icbc.gettotalceiling());
break;
case "0":
return;
default:
console.writeline("\ninvalid option,plwase choose again!");
break;
}
}
}
}
}