Is my second method correct?
It's hard to answer your question. She speaks in the second method, but she can not even tell what she is talking about. Looking at your question the second method is addPerson()
, but the accepted answer speaks of searchPerson()
which is the third method.
In fact, it's not even known if everyone is in the same class, it's just likely.
As mentioned, you have problems with not using a list and to use an array should make a more sophisticated mechanism, but let's do it this way. It probably should not even be a list because of the little that was seen in the question, another tree-based structure would probably be better. Any exercise that teaches you to do wrong is a bad exercise.
What you call the attribute is actually called the field .
The second method is correct, but it should probably not be done that way, or at least it should not be the only option. Generally it is more appropriate to create the person outside of it and pass the created object and not parameters to the create database. It is linking the detail of the creation of the person with the database, in this way it makes the code very flexible and may have problems in the future.
If you're talking about the third method, it seems to be correct though poorly formatted. At least that's what it looks like, you do not have to claim this without knowing exactly what it should do, not knowing all the requirements, that it does not form put into question.
How can I initialize the first and second methods? Will I need the "Scanner"?
There's no such thing as initializing method, this is a term you invented and only you know what that means.
Only you can tell if you need a Scanner
. Developing software is not following cake recipes, it is understanding the problem and putting the best solution into it. The question does not define well what the problem is, so it is difficult to say what is right to do. Any answer that tries to say this is wrong by definition, it makes assumptions to give an answer.
It is more complicated to answer when we do not actually 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 code organization is right, it's difficult to answer without seeing all of it.
Regardless of the answer, could you give me details on how to do this?
This is considered too broad for us to answer, in fact the most correct is to close the question because of this.
What I can say is that there are several conceptual errors in this code, but I do not even know which ones to fix because it has artificial requirements, one more reason not to give to respond properly. Whatever happens will learn wrong with this exercise.
In addition to the Person
class I can not talk too much (and just because it was not posted I do not show my code working), you should have your current class, I'll reproduce it completely here because the accepted answer made it leave to be a useful class and mix things up. 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;
}
}
I placed GitHub for future reference .
I know, you did not like the answer, but the question is not good either. I only answered because the current answer accepted is also not adequate, and between being alone giving the wrong impression and another answer that does not say much but indicates the correct path, I thought it was better to respond. I know most will not like this answer because today people want cake recipes and not learn to develop software in the right way, but it's the right thing to do since the question has not been closed before.