R
Is my second method correct?It's hard to answer your question. She speaks in a second method, but she doesn't even know what she's talking about. Looking at your question the second method is the addPerson() , but the answer accepts speaks of searchPerson() which is the third method.In fact, you don't even know if everyone is in the same class, it's just likely.As it has been said has problems not to use a list and to use a array It should make a more sophisticated mechanism, but let's go this way. It probably shouldn't be a list for the little one saw in the question, another tree-based structure would probably be better. Any exercise you teach to do wrong is a bad exercise.What you call https://pt.stackoverflow.com/q/269089/101 .The second method is correct, but probably should not be done so, or at least should not be the only option. It is usually more appropriate to create the person out of it and pass the created object and not parameters for the database to create. It is linking the detail of the person's creation with the database, thus makes the code unreliable and may have problems in the future.If you are talking about the third method, it seems to be correct despite poorly formatted. At least that's what it looks like, you don't have to assert this without knowing exactly what it should do, without knowing all the requirements, which does not form put in the question.How can I initialize the first and second method? Am I gonna need Scanner?There is no such thing as starting method, it's a term that you invented and only you know what this means.Only you can tell if you need one Scanner. Developing software is not following cake recipes, it is understanding the problem and putting the best solution for it. The question does not quite define what the problem is, so it is complicated to say what is right to do. Any answer you try to say that is wrong by definition, it makes assumptions to give an answer.It is more complicated to respond when we actually do not know the exact structure of the class, It would be much better to have put the whole class. When you want to know if the organization of the code is right, it complicated to respond without seeing all of it.Regardless of the answer, could you give me details of how to do this?This is considered very broad so that we can answer, in fact the most correct is to close the question because of it.What I can say is that it has several conceptual errors in this code, but I don't even know what they give to fix because it has artificial requirements, one more reason not to give to respond properly. Whatever happens will learn wrong with this exercise.Beyond the class Person that I can't talk much (and just because it wasn't posted I don't show my code working), you should have your current class, I'll play it completely here because the answer accepted made it stop being a useful class and mix things. besides having basic coding errors:class Program {
public static void main(String[] args) {
PersonDatabase db = new PersonDatabase(3);
//se quiser aqui colcoa um Scanner para pegar os dados e usá-los no lugar de literais
db.addPerson("Jorge", "X", 20);
db.addPerson("João", "Y", 20);
db.addPerson("Maria", "Z", 20);
Person pessoa = searchPerson("João", "Y");
if (pessoa == null) return;
System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
pessoa = searchPerson("Jorge", "X");
if (pessoa == null) return;
System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
}
}
class PersonDatabase {
private static Person[] persons;
private static int personCount;
public PersonDatabase(int defaultPersonsCapacity) {
persons = new Person[defaultPersonsCapacity];
personCount = 0;
}
public void addPerson(String name, String surname, int age) {
if (personCount == persons.length) {
System.out.println("Unable to add Person.");
return;
}
persons[personCount] = new Person(name, surname, age);
personCount++;
System.out.println("Person added.");
}
public int getPersonCount() {
return personCount;
}
public Person searchPerson(String name, String surname) {
for (Person p : persons) {
if (p.getName().equals(name) && p.getSurname().equals(surname))
return p;
}
return null;
}
}
https://github.com/maniero/SOpt/blob/master/Java/Class/PersonDatabase.java .I know, you didn't like the answer, but the question is not good either. I only answered because the current answer accepts is also not suitable, and between staying alone it giving the wrong impression and another answer that does not say much but indicates the correct way, I thought it was better to answer. I know that most will not like this answer because today people want cake recipes and do not learn to develop software in the correct way, but it is correct to do since the question was not closed before.