R
You did not give the code of methods AutentificaNome and AutentificaId, but I will assume that they are analogous to the AutentificaSenha.So I can reconstruct your full class more or less like this:import java.util.Scanner;
public class TestaSenha {
private Scanner entrada;
private String senha;
private String nome;
private int id;
private String aSenha;
private String aNome;
private int aId;
public TestaSenha(Scanner entrada, String senha, String nome, int id) {
this.entrada = entrada;
this.senha = senha;
this.nome = nome;
this.id = id;
}
private boolean AutentificaNome(String s) {
this.aNome = s;
boolean cond = this.nome == this.aNome;
if(cond) {
return true;
} else {
return false; }
}
private boolean AutentificaSenha(String s) {
this.aSenha = s;
boolean cond = this.senha == this.aSenha;
if(cond) {
return true;
} else {
return false; }
}
private boolean AutentificaId(int s) {
this.aId = s;
boolean cond = this.id == this.aId;
if(cond) {
return true;
} else {
return false; }
}
public boolean Autentifica() {
System.out.println("Seu nome: ");
this.aNome = entrada.nextLine();
System.out.println("Sua senha: ");
this.aSenha = entrada.nextLine();
System.out.println("Seu ID: ");
this.aId = entrada.nextInt();
System.out.println("Aguarde");
boolean cond =
this.AutentificaNome(this.aNome) &&
this.AutentificaSenha(this.senha) &&
this.AutentificaId(this.id);
if (cond) {
System.out.println("Logado com sucesso");
return true;
} else{
System.out.println("Falha ao efetuar loguin");
return false;
}
}
}
Your most serious mistake is to compare Strings using the operator == will not do what you want. You should use the method equals(). The reason is that == compares if two variables point to the same object, which will go wrong when you have two Strings that although they have the same content, are two different objects. https://pt.stackoverflow.com/q/3905/132 By doing str1.equals(str2), true will be returned if both Stringhave the same content, and false otherwise, except if str1 for null, which will result in a NullPointerException. In this case, we can use the following:Objects.equals(str1, str2);
And don't forget to add the import necessary:import java.util.Objects;
So here's your code:private boolean AutentificaSenha(String s) {
this.aSenha = s;
boolean cond = Objects.equals(this.senha, this.aSenha);
if(cond) {
return true;
} else {
return false; }
}
And the same goes for AutentificaNomebut not for the AutentificaId (i) ints are not objects, so can compare with == no problem).We can improve your code a little more, after all if cond is true, will be returned true, and if cond for false, will be returned false. How about then return cond directly then?private boolean AutentificaSenha(String s) {
this.aSenha = s;
boolean cond = Objects.equals(this.senha, this.aSenha);
return cond;
}
We are returning cond, whose value is always the result of Objects.equals(this.senha, this.aSenha). So, we can simplify even more returning directly the result of Objects.equals(this.senha, this.aSenha):private boolean AutentificaSenha(String s) {
this.aSenha = s;
return Objects.equals(this.senha, this.aSenha);
}
And the rules of Java nomenclatures dictate that method names should start with tiny letters. Thus, we will put all your four methods with names starting with tiny letters.We can do something similar at your end of your main method as well:public boolean autentifica() {
System.out.println("Seu nome: ");
this.aNome = entrada.nextLine();
System.out.println("Sua senha: ");
this.aSenha = entrada.nextLine();
System.out.println("Seu ID: ");
this.aId = entrada.nextInt();
System.out.println("Aguarde");
boolean cond =
this.autentificaNome(this.aNome) &&
this.autentificaSenha(this.senha) &&
this.autentificaId(this.id);
if (cond) {
System.out.println("Logado com sucesso");
} else {
System.out.println("Falha ao efetuar login");
}
return cond;
}
Since we're looking at your main method, I've already taken advantage of and got the "loguin" spelling for "login" and also gave a better identification in the definition of the variable cond. In fact, we will analyze this variable better: boolean cond =
this.autentificaNome(this.aNome) &&
this.autentificaSenha(this.senha) &&
this.autentificaId(this.id);
You read the variables aNome, aSenha and aId of the user, but passes to the methods the variables aNome, senha and id. Notice the inconsistency between the variables used.Method autentificaNome, the value of aNome will be assigned to own aNome (which does nothing) and then compared with nome.Method autentificaSenha, the value of senha will be assigned to aSenha, ignoring what the user entered and making the two passwords always equal.With the method autentificaId, something similar to that autentificaSenha.Now we come to an important point, the aNome, aSenha and aId are important to remain recorded after the login test is performed? Supposing not, then the ideal would be you to eliminate them and use only local variables to the method autentifica(). There is also no need to change the value of the fields of the object when you are just testing whether the entered password is correct or not, after all an attempt to login, whether successful or not, should not change the login and password or something.In this way, this is your code now:import java.util.Scanner;
import java.util.Objects;
public class TestaSenha {
private Scanner entrada;
private String senha;
private String nome;
private int id;
public TestaSenha(Scanner entrada, String senha, String nome, int id) {
this.entrada = entrada;
this.senha = senha;
this.nome = nome;
this.id = id;
}
private boolean autentificaNome(String s) {
return Objects.equals(this.nome, s);
}
private boolean autentificaSenha(String s) {
return Objects.equals(this.senha, s);
}
private boolean autentificaId(int s) {
return this.id == s;
}
public boolean autentifica() {
System.out.println("Seu nome: ");
String aNome = entrada.nextLine();
System.out.println("Sua senha: ");
String aSenha = entrada.nextLine();
System.out.println("Seu ID: ");
int aId = entrada.nextInt();
System.out.println("Aguarde");
boolean cond =
this.autentificaNome(aNome) &&
this.autentificaSenha(aSenha) &&
this.autentificaId(aId);
if (cond) {
System.out.println("Logado com sucesso");
} else {
System.out.println("Falha ao efetuar login");
}
return cond;
}
}
Your class should already be working properly now. The only detail is that it gets easier by encapsulating the verification of the three conditions together in a proper method, and therefore your code is like this:import java.util.Scanner;
import java.util.Objects;
public class TestaSenha {
private Scanner entrada;
private String senha;
private String nome;
private int id;
public TestaSenha(Scanner entrada, String senha, String nome, int id) {
this.entrada = entrada;
this.senha = senha;
this.nome = nome;
this.id = id;
}
private boolean autentificaNome(String s) {
return Objects.equals(this.nome, s);
}
private boolean autentificaSenha(String s) {
return Objects.equals(this.senha, s);
}
private boolean autentificaId(int s) {
return this.id == s;
}
private boolean autentifica(String n, String s, int i) {
return this.autentificaNome(n) &&
this.autentificaSenha(s) &&
this.autentificaId(i);
}
public boolean autentifica() {
System.out.println("Seu nome: ");
String aNome = entrada.nextLine();
System.out.println("Sua senha: ");
String aSenha = entrada.nextLine();
System.out.println("Seu ID: ");
int aId = entrada.nextInt();
System.out.println("Aguarde");
boolean cond = this.autentifica(aNome, aSenha, aId);
if (cond) {
System.out.println("Logado com sucesso");
} else {
System.out.println("Falha ao efetuar login");
}
return cond;
}
}