package ru.job4j.iterator.iteratorprimes;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class PrimeIt implements Iterator {

    private int[] numbers;
    private int position = 0;
    private Integer primeNumber;
    private boolean hasN = false;

    public PrimeIt(final int[] numbers) {
        this.numbers = numbers;
    }
    @Override
    public boolean hasNext() {
        if (position >= numbers.length)
            throw new NoSuchElementException();
        primeNumber = numbers[position];
        for (int i = position; i < numbers.length; i++) {
            hasN = true;
            primeNumber = numbers[i];
            if (primeNumber == 1)
                continue;
            primeNumb();
            if (hasN == true) {
                break;
            }
        }
        return position < numbers.length && hasN ? true : false;
    }

    @Override
    public Object next() {
        primeNumber = null;
        for (; position < numbers.length; position++) {
            primeNumber = numbers[position];
            if (primeNumber == 1)
                continue;
            primeNumb();
            if (primeNumber != null && position < numbers.length - 1) {
                position++;
                break;
            }
        }
        if (position > numbers.length || primeNumber == null)
            throw new NoSuchElementException();
        return primeNumber;
    }

    public void primeNumb() {
        for (int n = 2; n < primeNumber - 1; n++) {
            if (primeNumber % n == 0) {
                hasN = false;
                primeNumber = null;
                break;
            }
        }
    }
}

@@ -145,16 +145,25 @@
 Iterator
+%3CInteger%3E
  %7B%0A%0A    
@@ -180,45 +180,13 @@
 t%5B%5D 
-numbers;%0A    private int position = 0
+array
 ;%0A  
@@ -199,144 +199,89 @@
 ate 
-Integer primeNumber;%0A    private boolean hasN = false;%0A%0A    public PrimeIt(final int%5B%5D numbers) %7B%0A        this.numbers = numbers
+int index;%0A%0A    public PrimeIt (int%5B%5D array) %7B%0A        this.array = array
 ;%0A    %7D%0A
     
@@ -272,24 +272,25 @@
 rray;%0A    %7D%0A
+%0A
     @Overrid
@@ -317,16 +317,17 @@
  hasNext
+ 
 () %7B%0A   
@@ -335,183 +335,69 @@
     
-if (position %3E= numbers.length)%0A            throw new NoSuchElementException();%0A        primeNumber = numbers%5Bposition%5D;%0A        for (int i = position; i %3C numbers
+boolean hasPrime = false;%0A        while (index %3C array
 .length
-; i++
 ) %7B%0A
@@ -412,175 +412,92 @@
     
-hasN = true;%0A            primeNumber = numbers%5Bi%5D;%0A            if (primeNumber == 1)%0A                continue;%0A            primeNumb();%0A            if (hasN == true) %7B
+if (isPrime(array%5Bindex%5D)) %7B%0A                hasPrime = true;%0A                break;
 %0A   
@@ -501,34 +501,25 @@
             
-    break;
+%7D
 %0A           
@@ -511,33 +511,40 @@
   %7D%0A            
-%7D
+index++;
 %0A        %7D%0A     
@@ -557,55 +557,15 @@
 urn 
-position %3C numbers.length && hasN ? true : fals
+hasPrim
 e;%0A 
@@ -599,19 +599,21 @@
 lic 
-Object
+Integer
  next
+ 
 () %7B
@@ -625,187 +625,123 @@
     
-primeNumber = null;%0A        for (; position %3C numbers.length; position++) %7B%0A            primeNumber = numbers%5Bposition%5D;%0A            if (primeNumber == 1)%0A                continue
+if (hasNext()) %7B%0A            return array%5Bindex++%5D;%0A        %7D else %7B%0A            throw new NoSuchElementException()
 ;%0A  
@@ -750,346 +750,347 @@
     
+%7D%0A
     
-primeNumb();
+%7D%0A
 %0A    
+/**%0A
      
-   if (
+* Checks if parameter is 
 prime
-N
+ n
 umber 
-!= null && position %3C numbers.length - 1) %7B%0A                position++;
+or not.%0A     * @param numberToCheck - number to check%0A     * @return - true - it is prime number, false - otherwise.
 %0A     
+*/%0A
     
-       break;%0A         
+private boolean isPrime (int numberToCheck) %7B%0A
    
-%7D%0A
      
-   %7D%0A        if (position %3E numbers.length %7C%7C primeNumber == null)%0A            throw new NoSuchElementException();%0A        return primeNumber;%0A    %7D%0A%0A    public void primeNumb() %7B
+boolean isPrime = true;%0A        if (numberToCheck == 1) %7B%0A            isPrime = false;%0A        %7D
 %0A   
@@ -1107,37 +1107,35 @@
 int 
-n
+i
  = 2; 
-n %3C primeNumber - 1; n
+i %3C numberToCheck; i
 ++) 
@@ -1156,23 +1156,25 @@
 if (
-primeNumber %25 n
+numberToCheck %25 i
  == 
@@ -1198,56 +1198,23 @@
     
-hasN = false;%0A                primeNumber = null
+isPrime = false
 ;%0A  
@@ -1254,20 +1254,44 @@
 %7D%0A        %7D%0A
+        return isPrime;%0A
     %7D%0A%7D%0A

task 151