Advertisement
Guest User

ajuda

a guest
Oct 20th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1.  * To change this template file, choose Tools | Templates
  2.  * and open the template in the editor.
  3.  */
  4. package maluco;
  5.  
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8.  
  9. /**
  10.  *
  11.  * @author user
  12.  */
  13. public class PersistenciaR {
  14.    
  15.     private ArrayList<CadastraProdutos> lista;
  16.     BancoDadosR bancoDados = new BancoDadosR();
  17.    
  18.     public PersistenciaR() {
  19.         try {
  20.             lista = bancoDados.lerArquivo();
  21.         } catch (IOException ex) {
  22.             System.out.println("Erro: " + ex.getMessage());
  23.         }
  24.     }
  25.    
  26.     public void incluir(CadastraProdutos c) throws IOException{
  27.         if (lista != null) {
  28.             lista.add(c);
  29.         }else{
  30.             lista = new ArrayList<CadastraProdutos>();
  31.             lista.add(c);
  32.         }
  33.        
  34.         bancoDados.gravaArquivo(lista);
  35.     }
  36.    
  37.     public CadastraProdutos ler(int index){
  38.         CadastraProdutos retorno = null;
  39.    
  40.         for (CadastraProdutos c : lista) {
  41.             if (c.getCodigo() == index) {
  42.                 retorno = c;
  43.             }
  44.         }
  45.        
  46.         return retorno;
  47.     }
  48.    
  49.     public void atualiza (CadastraProdutos c) throws IOException{
  50.    
  51.         for (CadastraProdutos p : lista){
  52.             if (c.getCodigo() == p.getCodigo()) {
  53.                p = c;
  54.             }
  55.         }
  56.        
  57.         bancoDados.gravaArquivo(lista);
  58.        
  59.     }
  60.    
  61.    
  62.     public void apagar(int index) throws IOException{
  63.        
  64.         for (CadastraProdutos c : lista){
  65.             if (c.getCodigo() == index) {
  66.                 lista.remove(index);
  67.             }
  68.         }
  69.        
  70.         bancoDados.gravaArquivo(lista);
  71.        
  72.     }
  73.    
  74.     public int nroRegistros(){
  75.         return lista.size();
  76.     }
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement