Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Редактирование заявок.
- *
- * @param fresh новая заявка.
- */
- public void update(Item fresh) {
- for (int index = 0; index != items.length; ++index) {
- if (items[index].getId().equals(fresh.getId())) {
- items[index] = fresh;
- break;
- }
- }
- }
- private void changeItem() {
- System.out.println("------------ Изменения заявки --------------");
- String newname = this.input.ask("Введите имя заявки :");
- String newdesc = this.input.ask("Введите описание заявки :");
- Item newItem = new Item(newname, newdesc, System.currentTimeMillis());
- newItem.setId(tracker.getAll()[0].getId());
- this.tracker.update(newItem);
- }
- public class Item implements Cloneable {
- /**
- * Поле имя.
- */
- private String name;
- /**
- * Поле описание.
- */
- private String description;
- /**
- * Поле создание.
- */
- private Long create;
- /**
- * Поле id.
- */
- private String id;
- /**
- * Конструктор класса Item.
- *
- * @param name имя.
- * @param description описание.
- * @param create создание.
- */
- public Item(String name, String description, Long create) {
- this.name = name;
- this.description = description;
- this.create = create;
- }
- @Override
- public String toString() {
- String s = "name = " + name + "n" + "description = " + description;
- return s;
- }
- /**
- * GetName.
- *
- * @return String
- */
- public String getName() {
- return name;
- }
- /**
- * SetName.
- *
- * @param name имя.
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * GetDescription.
- *
- * @return String
- */
- public String getDescription() {
- return description;
- }
- /**
- * SetDescription.
- *
- * @param description описание.
- */
- public void setDescription(String description) {
- this.description = description;
- }
- /**
- * GetCreate.
- *
- * @return Long
- */
- public Long getCreate() {
- return create;
- }
- /**
- * SetCreate.
- *
- * @param create создание.
- */
- public void setCreate(Long create) {
- this.create = create;
- }
- /**
- * GetId.
- *
- * @return String
- */
- public String getId() {
- return id;
- }
- /**
- * SetId.
- *
- * @param id id.
- */
- public void setId(String id) {
- this.id = id;
- }
- }
- public class Tracker {
- /**
- * Поле массив item.
- */
- private Item[] items = new Item[100];
- /**
- * Поле позиция заявок.
- */
- private int position = 0;
- /**
- * Поле генерация id.
- */
- private static final Random RN = new Random();
- /**
- * Добавление заявок.
- *
- * @param item массив.
- * @return Item
- */
- public Item add(Item item) {
- item.setId(this.generateId());
- this.items[position++] = item;
- return item;
- }
- /**
- * Редактирование заявок.
- *
- * @param fresh новая заявка.
- */
- public void update(Item fresh) {
- for (int index = 0; index != items.length; ++index) {
- if (items[index].getId().equals(fresh.getId())) {
- items[index] = fresh;
- break;
- }
- }
- }
- /**
- * Удаление заявок.
- *
- * @param id заявки.
- */
- public void delete(String id) {
- for (int i = 0; i < items.length; i++) {
- if (items[i] != null && items[i].getId().equals(id)) {
- items[i] = null;
- }
- }
- }
- /**
- * Получение списка всех заяво.
- *
- * @return Item[]
- */
- public Item[] getAll() {
- Item[] result = new Item[this.position];
- for (int index = 0; index != this.position; index++) {
- result[index] = this.items[index];
- }
- return result;
- }
- /**
- * Получение списка по имени.
- *
- * @param key ключевое слово для поиска
- * @return Item[].
- */
- public Item[] findByName(String key) {
- Item[] result = new Item[0];
- for (int i = 0; i < items.length; i++) {
- if (items[i] != null && items[i].getName().equals(key)) {
- Item[] temp = new Item[result.length + 1];
- System.arraycopy(result, 0, temp, 0, result.length);
- temp[temp.length - 1] = items[i];
- result = temp;
- }
- }
- return result;
- }
- /**
- * Получение заявки по id.
- *
- * @param id заявки.
- * @return Item
- */
- public Item findById(String id) {
- Item result = null;
- for (Item item : items) {
- if (item != null && item.getId().equals(id)) {
- result = item;
- break;
- }
- }
- return result;
- }
- /**
- * Генерация id.
- *
- * @return String
- */
- public String generateId() {
- return String.valueOf(System.currentTimeMillis() + RN.nextInt(100));
- }
- }
- boolean equalsNameDescription(Item item){
- return this.name.equals(item.name) &&
- this.description.equals(item.description)
- }
- int getIdByItem(Item findItem){
- for(Item item : items){
- if(item != null && item.equalsNameDescription(findItem))
- return item.getId();
- }
- return -1;
- }
- private void changeItem() {
- System.out.println("------------ Изменения заявки --------------");
- String newname = this.input.ask("Введите имя заявки :");
- String newdesc = this.input.ask("Введите описание заявки :");
- Item newItem = new Item(newname, newdesc, System.currentTimeMillis());
- int id = tracker.getIdByItem(newItem);
- if(id == -1){
- // здесь напишите, что делать если такой запиcи нет
- throw new NotFoundException();
- }
- newItem.setId(id);
- this.tracker.update(newItem);
- }
Add Comment
Please, Sign In to add comment