Guest User

Untitled

a guest
Nov 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. /**
  2. * Редактирование заявок.
  3. *
  4. * @param fresh новая заявка.
  5. */
  6. public void update(Item fresh) {
  7. for (int index = 0; index != items.length; ++index) {
  8. if (items[index].getId().equals(fresh.getId())) {
  9. items[index] = fresh;
  10. break;
  11. }
  12. }
  13. }
  14.  
  15. private void changeItem() {
  16. System.out.println("------------ Изменения заявки --------------");
  17. String newname = this.input.ask("Введите имя заявки :");
  18. String newdesc = this.input.ask("Введите описание заявки :");
  19. Item newItem = new Item(newname, newdesc, System.currentTimeMillis());
  20. newItem.setId(tracker.getAll()[0].getId());
  21. this.tracker.update(newItem);
  22. }
  23.  
  24. public class Item implements Cloneable {
  25.  
  26. /**
  27. * Поле имя.
  28. */
  29. private String name;
  30.  
  31. /**
  32. * Поле описание.
  33. */
  34. private String description;
  35.  
  36. /**
  37. * Поле создание.
  38. */
  39. private Long create;
  40.  
  41. /**
  42. * Поле id.
  43. */
  44. private String id;
  45.  
  46. /**
  47. * Конструктор класса Item.
  48. *
  49. * @param name имя.
  50. * @param description описание.
  51. * @param create создание.
  52. */
  53. public Item(String name, String description, Long create) {
  54. this.name = name;
  55. this.description = description;
  56. this.create = create;
  57. }
  58.  
  59. @Override
  60. public String toString() {
  61. String s = "name = " + name + "n" + "description = " + description;
  62. return s;
  63. }
  64.  
  65. /**
  66. * GetName.
  67. *
  68. * @return String
  69. */
  70. public String getName() {
  71. return name;
  72. }
  73.  
  74. /**
  75. * SetName.
  76. *
  77. * @param name имя.
  78. */
  79. public void setName(String name) {
  80. this.name = name;
  81. }
  82.  
  83. /**
  84. * GetDescription.
  85. *
  86. * @return String
  87. */
  88. public String getDescription() {
  89. return description;
  90. }
  91.  
  92. /**
  93. * SetDescription.
  94. *
  95. * @param description описание.
  96. */
  97. public void setDescription(String description) {
  98. this.description = description;
  99. }
  100.  
  101. /**
  102. * GetCreate.
  103. *
  104. * @return Long
  105. */
  106. public Long getCreate() {
  107. return create;
  108. }
  109.  
  110. /**
  111. * SetCreate.
  112. *
  113. * @param create создание.
  114. */
  115. public void setCreate(Long create) {
  116. this.create = create;
  117. }
  118.  
  119. /**
  120. * GetId.
  121. *
  122. * @return String
  123. */
  124. public String getId() {
  125. return id;
  126. }
  127.  
  128. /**
  129. * SetId.
  130. *
  131. * @param id id.
  132. */
  133. public void setId(String id) {
  134. this.id = id;
  135. }
  136. }
  137.  
  138. public class Tracker {
  139. /**
  140. * Поле массив item.
  141. */
  142. private Item[] items = new Item[100];
  143.  
  144. /**
  145. * Поле позиция заявок.
  146. */
  147. private int position = 0;
  148.  
  149. /**
  150. * Поле генерация id.
  151. */
  152. private static final Random RN = new Random();
  153.  
  154. /**
  155. * Добавление заявок.
  156. *
  157. * @param item массив.
  158. * @return Item
  159. */
  160. public Item add(Item item) {
  161. item.setId(this.generateId());
  162. this.items[position++] = item;
  163. return item;
  164. }
  165.  
  166. /**
  167. * Редактирование заявок.
  168. *
  169. * @param fresh новая заявка.
  170. */
  171. public void update(Item fresh) {
  172. for (int index = 0; index != items.length; ++index) {
  173. if (items[index].getId().equals(fresh.getId())) {
  174. items[index] = fresh;
  175. break;
  176. }
  177. }
  178. }
  179.  
  180. /**
  181. * Удаление заявок.
  182. *
  183. * @param id заявки.
  184. */
  185. public void delete(String id) {
  186. for (int i = 0; i < items.length; i++) {
  187. if (items[i] != null && items[i].getId().equals(id)) {
  188. items[i] = null;
  189. }
  190. }
  191. }
  192.  
  193. /**
  194. * Получение списка всех заяво.
  195. *
  196. * @return Item[]
  197. */
  198. public Item[] getAll() {
  199. Item[] result = new Item[this.position];
  200. for (int index = 0; index != this.position; index++) {
  201. result[index] = this.items[index];
  202. }
  203. return result;
  204. }
  205.  
  206. /**
  207. * Получение списка по имени.
  208. *
  209. * @param key ключевое слово для поиска
  210. * @return Item[].
  211. */
  212. public Item[] findByName(String key) {
  213. Item[] result = new Item[0];
  214. for (int i = 0; i < items.length; i++) {
  215. if (items[i] != null && items[i].getName().equals(key)) {
  216. Item[] temp = new Item[result.length + 1];
  217. System.arraycopy(result, 0, temp, 0, result.length);
  218. temp[temp.length - 1] = items[i];
  219. result = temp;
  220. }
  221. }
  222. return result;
  223. }
  224.  
  225. /**
  226. * Получение заявки по id.
  227. *
  228. * @param id заявки.
  229. * @return Item
  230. */
  231. public Item findById(String id) {
  232. Item result = null;
  233. for (Item item : items) {
  234. if (item != null && item.getId().equals(id)) {
  235. result = item;
  236. break;
  237. }
  238. }
  239. return result;
  240. }
  241.  
  242. /**
  243. * Генерация id.
  244. *
  245. * @return String
  246. */
  247. public String generateId() {
  248. return String.valueOf(System.currentTimeMillis() + RN.nextInt(100));
  249. }
  250. }
  251.  
  252. boolean equalsNameDescription(Item item){
  253. return this.name.equals(item.name) &&
  254. this.description.equals(item.description)
  255. }
  256.  
  257. int getIdByItem(Item findItem){
  258. for(Item item : items){
  259. if(item != null && item.equalsNameDescription(findItem))
  260. return item.getId();
  261. }
  262.  
  263. return -1;
  264. }
  265.  
  266. private void changeItem() {
  267. System.out.println("------------ Изменения заявки --------------");
  268. String newname = this.input.ask("Введите имя заявки :");
  269. String newdesc = this.input.ask("Введите описание заявки :");
  270. Item newItem = new Item(newname, newdesc, System.currentTimeMillis());
  271. int id = tracker.getIdByItem(newItem);
  272. if(id == -1){
  273. // здесь напишите, что делать если такой запиcи нет
  274. throw new NotFoundException();
  275. }
  276. newItem.setId(id);
  277. this.tracker.update(newItem);
  278. }
Add Comment
Please, Sign In to add comment