package ru.job4j.map;

public class Map<K, V> {

    public Object[] container = new Object[10];
    int capacity = 10;
    private int index = 0;

    public Object[] ensureCapacity(Object[] array, int length) {
        capacity = capacity + length;
        Object[] newArray = new Object[capacity];
        for (int i = 0; i < container.length; i++) {
            if (container[i]!=null) {
                newArray[i] = container[i];
            }
        }
        return newArray;
    }

    public int getCell(K key) {
        return Math.abs(key.hashCode() % container.length);
    }

    public boolean insert(K key, V value) {
        if (index > container.length - 1) {
            container = ensureCapacity(container, index + 10);
        }

        int cell = getCell(key);

        if (container[cell] == null) {
            container[cell] = value;
            index++;
         return true;
        } else {
            return false;
        }
    }

    public V get(K key) {
        int cell = getCell(key);
        return (V)container[cell];
    }

    public boolean delete(K key) {
        int cell = getCell(key);
        if (container[cell]!=null) {
            container[cell] = null;
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Entry<Integer, String> entry1 = new Entry<>(1, "first");
        Entry<Integer, String> entry2 = new Entry<>(2, "second");
        Entry<Integer, String> entry3 = new Entry<>(3, "third");
        Map<Integer, String> map = new Map<>();
        map.insert(entry1.key, entry1.value);
        map.insert(entry2.key, entry2.value);
        map.insert(entry3.key, entry3.value);
    }
}

@@ -16,16 +16,63 @@
 j.map;%0A%0A
+import javafx.beans.binding.ObjectExpression;%0A%0A
 public c
@@ -137,24 +137,32 @@
 ct%5B10%5D;%0A    
+private 
 int capacity
@@ -235,24 +235,8 @@
 ity(
-Object%5B%5D array, 
 int 
@@ -414,18 +414,20 @@
 ainer%5Bi%5D
+ 
 !=
+ 
 null) %7B%0A
@@ -446,18 +446,129 @@
     
-newArray%5Bi
+int cell = Math.abs(i %25 newArray.length);%0A                if (newArray%5Bcell%5D == null) %7B%0A                    newArray%5Bcell
 %5D = 
@@ -581,16 +581,34 @@
 ner%5Bi%5D;%0A
+                %7D%0A
         
@@ -773,23 +773,41 @@
 boolean 
-insert(
+addEntry(Object%5B%5D array, 
 K key, V
@@ -820,126 +820,8 @@
 ) %7B%0A
-        if (index %3E container.length - 1) %7B%0A            container = ensureCapacity(container, index + 10);%0A        %7D%0A%0A
     
@@ -849,17 +849,16 @@
 l(key);%0A
-%0A
         
@@ -857,33 +857,29 @@
         if (
-container
+array
 %5Bcell%5D == nu
@@ -892,33 +892,29 @@
             
-container
+array
 %5Bcell%5D = val
@@ -938,16 +938,19 @@
 ndex++;%0A
+   
         
@@ -1015,32 +1015,238 @@
        %7D%0A    %7D%0A%0A
+    public boolean insert(K key, V value) %7B%0A        if (index %3E container.length - 1) %7B%0A            container = ensureCapacity(index + 10);%0A        %7D%0A%0A        return addEntry(container, key, value);%0A    %7D%0A%0A
     public V get
@@ -1306,16 +1306,17 @@
 turn (V)
+ 
 containe
@@ -1426,18 +1426,20 @@
 er%5Bcell%5D
+ 
 !=
+ 
 null) %7B%0A
@@ -1981,21 +1981,149 @@
 try3.value);
+%0A%0A        for (int i = 0; i %3C map.container.length; i++) %7B%0A            System.out.println(i+ %22) %22 + map.container%5Bi%5D);%0A        %7D
 %0A    %7D%0A%7D%0A

task #1008