Package controleEstoque.arquivos

Source Code of controleEstoque.arquivos.ControlCarregaArquivos

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controleEstoque.arquivos;

import controleEstoque.entidades.Estatisticas;
import controleEstoque.entidades.Estoque;
import controleEstoque.entidades.Produto;
import controleEstoque.estruturaDados.ListaProdutos;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
*
* @author User
*/
public class ControlCarregaArquivos {
   
    File file;
    /**
     * Constutor da Classe, define o atributo file como null.
     */

    public ControlCarregaArquivos() {
        file = null;
    }
   
   
    /**
     * Constutor da Classe, define o atributo file de acordo com o argumento passado.
     * @param file Arquivo de texto, contendo o path.
     */
    public ControlCarregaArquivos(File file){
        this.file = file;
    }
   
    /**
     * Construtor da classe, que define um tipo de arquivo baseado no Enum Arquivo.
     * @param arquivo Propriedades do Enum arquivo
     * @throws IOException Erro ao criar arquivo
     */
    public ControlCarregaArquivos(Arquivo arquivo) throws IOException{
        File dir = null;
        switch (arquivo){
            case PRODUTO:
                this.file = new File("Produtos/produto.txt");
                dir = new File("Produtos");
            break;
            case FUNCIONARIO:
                this.file = new File("Funcionarios/funcionario.txt");
                dir = new File("Funcionarios");
            break;
            case LOGIN:
                this.file = new File("Login/login.txt");
                dir = new File("Login");
            break;
            case FORNECEDOR:
                this.file = new File("Fornecedor/fornecedor.txt");
                dir = new File("Fornecedor");
            break;
        }       

        verificaDiretorio(file, dir);
      
    }
   
    /**
     * Cria o arquivo e o diretório caso não existam.
     * @param file Arquivo de texto
     * @param dir Caminho do arquivo
     */
    private void verificaDiretorio(File file, File dir){
        if(!dir.exists())
            dir.mkdirs();
        if(!file.exists())
            try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(ControlGravaArquivos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    /**
     * Método que verifica se o atributo file instanciado existe
     * @return true se existe, false se não.
     */
    public boolean verificaArquivo(){
        if(file.exists())
            return true;
       
        return false;
                   
    }
   
    /**
     * Método que carrega a lista de produtos direto do bloco de notas
     * @return a Lista completa com todos arquivos salvos
     */
    public ListaProdutos carregaListaProdutos(){
        ListaProdutos lista = new ListaProdutos();
       
        if(verificaArquivo()){
            try {               
                BufferedReader bufReader = new BufferedReader(new FileReader(file));
                String strReader = bufReader.readLine();
                int i = 0;               
                Produto produto = new Produto();
                Estoque estoque = new Estoque();
                Estatisticas estatistica = new Estatisticas();
               
                while(strReader != null){
                    switch (i){
                        case 0:                           
                            produto.setId(Integer.parseInt(strReader));                          
                        break;
                           
                        case 1:                           
                            produto.setNome(strReader);                          
                        break;
                           
                        case 2:
                           produto.setDescricao(strReader);                                                  
                        break;
                           
                        case 3:
                            produto.setValorUnitario(Double.parseDouble(strReader));
                        break;
                           
                        case 4:
                            estatistica.setVendaMediaMensal(Double.parseDouble(strReader));
                        break;  
                           
                        case 5:
                            estatistica.setTempoCobertura(Double.parseDouble(strReader));
                        break;  
                           
                        case 6:
                            estatistica.setEstoqueMinimo(Integer.parseInt(strReader));
                        break;  
                           
                        case 7:
                            estatistica.setEstoqueMaximo(Integer.parseInt(strReader));                                                      
                        break;                              
                           
                       case 8:
                            estoque.setQuantidade(Integer.parseInt(strReader));
                        break;       
                    }
                  i++;
                  if(i == 9){
                      estoque.setEstatistica(estatistica);
                      produto.setEstoque(estoque);
                      lista.adiciona(produto);
                      i = 0;
                  }
                     
                  strReader = bufReader.readLine();
                }
               
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.toString());
            }
           
        }
       
        return lista;
    }
   
    /**
     * Método que retorna o conteúdo de um arquivo de texto
     * @param arquivo diretório do arquivo de texto.
     * @return conteúdo do arquivo
     */
    public String retornaStringArquivo(String arquivo){
        String strReader = "", strFinal = "";       
        try{
            BufferedReader bufReader = new BufferedReader(new FileReader(arquivo));
           
            strReader = bufReader.readLine();           
            while(strReader != null){
                strFinal += strReader + "\n";
                strReader = bufReader.readLine();
            }
           
            bufReader.close();
        }catch (Exception e){
            strReader = "";
            JOptionPane.showMessageDialog(null, e.toString());
        }
       
        return strFinal;
    }
   
   
    /**
     * Método que retorna o contéudo do atributo file instanciado.
     * @return Um vetor contendo em cada indice uma das linhas do arquivo de texto.
     */
    public String[] retornaStringArquivo(){
        String strReader = "", strFinal = "";       
        String[] vetRetorno = null;
        int index = 0;
        try{
            BufferedReader bufReader = new BufferedReader(new FileReader(this.file));
            while((strReader = bufReader.readLine()) != null)
                index++;
            bufReader.close();
           
            vetRetorno = new String[index];
            index = 0;
            bufReader = new BufferedReader(new FileReader(this.file));
            while((strReader = bufReader.readLine()) != null){
                vetRetorno[index] = strReader;
                index++;
            }
           
           
        }catch (Exception e){
            strReader = "";
            JOptionPane.showMessageDialog(null, e.toString());
        }
       
        return vetRetorno;
    }
   
}
TOP

Related Classes of controleEstoque.arquivos.ControlCarregaArquivos

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.