Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package maluco;
- import java.io.IOException;
- import java.util.ArrayList;
- /**
- *
- * @author user
- */
- public class PersistenciaR {
- private ArrayList<CadastraProdutos> lista;
- BancoDadosR bancoDados = new BancoDadosR();
- public PersistenciaR() {
- try {
- lista = bancoDados.lerArquivo();
- } catch (IOException ex) {
- System.out.println("Erro: " + ex.getMessage());
- }
- }
- public void incluir(CadastraProdutos c) throws IOException{
- if (lista != null) {
- lista.add(c);
- }else{
- lista = new ArrayList<CadastraProdutos>();
- lista.add(c);
- }
- bancoDados.gravaArquivo(lista);
- }
- public CadastraProdutos ler(int index){
- CadastraProdutos retorno = null;
- for (CadastraProdutos c : lista) {
- if (c.getCodigo() == index) {
- retorno = c;
- }
- }
- return retorno;
- }
- public void atualiza (CadastraProdutos c) throws IOException{
- for (CadastraProdutos p : lista){
- if (c.getCodigo() == p.getCodigo()) {
- p = c;
- }
- }
- bancoDados.gravaArquivo(lista);
- }
- public void apagar(int index) throws IOException{
- for (CadastraProdutos c : lista){
- if (c.getCodigo() == index) {
- lista.remove(index);
- }
- }
- bancoDados.gravaArquivo(lista);
- }
- public int nroRegistros(){
- return lista.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement