标签:
作业题目
1、画UMl图
2、写代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 状态
{
class Program
{
static void Main(string[] args)
{
Club account = new Club("张三", 0);
Console.WriteLine("开户成功,姓名:" + account.Owner + ",初始金额:" + account.Balance);
Console.WriteLine("------------------------");
account.Deposit(50);
Console.WriteLine("------------------------");
account.Deposit(100);
Console.WriteLine("------------------------");
Console.ReadLine();
}
// AccountState类
public abstract class AccountState
{
protected Club Account;
//存款
public abstract void Deposit(double amount);
//消费
public abstract void Withdraw(double amount);
//检查账户状态
public abstract void StateChange();
}
//VistorState类
public class VistorState : AccountState
{
public VistorState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为"+Account.Owner+"的账户中存款"+amount+"元,存款后账户余额为"+Account.Balance+"元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 100 && Account.Balance <= 1000)
{
Account.State = new MenberState(Account);
}
else if (Account.Balance > 1000)
{
Account.State = new VIPState(Account);
}
}
}
//MenberState类
public class MenberState : AccountState
{
public MenberState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为" + Account.Owner + "的账户中存款" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 0&& Account.Balance <= 100)
{
Account.State = new VistorState(Account);
}
else if (Account.Balance > 1000)
{
Account.State = new VIPState(Account);
}
}
}
//VIPState类
public class VIPState : AccountState
{
public VIPState(Club account)
{
Account = account;
}
public override void Deposit(double amount)
{
Account.Balance += amount;
Console.WriteLine("向名为" + Account.Owner + "的账户中存款" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
public override void Withdraw(double amount)
{
double newBalance = Account.Balance - amount;
if (newBalance > 0)
{
Account.Balance = newBalance;
Console.WriteLine("向名为" + Account.Owner + "的账户中消费" + amount + "元,存款后账户余额为" + Account.Balance + "元");
StateChange();
}
else
{
Console.WriteLine("你的账户余额不足,无法消费,请先存款");
}
StateChange();
}
public override void StateChange()
{
if (Account.Balance > 0 && Account.Balance <= 100)
{
Account.State = new VistorState(Account);
}
else if (Account.Balance >100 && Account.Balance<=1000)
{
Account.State = new MenberState(Account);
}
}
}
//Club类
public class Club
{
public AccountState State { get; set; }
public double Balance { get; set; }
public string Owner { get; set; }
public Club(string owner, double initialAmount)
{
Owner = owner;
Balance = initialAmount;
State = new VistorState(this);
}
public void SetBalance(double amount)
{
Balance = amount;
}
public void Deposit(double amount)
{
Console.WriteLine("现要存款" + amount + "元");
State.Deposit(amount);
Console.WriteLine("账户状态变为" + State);
}
public void Withdraw(double amount)
{
Console.WriteLine("现要消费" + amount + "元");
State.Withdraw(amount);
Console.WriteLine("账户状态变为" + State);
}
}
}
}
效果
标签:
原文地址:http://www.cnblogs.com/houyanjie/p/5093041.html