Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp4Testing
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Customers customers = new Customers();
  14. Customer customer = new Customer("C00066", "John", "Smith", "1234 Main St", "", "Boise", "ID", "53432", "US");
  15. customers.Add(customer);
  16. customer = new Customer("C00017", "Bob", "Jones", "1001 First Ave", "", "Detroit", "MI", "84772", "US");
  17. customers.Add(customer);
  18. customer = new Customer("C00024", "Susan", "Day", "PO Box 2509", "", "Dallas", "TX", "57212", "US");
  19. customers.Add(customer);
  20. customer = new Customer("C00009", "Bill", "Mason", "987 Washington Av", "", "Los Angeles", "CA", "90254", "US");
  21. customers.Add(customer);
  22. customer = new Customer("C00042", "Alice", "Jones", "1401 G St", "", "Atlanta", "GA", "65354", "US");
  23. customers.Add(customer);
  24. customer = new Customer("C00035", "Joan", "King", "879 Chestnut St", "", "Philadelphia", "PA", "22531", "US");
  25. customers.Add(customer);
  26. customer = new Customer("C00013", "John", "Smith", "67 Filmore Ave", "", "Chicago", "IL", "61535", "US");
  27. customers.Add(customer);
  28. Console.WriteLine("Customers in order as added:");
  29. foreach (Customer cust in customers)
  30. {
  31. Console.WriteLine(cust.AccountNo + ", " + cust.FirstName + " " + cust.LastName);
  32.  
  33. }
  34. Console.WriteLine();
  35.  
  36. customers.Sort();
  37. Console.WriteLine("Customers sorted by Lastname, FirstName, AccountNo:");
  38. foreach (Customer cust in customers)
  39. {
  40. Console.WriteLine(cust.AccountNo + ", " + cust.FirstName + " " + cust.LastName);
  41. }
  42. Console.ReadLine();
  43. }
  44. }
  45. }
  46.  
  47. using System;
  48. using System.Collections;
  49. using System.Collections.Generic;
  50. using System.Linq;
  51. using System.Web;
  52.  
  53. namespace ConsoleApp4Testing
  54. {
  55. public class Customers : IEnumerable
  56. {
  57. private ArrayList m_customerList;
  58.  
  59. public Customers()
  60. {
  61. m_customerList = new ArrayList();
  62. }
  63.  
  64. public Customer this[int index]
  65. {
  66. get { return (Customer)m_customerList[index]; }
  67. set
  68. {
  69. if (index > (m_customerList.Count - 1))
  70. { m_customerList.Add(value); }
  71. else
  72. { m_customerList[index] = value; }
  73. }
  74. }
  75. public Customer this[Guid custId]
  76. {
  77. get { return (Customer)m_customerList[indexof(custId)]; }
  78. }
  79. public int indexof(Guid custId)
  80. {
  81. int i = 0;
  82. foreach (Customer cst in this.m_customerList)
  83. {
  84. if (cst.ID == custId) { break; }
  85. i++;
  86. }
  87. if (i >= this.count)
  88. { return -1; }
  89. else
  90. { return i; }
  91. }
  92. public bool Exists(Guid custId)
  93. {
  94. int i = 0;
  95. foreach (Customer cst in this.m_customerList)
  96. {
  97. if (cst.ID == custId) { break; }
  98. i++;
  99. }
  100. if (i >= this.count)
  101. { return false; }
  102. else
  103. { return true; }
  104. }
  105. public void Add(Customer customer)
  106. {
  107. //if (this.indexof(customer.ID) < 0) //Don't add the customer if it already exists
  108. //{
  109. this.m_customerList.Add(customer);
  110. //}
  111. }
  112. public void Sort()
  113. {
  114. try {
  115. this.m_customerList.Sort();
  116. }
  117. catch (Exception ex)
  118. { System.Diagnostics.Debug.Print(ex.ToString()); }
  119. }
  120. public int count
  121. {
  122. get { return m_customerList.Count; }
  123. }
  124. // IEnumerable Interface Implementation:
  125. // Declaration of the GetEnumerator() method
  126. // required by IEnumerable
  127. public IEnumerator GetEnumerator()
  128. {
  129. return new customerEnumerator(this);
  130. }
  131.  
  132. // Inner class implements IEnumerator interface:
  133. private class customerEnumerator : IEnumerator
  134. {
  135. private int position = -1;
  136. private Customers cstmrs;
  137.  
  138. public customerEnumerator(Customers cstmrs)
  139. {
  140. this.cstmrs = cstmrs;
  141. }
  142.  
  143. // Declare the MoveNext method required by IEnumerator:
  144. public bool MoveNext()
  145. {
  146. if (position < cstmrs.m_customerList.Count - 1)
  147. {
  148. position++;
  149. return true;
  150. }
  151. else
  152. {
  153. return false;
  154. }
  155. }
  156.  
  157. // Declare the Reset method required by IEnumerator:
  158. public void Reset()
  159. {
  160. position = -1;
  161. }
  162.  
  163. // Declare the Current property required by IEnumerator:
  164. public object Current
  165. {
  166. get
  167. {
  168. return cstmrs.m_customerList[position];
  169. }
  170. }
  171. }
  172. }
  173.  
  174. public class Customer : IComparable<Customer>
  175. {
  176. private Guid _id;
  177. private string _acctNo;
  178. private string _frstNm;
  179. private string _lastNm;
  180. private string _addr1;
  181. private string _addr2;
  182. private string _city;
  183. private string _st;
  184. private string _postal;
  185. private string _country;
  186.  
  187. public Customer(Guid id)
  188. {
  189. this._id = id;
  190. }
  191. public Customer(string acctNo, string frstNm, string lastNm, string addr1, string addr2, string city, string state, string postal, string country)
  192. {
  193. this._acctNo = acctNo;
  194. this._frstNm = frstNm;
  195. this._lastNm = lastNm;
  196. this._addr1 = addr1;
  197. this._addr2 = addr2;
  198. this._city = city;
  199. this._st = state;
  200. this._postal = postal;
  201. this._country = country;
  202. }
  203. public int CompareTo(Customer c)
  204. {
  205. int compare;
  206. compare = String.Compare(this.LastName, c.LastName, true);
  207. if (compare == 0)
  208. {
  209. compare = this.FirstName.CompareTo(c.FirstName);
  210. if (compare == 0)
  211. {
  212. compare = this.AccountNo.CompareTo(c.AccountNo);
  213. }
  214. }
  215. return compare;
  216. }
  217. public Guid ID
  218. {
  219. get { return _id; }
  220. set { this._id = value; }
  221. }
  222. public string AccountNo
  223. {
  224. get { return _acctNo; }
  225. set { this._acctNo = value; }
  226. }
  227. public string FirstName
  228. {
  229. get { return _frstNm; }
  230. set { this._frstNm = value; }
  231. }
  232. public string LastName
  233. {
  234. get { return _lastNm; }
  235. set { this._lastNm = value; }
  236. }
  237. public string Address1
  238. {
  239. get { return _addr1; }
  240. set { this._addr1 = value; }
  241. }
  242. public string Address2
  243. {
  244. get { return _addr2; }
  245. set { this._addr2 = value; }
  246. }
  247. public string City
  248. {
  249. get { return _city; }
  250. set { this._city = value; }
  251. }
  252. public string State
  253. {
  254. get { return _st; }
  255. set { this._st = value; }
  256. }
  257. public string Postal
  258. {
  259. get { return _postal; }
  260. set { this._postal = value; }
  261. }
  262. public string Country
  263. {
  264. get { return _country; }
  265. set { this._country = value; }
  266. }
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement