static void Main(string[] args) { string Continue="";//用来保存是否还需要交易标志 List< Account > listAccounts= new List{ new Account(101), new Account(102), new Account(99), new Account(250), new Account(200) }; foreach (Account item in listAccounts) { Console.WriteLine(item.Balance); } Console.WriteLine("***************************************"); listAccounts.Sort(); foreach (Account item in listAccounts) { Console.WriteLine(item.Balance); } Console.Read(); }
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Collections; namespace Banking{ class Account:IComparable { public Account() { } public Account(int balance) { this.balance = balance; } private int balance; public int Balance { get { return balance; } set { if (value<=0) { this.balance = 0; } else { this.balance = value; } } } public bool Deposit(int deposit) { if (deposit<=0) { Console.WriteLine("输入的存款数目不能为负数,请重新输入:"); return false ; } Mutex mutex = new Mutex(true, "Mutex1"); mutex.WaitOne(); this.balance += deposit; mutex.Close(); Console.WriteLine("系统正在处理,请耐心等待..."); Thread.Sleep(3000); Console.WriteLine("存款交易成功!"); Thread.Sleep(2000); return true; } public bool WithDraw(int draw) { if (draw<0) { Console.WriteLine("输入的取款数目不能为负数,请重新输入:"); return false; } if (draw>this.balance) { Console.WriteLine("你的余额不足以支付你的申请金额,请重新输入:"); return false; } Mutex mutex = new Mutex(true, "Mutex2"); mutex.WaitOne(); this.balance -= draw; mutex.Close(); Console.WriteLine("系统正在处理,请耐心等待..."); Thread.Sleep(3000); Console.WriteLine("取款交易成功!"); Thread.Sleep(2000); return true; } public int CompareTo(object obj) { Account a = obj as Account; if (this.Balance > a.Balance) { return 1; } else if (this.Balance < a.Balance) { return -1; } else if (this.Balance == a.Balance) { return 0; } return 2; } }}