Поиск по строке (с применением индексатора) - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день, Хочу, помимо всего прочего реализовать поиск по названию модели с применением индексатора, но так чтобы получить все возможные вхождения. Пока получается найти только первое (328 строка).
Листинг программы
  1. using System;
  2. abstract class Device
  3. {
  4. protected Double price;
  5. protected String vendor;
  6. protected String category;
  7. protected DateTime productionDate;
  8. protected Byte warranty;
  9. protected String model;
  10. public Double Price
  11. {
  12. get
  13. {
  14. return price;
  15. }
  16. set
  17. {
  18. if (price > 0) price = value;
  19. }
  20. }
  21. public String Vendor
  22. {
  23. get
  24. {
  25. return vendor;
  26. }
  27. set
  28. {
  29. vendor = value;
  30. }
  31. }
  32. public String Category
  33. {
  34. get
  35. {
  36. return category;
  37. }
  38. set
  39. {
  40. category = value;
  41. }
  42. }
  43. public DateTime ProductionDate
  44. {
  45. get
  46. {
  47. return productionDate;
  48. }
  49. set
  50. {
  51. productionDate = value;
  52. }
  53. }
  54. public Byte Warranty
  55. {
  56. get
  57. {
  58. return warranty;
  59. }
  60. set
  61. {
  62. if (warranty > 0) warranty = value;
  63. }
  64. }
  65. public String Model
  66. {
  67. get
  68. {
  69. return model;
  70. }
  71. set
  72. {
  73. model = value;
  74. }
  75. }
  76. }
  77. class Laptop : Device
  78. {
  79. public Laptop(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
  80. {
  81. this.price = price;
  82. this.vendor = vendor;
  83. this.category = category;
  84. this.productionDate = productionDate;
  85. this.warranty = warranty;
  86. this.model = model;
  87. }
  88. public override String ToString()
  89. {
  90. return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
  91. + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
  92. }
  93. }
  94. class Tablet : Device
  95. {
  96. public Tablet(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
  97. {
  98. this.price = price;
  99. this.vendor = vendor;
  100. this.category = category;
  101. this.productionDate = productionDate;
  102. this.warranty = warranty;
  103. this.model = model;
  104. }
  105. public override String ToString()
  106. {
  107. return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
  108. + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
  109. }
  110. }
  111. class Smartphone : Device
  112. {
  113. public Smartphone(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
  114. {
  115. this.price = price;
  116. this.vendor = vendor;
  117. this.category = category;
  118. this.productionDate = productionDate;
  119. this.warranty = warranty;
  120. this.model = model;
  121. }
  122. public override String ToString()
  123. {
  124. return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
  125. + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
  126. }
  127. }
  128. class Charger : Device
  129. {
  130. public Charger(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
  131. {
  132. this.price = price;
  133. this.vendor = vendor;
  134. this.category = category;
  135. this.productionDate = productionDate;
  136. this.warranty = warranty;
  137. this.model = model;
  138. }
  139. public override String ToString()
  140. {
  141. return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
  142. + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
  143. }
  144. }
  145. class Case : Device
  146. {
  147. public Case(Double price, String vendor, String category, DateTime productionDate, Byte warranty, String model)
  148. {
  149. this.price = price;
  150. this.vendor = vendor;
  151. this.category = category;
  152. this.productionDate = productionDate;
  153. this.warranty = warranty;
  154. this.model = model;
  155. }
  156. public override String ToString()
  157. {
  158. return "Vendor: " + vendor + ", model: " + model + ", price: $" + price + ", date of production: "
  159. + productionDate.ToShortDateString() + ", warranty: " + warranty + " years.";
  160. }
  161. }
  162. //////////////////////////////////////////////////////////////////////////////////////
  163. class Store
  164. {
  165. private Device[] devices;
  166. public Store(int size)
  167. {
  168. devices = new Device[size];
  169. }
  170. public int Length
  171. {
  172. get
  173. {
  174. return devices.Length;
  175. }
  176. }
  177. public Device this[int index]
  178. {
  179. get
  180. {
  181. if (index < 0 || index >= devices.Length)
  182. {
  183. throw new IndexOutOfRangeException();
  184. }
  185. else
  186. {
  187. return devices[index];
  188. }
  189. }
  190. set
  191. {
  192. devices[index] = value;
  193. }
  194. }
  195. public Device this[Double price]
  196. {
  197. get
  198. {
  199. if (price <= 0.0)
  200. {
  201. throw new IndexOutOfRangeException();
  202. }
  203. return this[FindByPrice(price)];
  204. }
  205. }
  206. public Device this[String name]
  207. {
  208. get
  209. {
  210. if (name.Length == 0)
  211. {
  212. //return null;
  213. throw new IndexOutOfRangeException();
  214. }
  215. return this[FindByName(name)];
  216. }
  217. }
  218. private Int32 FindByName(String name)
  219. {
  220. Int32 result = -1;
  221. for (int i = 0; i < devices.Length; i++)
  222. {
  223. if (devices[i].Vendor == name)
  224. {
  225. result = i;
  226. break;
  227. }
  228. }
  229. return result;
  230. }
  231. public Int32 FindByPrice(Double price)
  232. {
  233. Int32 result = -1;
  234. for (Int32 i = 0; i < devices.Length; i++)
  235. {
  236. if (devices[i].Price == price)
  237. {
  238. result = i;
  239. break;
  240. }
  241. }
  242. return result;
  243. }
  244. public Int32[] FindByPrice(Double priceFrom, Double priceTo)
  245. {
  246. Int32 count = 0;
  247. for (Int32 i = 0; i < devices.Length; i++)
  248. {
  249. if (devices[i].Price >= priceFrom && devices[i].Price <= priceTo)
  250. {
  251. count++;
  252. }
  253. }
  254. Int32[] result = new Int32[count];
  255. for (Int32 i = 0, y = 0; i < devices.Length; i++)
  256. {
  257. if (devices[i].Price >= priceFrom && devices[i].Price <= priceTo)
  258. {
  259. result[y] = i;
  260. y++;
  261. }
  262. }
  263. return result;
  264. }
  265. }
  266. class Program
  267. {
  268. static void Main(string[] args)
  269. {
  270. Store s = new Store(6);
  271. s[0] = new Laptop(5700, "Samsung", "Laptops", DateTime.Parse("2015.11.23"), 2, "AAA");
  272. s[1] = new Tablet(4700.25, "Asus", "Tablets", DateTime.Parse("2016.01.01"), 1, "BBB");
  273. s[2] = new Smartphone(3700, "HTC", "Smartphones", DateTime.Parse("2015.10.23"), 1, "CCC");
  274. s[3] = new Charger(150.55, "Hiawei", "Chargers", DateTime.Parse("2016.03.17"), 1, "DDD");
  275. s[4] = new Case(80, "no name", "Cases", DateTime.Parse("2015.08.08"), 0, "EEE");
  276. s[5] = new Laptop(6700, "Samsung", "Laptops", DateTime.Parse("2016.06.23"), 2, "FFF");
  277. /*
  278. try
  279. {
  280. foreach(int el in s.FindByPrice(4000, 6000))
  281. {
  282. Console.WriteLine(s[el]);
  283. }
  284. }
  285. catch
  286. {
  287. Console.WriteLine("Ничего не найдено!");
  288. }*/
  289. try
  290. {
  291. for (int i = 0; i < s.Length; i++)
  292. {
  293. if (s[i] == s["Samsung"])
  294. Console.WriteLine(s[i]);
  295. }
  296. }
  297. catch
  298. {
  299. Console.WriteLine("Ничего не найдено!");
  300. }
  301. //Console.WriteLine(s["Samsung"]);
  302. //Console.WriteLine(s[4700.25]);
  303. }
  304. }

Решение задачи: «Поиск по строке (с применением индексатора)»

textual
Листинг программы
  1. public IEnumerable<Device> this[String name]
  2.     {
  3.         get
  4.         {
  5.             if (name.Length == 0)
  6.             {
  7.                 //return null;
  8.                 throw new IndexOutOfRangeException();
  9.             }
  10.  
  11.             //return devices.Where(x => x.Vendor == name);
  12.             return FindByName(name);
  13.         }
  14.     }
  15.  
  16.     private IEnumerable<Device> FindByName(String name)
  17.     {
  18.         foreach (Device dev in devices) {
  19.             if (dev.Vendor == name) {
  20.                 yield return dev;
  21.             }
  22.         }
  23.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут