package sample;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

/**
 * Created by sebastian on 09.05.17.
 */
public class ChairIterator implements Iterator<Chair> {

    private ResultSet resultSet;
    private boolean hasNext = false;

    public ChairIterator(ResultSet resultSet) throws SQLException {
        this.resultSet = resultSet;
        hasNext = resultSet.next();
    }

    @Override
    public boolean hasNext() {
        return hasNext;
    }

    @Override
    public Chair next() {
        if (!hasNext) {
            return null;
        } else {
            try {
                Chair chair = new Chair(
                        resultSet.getInt("Row"),
                        resultSet.getInt("Pos"),
                        resultSet.getBoolean("isUsed"),
                        resultSet.getDouble("Price")
                );
                hasNext = resultSet.next();
                return chair;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}
