package ru.job4j.start;

import ru.job4j.models.Item;
import java.util.Arrays;
/**
* Tracker.
*
* auhtor Indyukovs
* version 0.1
*/
public class Tracker {
/**
* @param items
*/
	private Item[] items = new Item[10];
/**
* @param position second
*/
	private int position = 0;
/**
* method add.
* @param item first
* @return item
*/
	public Item add(Item item) {
		//item.setId(String.valueof(RN.next()));
		this.items[position++] = item;
		return item;
	}
/**
* method finById.
* @param id first
* @return result
*/
	protected Item findById(String id) {
		Item result = null;
			for (Item item : items) {
				if (item != null && item.getId().equals(id)) {
				result = item;
				break;
				}
			}
		return result;
	}
/**
* method update.
* @param item first
*/
	public void update(Item item) {
		for (int i = 0; i < items.length; i++) {
			if (items[i] != null && item.getId().equals(items[i].getId())) {
				items[i] = item;
				break;
			}
		}
	}
/**
* method delete.
* @param item first
*/
	public void delete(Item item) {
		/*for (Item item1 : items) {
			if (item1 != null && item.getId().equals(item1.getId())) {
				item1 = null;
			}
		}*/
		for (int i = 0; i < items.length; i++) {
			if (this.items[i] != null && item.getId().equals(this.items[i].getId())) {
				this.items[i] = null;
				position--;
				break;
			}
		}
	}
/**
* method findAll.
* @return tempItem
*/
	public Item[] findAll() {
		Item[] tempItem = new Item[items.length];
		int a = 0;
		int b = 0;
		for (int i = 0; i < items.length; i++) {
			if (items[i] != null) {
			tempItem[a++] = items[i];
			} else {
			b++;
			}
		}
		return Arrays.copyOf(tempItem, items.length - b);
	}
/**
* method findByName.
* @param key first
* @return tempItem
*/
	public Item[] findByName(String key) {
		Item[] tempItem = new Item[items.length];
		//System.arraycopy(items, 0, tempItem, 0, items.length);
		int a = 0;
		for (int i = 0, j = 0; i < items.length; i++) {
			if (items[i] != null && items[i].getName().equals(key)) {
				tempItem[j++] = items[i];
			} else {
			a++;
			}
		}
		return Arrays.copyOf(tempItem, items.length - a);
	}
/**
* method getAll.
* @return items
*/
	public Item[] getAll() {
		return items;
	}
}
@@ -1021,130 +1021,8 @@
 ) %7B%0A
-%09%09/*for (Item item1 : items) %7B%0A%09%09%09if (item1 != null && item.getId().equals(item1.getId())) %7B%0A%09%09%09%09item1 = null;%0A%09%09%09%7D%0A%09%09%7D*/%0A
 %09%09fo
@@ -1056,24 +1056,24 @@
 gth; i++) %7B%0A
+
 %09%09%09if (this.
@@ -1335,21 +1335,8 @@
  0;%0A
-%09%09int b = 0;%0A
 %09%09fo
@@ -1446,16 +1446,8 @@
 e %7B%0A
-%09%09%09b++;%0A
 %09%09%09%7D
@@ -1488,24 +1488,9 @@
 em, 
-items.length - b
+a
 );%0A%09
@@ -1698,33 +1698,33 @@
 .length);%0A%09%09int 
-a
+j
  = 0;%0A%09%09for (int
@@ -1731,15 +1731,8 @@
  i =
- 0, j =
  0; 
@@ -1863,16 +1863,8 @@
 e %7B%0A
-%09%09%09a++;%0A
 %09%09%09%7D
@@ -1905,24 +1905,9 @@
 em, 
-items.length - a
+j
 );%0A%09

task 396