Csharp/C Sharp/Generics/Queue — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Generic Queue
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
class Program {
public static void Main(){
Queue<Customer> custQueue = new Queue<Customer>(100);
custQueue.Enqueue(new Customer(99, "H", "P"));
custQueue.Enqueue(new Customer(77, "B", "G"));
custQueue.Enqueue(new Customer(55, "B", "S"));
custQueue.Enqueue(new Customer(88, "B", "P"));
Customer tmpCust = custQueue.Dequeue();
Console.Out.WriteLine("Dequeued: {0}", tmpCust);
foreach (Customer cust in custQueue)
Console.Out.WriteLine("Queue Item: {0}", cust);
tmpCust = custQueue.Peek();
Console.Out.WriteLine("Queue Peek: {0}", tmpCust);
custQueue.Enqueue(new Customer(88, "B", "P"));
foreach (Customer cust in custQueue)
Console.Out.WriteLine("Queue Item: {0}", cust);
}
}