Opiniões para código criado

Java

21/02/2014

Olá pessoal.

Sou novato em Java e ontem desenvolvi um algoritmo em Java que faz cadastro de usuários e o manda para um arquivo .txt no desktop, e além de cadastrar, esse mesmo programinha faz login com os dados que estão dentro desse .txt.

Vou postar o código aqui, ele está funcionando perfeitamente, contudo, queria a opinião de vocês sobre o que posso melhorar nele, só para fim de aprendizagem mesmo. Queria a opinião de vocês sobre como melhorar a performance dele.

DoLogin.java
package br.com.testando;

import java.io.*;

public class DoLogin {
    
    private File f = new File("C:\\Users\\Guilherme\\Desktop\\account\\log.txt");
    private FileReader fr;
    private BufferedReader br ;
    private String line1;
    private String line2;
    
    public DoLogin(String u, String s) throws FileNotFoundException, IOException {
        
        getFileData();
        
        if ( line1.equals(u) && line2.equals(s)){
            System.out.println("Logado.");
        } else
            System.err.println("Nome de usuário ou senha inválido.");

    }
    
    private void getFileData() throws FileNotFoundException, IOException {
        
        fr = new FileReader(f);
        br = new BufferedReader(fr);
        
        line1 = br.readLine();
        line2 = br.readLine();
        
        fr.close();
        br.close();
    }
}


CreateAccount.java
package br.com.testando;

import java.io.*;
public class CreateAccount {
    
     private String dir = "C:\\Users\\Guilherme\\Desktop\\account";
     private String fil = "\\log.txt";
     private File directory = new File(dir);
     private File file = new File(dir, fil);
     FileWriter fw;
     PrintWriter pw;
     boolean test;
     
     
     public CreateAccount(String u, String s) throws IOException, FileNotFoundException {
         
         if(createDir() == true && createFile() == true) {
             putData(u, s);
         } else {
             putData(u, s);
         }
     }
     
     private boolean createDir() {
         
         if((test = directory.mkdir()) == true){
             return true;
         } else {
             return false;
         }
     }
     
     private boolean createFile() throws IOException {
         
         if ((test = file.createNewFile()) == true) {
             return true;
         } else {
             return false;
         }
     }
     
     
     private void putData(String u, String s) throws FileNotFoundException, IOException {
         
         fw = new FileWriter(dir + fil, false);
         pw = new PrintWriter(fw);
         
         pw.println(u);
         pw.println(s);
         
         pw.flush();
         fw.close();
         pw.close();
     }
}


MainModule.java

package br.com.testando;

import java.io.*;
import java.util.*;

public class MainModule {
    
    private Scanner s;
    private String st;
    private CreateAccount ca;
    private DoLogin dl;
    
    public static void main(String[] args) {
        MainModule mm = new MainModule();
        mm.catchCommand();
        
        try {
            switch(mm.st) {
            case "login":
                mm.catchLogin();
                break;
                
            case "registrar":
                mm.catchRegister();
                break;
                
            default:
                System.out.println("Esse comando não é reconhecido.");
        }
            
        } catch(IOException e){
            System.err.println("Erro no fluxo de entrada/saida de dados.");
        }
        
    }
    
    private void catchCommand() {
        
        s = new Scanner(System.in);
        System.out.printf("> ");
        st = s.nextLine();
    }
    
    private void catchLogin() throws FileNotFoundException, IOException {
        
        String user;
        String pass;
        
        s = new Scanner(System.in);
        
        System.out.println("Por favor, digite um login e uma senha.");
        System.out.printf("> ");
        user = s.nextLine();
        
        System.out.printf("> ");
        pass = s.nextLine();
        
        dl = new DoLogin(user, pass);
        }
    
    private void catchRegister() throws IOException, FileNotFoundException {
        
        String user;
        String pass;
        
        s = new Scanner(System.in);
        
        System.out.println("Digite o login e a senha para cadastro.");
        System.out.printf("> ");
        user = s.nextLine();
        
        System.out.printf("> ");
        pass = s.nextLine();
        
        ca = new CreateAccount(user, pass);
    }
    }
Guilherme Caique

Guilherme Caique

Curtidas 0

Respostas

Eduardo Pessoa

Eduardo Pessoa

21/02/2014

opinião de um eterno estudante, pra mim o codigo ta legal, agora me diz uma coisa, vc acha ele lento? ou algum problema?
GOSTEI 0
Guilherme Caique

Guilherme Caique

21/02/2014

Bem, o código até que roda bem rápido. Mas como novato ainda em Java, queria saber o que vocês mudariam para ficar no gosto de cada um, pois ainda tenho dificuldades em fazer bom uso de todos os recursos que orientação a objetos nos oferece e vendo outras opiniões talvez possa aplicar esses recursos com mais precisão para manter a performance do software sempre superior.
GOSTEI 0
Eduardo Pessoa

Eduardo Pessoa

21/02/2014

bom, leia estes artigo, acho que é meio antigo, mas é bom para se ter uma base:

[url]https://www.devmedia.com.br/criando-e-gravando-dados-em-txt-com-java/23060[/url]
GOSTEI 0
POSTAR