Skip to content

Commit 7914d05

Browse files
committed
2 parents 9d01c63 + 3138df6 commit 7914d05

File tree

16 files changed

+544
-80
lines changed

16 files changed

+544
-80
lines changed

DesignPattern.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FacadePattern", "FacadePatt
3535
EndProject
3636
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MementoPattern", "MementoPattern\MementoPattern.csproj", "{075919B3-9370-4DCE-9B16-ED0EDEC53735}"
3737
EndProject
38+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{04898517-CD60-419B-9C4D-2BE8A99C40FC}"
39+
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4042
Debug|Any CPU = Debug|Any CPU
@@ -105,6 +107,10 @@ Global
105107
{075919B3-9370-4DCE-9B16-ED0EDEC53735}.Debug|Any CPU.Build.0 = Debug|Any CPU
106108
{075919B3-9370-4DCE-9B16-ED0EDEC53735}.Release|Any CPU.ActiveCfg = Release|Any CPU
107109
{075919B3-9370-4DCE-9B16-ED0EDEC53735}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.Build.0 = Release|Any CPU
108114
EndGlobalSection
109115
GlobalSection(SolutionProperties) = preSolution
110116
HideSolutionNode = FALSE

FacadePattern/ATM.cs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,86 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace FacadePattern
84
{
9-
class ATM
5+
public class ATM
106
{
7+
public void DisplayUi()
8+
{
9+
var facade = new AtmFacade();
10+
11+
while (true)
12+
try
13+
{
14+
if (!facade.IsLogin())
15+
{
16+
Console.WriteLine("请输入银行卡号:");
17+
var bkNo = Console.ReadLine();
18+
Console.WriteLine("请输入密码:");
19+
var pwd = Console.ReadLine();
20+
facade.Login(bkNo, pwd);
21+
}
22+
else
23+
{
24+
ShowBusiness(facade);
25+
}
26+
}
27+
catch (Exception ex)
28+
{
29+
Console.ForegroundColor = ConsoleColor.Red;
30+
Console.WriteLine(ex.Message);
31+
Console.ResetColor();
32+
}
33+
}
34+
35+
36+
private static void ShowBusiness(AtmFacade facade)
37+
{
38+
Console.WriteLine("==========================================");
39+
Console.WriteLine("欢迎你!请选择服务项目:");
40+
Console.WriteLine("1、取款");
41+
Console.WriteLine("2、存款");
42+
Console.WriteLine("3、转账");
43+
Console.WriteLine("4、查询余额");
44+
Console.WriteLine("5、清屏");
45+
Console.WriteLine("==========================================");
46+
47+
var pressKey = Console.ReadKey();
48+
49+
switch (pressKey.Key)
50+
{
51+
case ConsoleKey.D1:
52+
Console.WriteLine();
53+
Console.WriteLine("请输入取款金额:");
54+
var money = Convert.ToInt32(Console.ReadLine());
55+
facade.WithdrewCash(money);
56+
break;
57+
case ConsoleKey.D2:
58+
Console.WriteLine();
59+
Console.WriteLine("请输入存款金额:");
60+
var depositNum = Convert.ToInt32(Console.ReadLine());
61+
facade.DepositCash(depositNum);
62+
break;
63+
case ConsoleKey.D3:
64+
Console.WriteLine();
65+
Console.WriteLine("请输入目标账号:");
66+
var targetNo = Console.ReadLine();
67+
Console.WriteLine("请输入转账金额:");
68+
var transferNum = Convert.ToInt32(Console.ReadLine());
69+
facade.TransferMoney(targetNo, transferNum);
70+
break;
71+
case ConsoleKey.D4:
72+
Console.WriteLine();
73+
facade.QueryBalance();
74+
break;
75+
case ConsoleKey.D5:
76+
Console.Clear();
77+
break;
78+
default:
79+
Console.ForegroundColor = ConsoleColor.Red;
80+
Console.WriteLine("输入有误,请重新输入");
81+
Console.ResetColor();
82+
break;
83+
}
84+
}
1185
}
12-
}
86+
}

FacadePattern/AccountSubsystem.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace FacadePattern
6+
{
7+
/// <summary>
8+
/// 账户管理子系统
9+
/// </summary>
10+
public static class AccountSubsystem
11+
{
12+
private static readonly List<BankAccount> Accounts = new List<BankAccount>
13+
{
14+
new BankAccount("123455", "555555", "圣杰", "138****9309", 1000000),
15+
new BankAccount("123454", "444444", "产品汪", "157****9309", 2000000),
16+
new BankAccount("123453", "333333", "运营喵", "154****9309", 3000000),
17+
new BankAccount("123452", "222222", "程序猿", "187****9309", 4000000),
18+
new BankAccount("123451", "111111", "设计狮", "189****9309", 5000000)
19+
};
20+
21+
public static BankAccount Login(string bankNo, string password)
22+
{
23+
var bankAccount = Accounts.FirstOrDefault(a => a.BankNo == bankNo);
24+
if (bankAccount == null)
25+
throw new Exception("无效卡号!!!");
26+
27+
if (bankAccount.Password != password)
28+
throw new Exception("密码错误!!!");
29+
30+
return bankAccount;
31+
}
32+
33+
public static BankAccount GetAccount(string bankNo)
34+
{
35+
var bankAccount = Accounts.FirstOrDefault(a => a.BankNo == bankNo);
36+
if (bankAccount == null)
37+
throw new Exception("无效卡号!!!");
38+
39+
40+
return bankAccount;
41+
}
42+
43+
public static void Display(BankAccount account)
44+
{
45+
Console.WriteLine("卡号:{0},持卡人姓名:{1},手机号:{2},余额:{3}", account.BankNo, account.Name, account.Phone,
46+
account.TotalMoney);
47+
}
48+
49+
50+
public static bool ChangePassword()
51+
{
52+
throw new NotImplementedException();
53+
}
54+
}
55+
}

