程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#模仿銀行atm機示例分享

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

編輯:C#入門知識

c#模仿銀行atm機示例分享。本站提示廣大學習愛好者:(c#模仿銀行atm機示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
}
}
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved