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

 更新时间:2020年6月25日 11:37  点击:1970

账户类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;
}
}
}
}
}

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25