当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-29

    本文导语:  账户类Account:Id:账户号码PassWord:账户密码Name:真实姓名PersonId:身份证号码Email:客户的电子邮箱Balance:账户余额 Deposit:存款方法,参数是double型的金额Withdraw:取款方法,参数是double型的金额 银行的客户分为两种类型:储蓄账户...

账户类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 accounts;
public List Accounts
{
get { return accounts; }
set { accounts = value; }
}

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

//构造函数
public Bank()
{
accounts=new List();
}

//开户
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 Accountn(2)Credit Accountn(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;
}
}
}
}
}


    
 
 

您可能感兴趣的文章:

  • c#模拟js escape方法的简单实例
  • c#模拟平抛运动动画的方法详解
  • C#模拟MSN窗体抖动的实现代码
  • C# SendInput 模拟鼠标操作的实现方法
  • C# winfrom 模拟ftp文件管理实现代码
  • python采用requests库模拟登录和抓取数据的简单示例
  • jQuery模拟点击A标记示例参考
  • LinkedList学习示例模拟堆栈与队列数据结构
  • Jquery模拟超链接点击效果的代码示例
  • php模拟远程登录示例代码
  • jquery查找tr td 示例模拟
  • java模拟hibernate一级缓存示例分享
  • PHP模拟POST提交的示例代码
  • php curl模拟post提交数据示例
  • python模拟登陆Tom邮箱示例分享
  • java使用httpclient模拟post请求和get请求示例
  • C实现分子沉积模拟的示例代码
  • java模拟post请求登录猫扑示例分享
  • 用jQuery模拟select下拉框的简单示例代码
  • python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
  • windows下python模拟鼠标点击和键盘输示例
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • linux socket编程问题,下面是我做的一个简单的模拟银行排队叫号系统。
  • 电路教学模拟器 稳恒电路模拟器
  • php模拟登录 php curl模拟登录教程大全
  • 在Android模拟器上模拟GPS功能总是null的解决方法
  • Intel HAXM为Android 模拟器加速解决模拟器运行慢的问题
  • php模拟qq登录 php模拟登录实例
  • ◆◆◆◆◆◆◆j2me中,用repaint()在同一地方重画图象,当我画到二十几遍后,手机模拟器就出错或者程序自动退出。我想是不是由于重画时,以前的图片好保存在模拟器的内存中,当重画到一定数时,内存不够,便出错了。还是其他的原因?请问如何解决?
  • 指令级机器模拟器 GXemul
  • FC模拟器 Nintendulator
  • 任天堂游戏模拟器 VisualBoyAdvance
  • 在线模拟考试系统 PHPems
  • EFL设计示波器,模拟量输入的问题
  • PSP模拟器 JPcsp
  • 电路模拟器 Qucs
  • JavaScript 模拟器库 Virtjs
  • Web服务器/前端 iis7站长之家
  • Linux游戏模拟器 Xmame
  • 微控制器模拟器 PICsim
  • NDS模拟器 DeSmuME
  • FC 模拟器 Nestopia
  • PS2模拟器 PCSX2


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3