FacadePattern/AccountVerificationCenter.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

FacadePattern/AtmFacade.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
3+
namespace FacadePattern
4+
{
5+
/// <summary>
6+
/// ATM机专属门面
7+
/// </summary>
8+
public class AtmFacade
9+
{
10+
private readonly IBankSubsystem _bankSubsystem = new BankSubsystem();
11+
private BankAccount _account;
12+
13+
public void Login(string no, string pwd)
14+
{
15+
_account = AccountSubsystem.Login(no, pwd);
16+
}
17+
18+
public bool IsLogin()
19+
{
20+
return _account != null;
21+
}
22+
23+
/// <summary>
24+
/// 取款
25+
/// </summary>
26+
/// <param name="money"></param>
27+
public void WithdrewCash(int money)
28+
{
29+
if (_bankSubsystem.WithdrewMoney(_account, money))
30+
{
31+
Console.WriteLine("取款成功!");
32+
AccountSubsystem.Display(_account);
33+
}
34+
}
35+
36+
/// <summary>
37+
/// 存款
38+
/// </summary>
39+
/// <param name="money"></param>
40+
public void DepositCash(int money)
41+
{
42+
if (_bankSubsystem.DepositMoney(_account, money))
43+
{
44+
Console.WriteLine("存款成功!");
45+
AccountSubsystem.Display(_account);
46+
}
47+
}
48+
49+
/// <summary>
50+
/// 查余额
51+
/// </summary>
52+
public void QueryBalance()
53+
{
54+
if (_bankSubsystem.CheckBalance(_account) > 0)
55+
AccountSubsystem.Display(_account);
56+
}
57+
58+
/// <summary>
59+
/// 转账
60+
/// </summary>
61+
/// <param name="targetNo"></param>
62+
/// <param name="money"></param>
63+
public void TransferMoney(string targetNo, int money)
64+
{
65+
if (_bankSubsystem.TransferMoney(_account, targetNo, money))
66+
{
67+
Console.WriteLine("转账成功!");
68+
AccountSubsystem.Display(_account);
69+
}
70+
}
71+
}
72+
}

FacadePattern/Bank.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

FacadePattern/BankSubsystem.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
3+
namespace FacadePattern
4+
{
5+
public class BankSubsystem : IBankSubsystem
6+
{
7+
/// <summary>
8+
/// 查询余额
9+
/// </summary>
10+
/// <param name="account">银行账户</param>
11+
/// <returns></returns>
12+
public int CheckBalance(BankAccount account)
13+
{
14+
return account.TotalMoney;
15+
}
16+
17+
/// <summary>
18+
/// 取款
19+
/// </summary>
20+
/// <param name="account">银行账户</param>
21+
/// <param name="money">取多少钱</param>
22+
/// <returns>余额</returns>
23+
public bool WithdrewMoney(BankAccount account, int money)
24+
{
25+
if (account.TotalMoney >= money)
26+
account.TotalMoney -= money;
27+
else
28+
throw new Exception("余额不足!");
29+
30+
return true;
31+
}
32+
33+
/// <summary>
34+
/// 转账
35+
/// </summary>
36+
/// <param name="account">转出账户</param>
37+
/// <param name="targetNo">目标账户</param>
38+
/// <param name="money">转多少钱</param>
39+
/// <returns></returns>
40+
public bool TransferMoney(BankAccount account, string targetNo, int money)
41+
{
42+
var targetAccount = AccountSubsystem.GetAccount(targetNo);
43+
44+
if (targetAccount == null)
45+
throw new Exception("目标账户不存在!");
46+
47+
if (account.TotalMoney < money)
48+
throw new Exception("余额不足!");
49+
50+
account.TotalMoney -= money;
51+
targetAccount.TotalMoney += money;
52+
53+
return true;
54+
}
55+
56+
/// <summary>
57+
/// 存款
58+
/// </summary>
59+
/// <param name="account">银行账户</param>
60+
/// <param name="money">存多少钱</param>
61+
/// <returns></returns>
62+
public bool DepositMoney(BankAccount account, int money)
63+
{
64+
account.TotalMoney += money;
65+
return true;
66+
}
67+
68+
/// <summary>
69+
/// 充值话费
70+
/// </summary>
71+
/// <param name="phoneNumber">手机号</param>
72+
/// <param name="account">银行账户</param>
73+
/// <param name="money">充值多少</param>
74+
/// <returns></returns>
75+
public bool RechargeMobilePhone(BankAccount account, string phoneNumber, int money)
76+
{
77+
throw new NotImplementedException();
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)