Job Title
1. Draw UML Diagram
2. Write code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace status
{
Class Program
{
static void Main (string[] args)
{
Club account = new club ("Zhang San", 0);
Console.WriteLine ("Successful account Opening, Name:" + accounts.) Owner + ", Initial amount:" + account. Balance);
Console.WriteLine ("------------------------");
Account. Deposit (50);
Console.WriteLine ("------------------------");
Account. Deposit (100);
Console.WriteLine ("------------------------");
Console.ReadLine ();
}
Accountstate class
Public abstract class Accountstate
{
protected Club Account;
Deposit
public abstract void Deposit (double amount);
Consumption
public abstract void withdraw (double amount);
Check account status
public abstract void StateChange ();
}
Vistorstate class
public class Vistorstate:accountstate
{
Public vistorstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("+amount+" in the account of "+account.owner+", the account balance after deposit is "+account.balance+");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
StateChange ();
}
public override void StateChange ()
{
if (Account.balance > && account.balance <= 1000)
{
Account.state = new Menberstate (account);
}
else if (account.balance > 1000)
{
Account.state = new Vipstate (account);
}
}
}
Menberstate class
public class Menberstate:accountstate
{
Public menberstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("To the account named" + Account.owner + "deposit" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
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 class
public class Vipstate:accountstate
{
Public vipstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("To the account named" + Account.owner + "deposit" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
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 Class
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 ("Now to deposit" + Amount + "Yuan");
State.deposit (amount);
Console.WriteLine ("Account status changed to" + state);
}
public void withdraw (double amount)
{
Console.WriteLine ("Now to consume" + Amount + "Yuan");
State.withdraw (amount);
Console.WriteLine ("Account status changed to" + state);
}
}
}
}
Effect
A small example of state patterns in software architecture and design patterns