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

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

(0)

相关推荐

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

    账户类Account:Id:账户号码PassWord:账户密码Name:真实姓名PersonId:身份证号码Email:客户的电子邮箱Balance:账户余额 Deposit:存款方法,参数是double型的金额Withdraw:取款方法,参数是double型的金额 银行的客户分为两种类型:储蓄账户(SavingAccount)和信用账户(CreditAccount)两者的区别是储蓄账户不许透支,而信用账户可以透支,并允许用户设置自己的透支额度(使用ceiling表示) Bank类,属性如下(1

  • java模拟实现银行ATM机操作

    java模拟银行ATM机操作(基础版),供大家参考,具体内容如下 实现的功能需求: 修改密码之后,就会自动退出登录,再重新登录,若登录成功才能验证修改密码成功,这里用到 [跳出指定循环], 其他功能都是基本操作,作为入门入手程序. 准备两个实体类(一个银行类,一个用户类),一个测试类,注意,这里暂且存储了两个用户,这里可以优化,暂且不优化了 Blank.java package com.demo2; public class Blank {     /*数组模拟数据库后台,并初始化*/    

  • Java模拟实现ATM机

    Java模拟ATM机,供大家参考,具体内容如下 实现登录,查询,转账,取款,修改密码,退出功能. 源码 package bank; import java.io.*; import java.util.Scanner; //ATM类 public class Atm { private String[] user;//用户全部信息 private double money;//修改钱数 private double userMoney;//用户的钱 private String newPassw

  • java模拟hibernate一级缓存示例分享

    纯Java代码模拟Hibernate一级缓存原理,简单易懂. 复制代码 代码如下: import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; public class LevelOneCache { //这个对象就是用来模拟hibernate一级缓存的 private static Map<Integer, Student> stus=new HashMap&l

  • python模拟登陆Tom邮箱示例分享

    复制代码 代码如下: def loginTom(username, password): url1 = ''' http://login.mail.tom.com/cgi/login ''' values = {  'type' : '0',  'user' : '%s' % username,  'in_username' : '%s@tom.com' % username,  'pass' : '%s' % password,  'style' : '21',  'verifycookie'

  • java实现简单银行ATM系统

    本文实例为大家分享了java实现简单银行ATM系统的具体代码,供大家参考,具体内容如下 #ATM系统 ##功能 模拟银行ATM机系统,具有注册.登录功能用户登录后可实现以下功能:1)存款 2)取款 3)转账 4)查询 5)退出 ##设计思路 首先,要进行ATM机操作应该具有银行卡和ATM,所以我们要设计一个ATM类和Bankcard类,ATM具有存取款等操作,Bankcard用来记录用户存取款等操作后的越,其次因为ATM同时为多个用户服务,所以我们应该识别每张卡,这里增加一个Bank类来记录银行

  • Java详解实现ATM机模拟系统

    目录 一.概述 二.程序概要设计 三.程序详细设计 四.程序演示 一.概述 (1)选题分析 (2) 开发环境 开发环境,选择IDEA这一Java开发软件,基于JDK1.8版本,在本机window上开发本ATM模拟程序. 二.程序概要设计 (1) 功能模块设计 经过对题目的分析,把本ATM模拟程序分为管理员端和用户模式两大模块.其中,管理员具有查询所有账户.导出所有账户信息到文件.注销功能.用户模块具有查询余额.ATM转账.ATM存款.ATM取款.修改密码.查询交易记录.导出记录.退卡等功能. 系

  • Java使用嵌套循环模拟ATM机取款业务操作示例

    本文实例讲述了Java使用嵌套循环模拟ATM机取款业务操作.分享给大家供大家参考,具体如下: 代码: package com.jredu.ch03; import java.util.Scanner; public class Work4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); for (int i =

  • Java项目实现模拟ATM机

    本文实例为大家分享了Java实现模拟ATM机的具体代码,供大家参考,具体内容如下 项目名称 模拟ATM机 项目描述 简单实现ATM机功能 代码实现 测试类 public class Test { //模拟多功能ATM机 public static void main(String[] args) { ATM atm = new ATM(); atm.opearte(); } } 主类:实现主方法 public class ATM { private Bank bank; public ATM()

  • Java实现ATM机操作系统

    本文实例为大家分享了Java实现ATM机操作系统的具体代码,供大家参考,具体内容如下 用IO流操作txt文件作为数据库模拟实现一个ATM业务操作系统---->网上银行,实现登录,查询余额,存款,取款,转账,开户,销户等业务功能看代码 1.用户类----->User: package atm; import java.io.Serializable; public class User implements Serializable{          //建议除了私有属性  无参数有参数构造方

随机推荐