欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#模擬銀行atm機示例分享

 更新時間:2014年03月10日 14:49:38   作者:  
這篇文章主要介紹了c#模擬銀行atm機示例,實現(xiàn)了用戶登錄、用戶存款、用戶取款等功能,需要的朋友可以參考下

賬戶類Account:
Id:賬戶號碼
PassWord:賬戶密碼
Name:真實姓名
PersonId:身份證號碼
Email:客戶的電子郵箱
Balance:賬戶余額

Deposit:存款方法,參數(shù)是double型的金額
Withdraw:取款方法,參數(shù)是double型的金額

銀行的客戶分為兩種類型:
儲蓄賬戶(SavingAccount)和信用賬戶(CreditAccount)
兩者的區(qū)別是儲蓄賬戶不許透支,而信用賬戶可以透支,并允許用戶設(shè)置自己的透支額度(使用ceiling表示)

Bank類,
屬性如下
(1)當(dāng)前所有的賬戶對象的集合
(2)當(dāng)前賬戶數(shù)量
構(gòu)造方法
(1)用戶開戶:需要的參數(shù)包括id,密碼,姓名,身份證號碼,油箱和賬戶類型
(2)用戶登錄:參數(shù)包括id,密碼,返回Account對象
(3)用戶存款:參數(shù)包括id和存款數(shù)額
(4)用戶取款:參數(shù)包括id和取款數(shù)額
(5)設(shè)置透支額度:參數(shù)包括id和新的額度,這個方法需要哦驗證賬戶是否是信用賬戶參數(shù)
統(tǒng)計方法
(6)統(tǒng)計銀行所有賬戶的余額總數(shù)
(7)統(tǒng)計所有信用賬戶透支額額度總數(shù)

源代碼:

復(fù)制代碼 代碼如下:

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; }
}

//靜態(tài)號碼生成器
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("輸入的金額為負數(shù)");
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;
}
}
//創(chuàng)建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("輸入的金額為負數(shù)!");
}
if (sum > balance + ceiling)
{
throw new InvalidOperationException("金額已經(jīng)超出余額和透支度的總數(shù)了");
}
balance -= sum;
}
}
//創(chuàng)建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("輸入的金額為負數(shù)!");
}
if(sum>balance)
{
throw new InvalidOperationException("金額已經(jīng)超出金額!");
}
balance -= sum;
}
}

//bank類,對銀行中的所有賬戶進行管理
class Bank
{
//存放賬戶的集合
private List<Account> accounts;
public List<Account> Accounts
{
get { return accounts; }
set { accounts = value; }
}

//當(dāng)前銀行的賬戶數(shù)量
private int currentAccountNumber;
public int CurrentAccountNumber
{
get { return currentAccountNumber; }
set { currentAccountNumber = value; }
}

//構(gòu)造函數(shù)
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之間的整數(shù)");
}
//把新開的賬號加到集合中
accounts.Add(newAccount);
return newAccount;
}
//根據(jù)賬戶id得到賬戶對象
private Account GetAccountByID(long id)
{
foreach (Account account in accounts)
{
if (account.ID == id)
{
return account;
}
}
return null;
}

//根據(jù)賬號和密碼登陸賬戶
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("非法賬戶!");
}

//設(shè)置透支額度
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("非法賬戶");
}

//統(tǒng)計銀行所有賬戶余額
public double GetTotalBalance()
{
double totalBalance = 0;
foreach (Account account in accounts)
{
totalBalance += account.Balance;
}
return totalBalance;
}
//統(tǒng)計所有信用賬戶透支額度總數(shù)
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;
}
}
}
}
}

相關(guān)文章

  • C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn)

    C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn)

    RabbitMQ是一個消息的代理器,用于接收和發(fā)送消息,本文主要介紹了C#使用RabbitMQ發(fā)送和接收消息工具類的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • C#中Dapper的使用教程

    C#中Dapper的使用教程

    Dapper是一款輕量級ORM工具(Github),Dapper語法十分簡單。并且無須遷就數(shù)據(jù)庫的設(shè)計,今天通過本文給大家介紹C# Dapper的使用,感興趣的朋友一起看看吧
    2021-07-07
  • C#基于COM方式讀取Excel表格的方法

    C#基于COM方式讀取Excel表格的方法

    這篇文章主要介紹了C#基于COM方式讀取Excel表格的方法,涉及C# COM組件的調(diào)用與Excel表格的使用技巧,需要的朋友可以參考下
    2016-07-07
  • C#工程建立后修改工程文件名與命名空間操作

    C#工程建立后修改工程文件名與命名空間操作

    這篇文章主要介紹了C#工程建立后修改工程文件名與命名空間操作,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • unity實現(xiàn)繪畫功能

    unity實現(xiàn)繪畫功能

    這篇文章主要為大家詳細介紹了unity實現(xiàn)繪畫功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C#實現(xiàn)應(yīng)用程序的監(jiān)控與調(diào)試的示例代碼

    C#實現(xiàn)應(yīng)用程序的監(jiān)控與調(diào)試的示例代碼

    日志記錄是軟件開發(fā)中不可或缺的功能,它能幫助開發(fā)者在應(yīng)用程序運行時記錄重要信息,本文就來介紹一下常用日志記錄功能以及常用的日志庫,感興趣的可以了解一下
    2024-03-03
  • C#實現(xiàn)運行狀態(tài)堆疊柱狀圖

    C#實現(xiàn)運行狀態(tài)堆疊柱狀圖

    這篇文章主要為大家詳細介紹了C#實現(xiàn)運行狀態(tài)堆疊柱狀圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 事務(wù)在c#中的使用

    事務(wù)在c#中的使用

    這篇文章介紹了事務(wù)在c#中的使用,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • unity實現(xiàn)翻頁效果

    unity實現(xiàn)翻頁效果

    這篇文章主要為大家詳細介紹了unity實現(xiàn)翻頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • c#轉(zhuǎn)換全角半角方法示例

    c#轉(zhuǎn)換全角半角方法示例

    這篇文章主要介紹了c#如何轉(zhuǎn)換全角半角,大家可以看一下下面的代碼方法,可以參考一下
    2013-12-12

最新評論