State person Mode

Source: Internet
Author: User

Class sum:

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;             6 7 Namespace Club 8 {9 class program ten {one static void Main (string[] args) 12 {13 Account Account = new Account ("Legend", 270); Console.WriteLine ("successed!"); Console.WriteLine ("Name:{0}", account. Owner); Console.WriteLine ("Initial:{0}", account. Balance); Console.WriteLine ("------------------"); Account. Cost (250); Console.WriteLine ("-----------------------------------------"); Account. Deposit (100); Console.WriteLine ("-------------------"); Account. Deposit (200); Console.WriteLine ("-----------------------------------------"); Account. Cost (300); Console.WriteLine ("--------------------"); Account. DeposIt (999); Console.WriteLine ("-----------------------------------------"); The account. Cost (700); Console.WriteLine ("---------------------"); The account of the PNS. Cost (123); Console.WriteLine ("-----------------------------------------"); Console.ReadLine (); * * * * * * * * The class account of {ClientState state{get;set;} the public string owner{get; Set;} Balance{get;set public Account (String owner,double initialamount), {Ow Ner=owner; Balance=initialamount; State=new Vistorstate (this); Setbalance (double amount), the public void, Balance=amount, and the public V OID Deposit (double amount), {Console.WriteLine ("Now,deposit {0}.", amount), and State.deposit (AMO UNT); Console.WriteLine ("ClientState turn to {0}", state); 67} 68 public void Cost (double amount) {Console.WriteLine ("Now,cost {0}.", amount); . Cost (amount); Console.WriteLine ("ClientState turn to {0}", state); The clientstate}, the abstract class, the protected account account, and the public abstract void Deposit (dou ble amount); The public abstract void cost (double amount); Bayi public abstract void Check (); Vistorstate:clientstate (vistorstate) (account account) (Account=accoun) T The Deposit (double amount) of the public override void is account.balance+=amount; Onsole. WriteLine ("Deposit {0} to {1},surplus of the account has {2} affter Deposit", Amount, Account.owner, account.balance); 94 Check (); Amount} The public override void Cost (double) 98 {Newbalance=account.balance-amount double ; Account.balance-=amount;101 Console.WriteLine("cost {0} from {1},surplus of the account has {2} affter cost", Amount, Account.owner, account.balance); 102 Check (); 103}104 public override void Check () 106 if (account.balance>100 && account.balance&lt ) 107 {108 Account.state=new memberstate (account); 109}110 else if (account.balance >=1000) 111 {account.state=new Vipstate (account) 113}114}115}116 class Membersta     te:clientstate117 {118 Public memberstate (account account) 119 {account=account;121}122 123 public override void Deposit (double amount) 124 {account.balance+=amount;126 Console.WriteLine ("De Posit {0} to {1},surplus of the account has {2} affter deposit ", Amount, Account.owner, account.balance); 127 Check (); }129-public override void Cost (double amount) 131 {double Newbalance=account.balance-amoun t;133 account.balance-=amount;134 Console.WriteLine ("Cost {0} from {1},surplus of the account has {2} affter cost", Amount, ACCOUNT.O Wner, account.balance) 135 check () 136}137 public override void Check () 138 {139 if (account.b         Alance>0 && account.balance<=100) 141 {account.state = new vistorstate (account); 142          }143 else if (account.balance>=1000) 144 {145 account.state=new vipstate (account); 146         }147}148}149 class vipstate:clientstate150 {151 public vipstate (account account) 152 {153 account=account;154}155 156 public override void Deposit (double amount) 157 {158 Account.balance+=amo unt;159 Console.WriteLine ("Deposit {0} to {1},surplus of the account has {2} affter Deposit", Amount, Account.owner, Account.balance); 161}162 163 public override void Cost (double amount) 164 {165 Do Uble Newbalance=account.balance-amount;166 account.balance-=amount;167 Console.WriteLine ("Cost {0} ' from {1},surplus of account hav e {2} affter cost ", Amount, Account.owner, account.balance); 168 Check (); 169}170 public override void Chec K () 171 {172 if (account.balance>100 && account.balance<1000) 173 {174 Account . State=new memberstate (account); 175}176 else if (account.balance > 0 && account.balance <=10 0) 177 {178 account.state = new Vistorstate (account); 179}180}181}

Operation Result:

State person Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.