I'm studying java, and I'm trying to paginate a list of objects, is it possible using only java?
public class Produto {
public static List<Produto> estoque = new ArrayList<>();
private String nome;
private Double preco;
private int quantidade;
I'm using this toString()
@Override
public String toString() {
return "\nProduto: " + nome + ", preco: R$"
+ preco + ", quantidade: " + quantidade;
}
I got this pagination to work:
public static <Produto> List<Produto> getPageProduct(List<Produto> produtos, int page, int pageSize) {
if(pageSize <= 0 || page <= 0) {
throw new IllegalArgumentException("invalid page size: " + pageSize);
}
int fromIndex = (page - 1) * pageSize;
if(produtos == null || produtos.size() <= fromIndex){
return Collections.emptyList();
}
return produtos.subList(fromIndex, Math.min(fromIndex + pageSize, produtos.size()));
}
But I cant get around having to input manually the number of the page
System.out.println(getPageProduct(getProducts(), 1, 5));
Thread.sleep(3000);
What would be the best solution?