任务的要求如下:
下面是设计的UML类图:
下面是部分代码片段:
1 /// <summary> 2 /// 环境类 3 /// </summary> 4 public class Account 5 { 6 public AccountState State { set; get; } 7 public string Owner { set; get; } 8 public double Balance { set; get; } 9 10 public Account(string owner, double initialAmount) 11 { 12 Owner = owner; 13 Balance = initialAmount; 14 State = new VistorState(this); //设置初始状态 15 } 16 17 public void SetBalance(double amount) 18 { 19 Balance = amount; 20 21 } 22 23 //存款 24 public void Deposit(double amount) 25 { 26 Console.WriteLine("存入" + amount); 27 State.Deposit(amount); 28 Console.WriteLine("用户状态变为" + State._Statename); 29 } 30 31 //消费 32 public void Shopping(double amount) 33 { 34 Console.WriteLine("消费" + amount); 35 State.Shopping(amount); 36 Console.WriteLine("用户状态变为" + State._Statename); 37 } 38 39 40 }
1 /// <summary> 2 /// 抽象状态类 3 /// </summary> 4 public abstract class AccountState 5 { 6 protected Account _account; 7 public string _Statename; 8 9 public abstract void Deposit(double amount); 10 public abstract void Shopping(double amount); 11 public abstract void StateCheck(); 12 }
1 /// <summary> 2 /// 访客状态类 3 /// </summary> 4 public class VistorState:AccountState 5 { 6 public VistorState(Account account) 7 { 8 _account = account; 9 _Statename = "访客账户"; 10 StateCheck(); //默认的检查 11 } 12 13 //取款 14 public override void Deposit(double amount) 15 { 16 _account.Balance += amount; 17 Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); 18 StateCheck(); 19 } 20 21 //消费 22 public override void Shopping(double amount) 23 { 24 double newBalance = _account.Balance - amount; 25 if (newBalance > 0) 26 { 27 _account.Balance -= amount; 28 Console.WriteLine(_account.Owner + "在《访客服务》中消费" + amount + "元,账户余额:" + _account.Balance); 29 } 30 else 31 { 32 Console.WriteLine("您的账户余额不足,请充值。"); 33 } 34 StateCheck(); 35 } 36 37 //检查用户状态 38 public override void StateCheck() 39 { 40 if (_account.Balance > 100 && _account.Balance < 1000) 41 { 42 //会员用户状态 43 _account.State = new MemberState(_account); 44 } 45 else if (_account.Balance > 1000) 46 { 47 //贵宾用户状态 48 _account.State = new VIPState(_account); 49 } 50 } 51 }
1 /// <summary> 2 /// 会员状态类 3 /// </summary> 4 public class MemberState:AccountState 5 { 6 public MemberState(Account account) 7 { 8 _account = account; 9 _Statename = "会员账户"; 10 } 11 12 //取款 13 public override void Deposit(double amount) 14 { 15 _account.Balance += amount; 16 Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); 17 StateCheck(); 18 } 19 20 //消费 21 public override void Shopping(double amount) 22 { 23 double newBalance = _account.Balance - amount; 24 if (newBalance > 0) 25 { 26 _account.Balance -= amount; 27 Console.WriteLine(_account.Owner + "在《会员服务》中消费" + amount + "元,账户余额:" + _account.Balance); 28 } 29 else 30 { 31 Console.WriteLine("您的账户余额不足,请充值。"); 32 } 33 StateCheck(); 34 } 35 36 //检查用户状态 37 public override void StateCheck() 38 { 39 if (_account.Balance > 0 && _account.Balance < 100) 40 { 41 //访客用户状态 42 _account.State = new VistorState(_account); 43 } 44 else if (_account.Balance > 1000) 45 { 46 //贵宾用户状态 47 _account.State = new VIPState(_account); 48 49 } 50 } 51 }
1 /// <summary> 2 /// 贵宾状态类 3 /// </summary> 4 public class VIPState:AccountState 5 { 6 public VIPState(Account account) 7 { 8 _account = account; 9 _Statename = "贵宾账户"; 10 } 11 12 //取款 13 public override void Deposit(double amount) 14 { 15 _account.Balance += amount; 16 Console.WriteLine(_account.Owner + " 向账户中存款" + amount + "元,存款后的账户余额为:" + _account.Balance); 17 StateCheck(); 18 } 19 20 //消费 21 public override void Shopping(double amount) 22 { 23 double newBalance = _account.Balance - amount; 24 if (newBalance > 0) 25 { 26 _account.Balance -= amount; 27 Console.WriteLine(_account.Owner + "在《贵宾服务》中消费" + amount + "元,账户余额:" + _account.Balance); 28 } 29 else 30 { 31 Console.WriteLine("您的账户余额不足,请充值。"); 32 } 33 StateCheck(); 34 } 35 36 //检查用户状态 37 public override void StateCheck() 38 { 39 if (_account.Balance > 0 && _account.Balance < 100) 40 { 41 //访客用户状态 42 _account.State = new VistorState(_account); 43 } 44 else if (_account.Balance > 100&&_account.Balance<1000) 45 { 46 //会员用户状态 47 _account.State = new MemberState(_account); 48 } 49 } 50 }
/// <summary> /// 客户端代码 /// </summary> class Program { static void Main(string[] args) { Account account = new Account("王大锤", 99); Console.WriteLine("开户成功。姓名:" + account.Owner + " 初始金额:" + account.Balance + " 账户类型:" + account.State._Statename); Console.WriteLine("----------------------------"); account.Deposit(20); Console.WriteLine("-----------------------------"); account.Shopping(30); Console.WriteLine("-----------------------------"); account.Deposit(9999); Console.WriteLine("------------------------------"); Console.ReadLine(); } }
下面是程序的结果: