Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java and I'm trying to create a social network. I have a array of objects "People" which their names, e-mails and so on. I would like to create relations between them (like 'friends') so that program would connect two object.
I though about doing another array inside of every "Person" object, so each person would have a name, e-mail and friend array with all friends inside of it. I don't know how to refer to an other object of type People inside of object of this type.
I don't know if it's clear..
Hope it is!
Thank You in advance!
Here's one way:
public class Person {
private String name;
private String email;
private List<Person> friends;
// Leave the rest for you.
public void addFriend(Person p) {
if (p != null) {
this.friends.add(p);
}
}
public void removeFriend(Person p) {
this.friends.remove(p);
}
public boolean isFriend(Person p) {
return this.friends.contains(p);
}
}
You'll want constructors and a way to add and remove Person from your friend List.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Can I use a utility class like this?
public final class ProfessorDirectory {
private static Map<String, Professor> directory = new HashMap<>();
private ProfessorDirectory() {
throw new IllegalStateException("Utility Class");
}
static void addProfessorsFromDescription(String description) {
String regex = "(?<=\\n).* .*(?=\\n)";
Matcher m = Pattern.compile(regex).matcher(description);
while (m.find()) {
String professorName = Professor.formater(m.group(0));
directory.put(professorName, new Professor(professorName));
}
}
public static Professor get(String firstName, String lastName) {
return directory.get(Professor.formater(firstName, lastName));
}
}
I used this to create a library in which you can retrieve a teacher's planning.
Exemple of utilisation:
Planning professorPlanning = schedules.getPlanningOf(ProfessorDirectory.get("Jack", "Sticky"));
The ProfessorDirectory is initialized internally, and should never be initialized by the user.
There are a few disadvantages to this approach, that is, the approach of having static data and methods.
You can never have more than one ProfessorDirectory, even if you find it would be useful to have several.
It is difficult to test clients of ProfessorDirectory, because they must use the real code instead of being able to mock an interface.
You rule out using some useful patterns. For instance, you can't write a caching proxy that implements a ProfessorDirectory interface and wraps an arbitrary implementation of that interface.
Overall this is a bad approach.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello so I'm having a bit of difficulties with a setter method on objects.
public class Company {
private String companyName;
public static int numberOfEmployees;
public Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
Employees employee1 = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
employees[numberOfEmployees] = employee1;
numberOfEmployees++;
}
So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.
But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.
So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].
Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.
I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:
public class Company {
private String companyName;
public static int numberOfEmployees;
public static Employees employees[];
public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
numberOfEmployees++;
employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I wanted to know how to have an enum in java, containing values that could adjust depending on the condition.
Exemple :
Public enum values{
If (condition){
A("a", 1),
B("b",2);
} else {
C("c", 1);
}
Private string value;
Private int id;
values(string value, int id) {
this.value = value;
this.id = id;
}
}
Thank you for your help
That's not the purpose of Enum. From java docs
An enum type is a special data type that enables for a variable to be
a set of predefined constants. The variable must be equal to one of
the values that have been predefined for it.
You can add the logic in method,
if (condition)
value = values.A;
else
value = values.C;
You can also use filter on enum,
Arrays.stream(values.values())
.filter(condition)
.collect(toList());
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a List<Person>. Person's attributes are String name, String secondName, int phoneNum.
How can I sort this List by the String property secondName?
I've tried moving into another List<String> the data from the previous list moving secondName to the first position, then applying Collection.sort and creating other List<Person> with the data of the List sorted but its too complicated.
Java 8 introduced a sort method in the List interface which can use a comparator.
If you have a getter for secondName you can do this:
myList.sort(Comparator.comparing(Person::getSecondName));
If you don't have a getter use this one:
myList.sort(Comparator.comparing((person)->(person.secondName)));
for more ways and answers:
link
Lets assume here is your Pojo Class Person.
class Person{
String name;
String secondName;
//Getter and setters
}
Then sort the list by java collection API itself.
java.util.Collections.sort(personList, new Comparator<Person>() {
#Override
public int compare(Person o1, Person o2) {
return o1.getSecondName().compareToIgnoreCase(o2.getSecondName());
}
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm kind of off on nodes, what are their uses in java and what does it mean to "create a dog node and sets its data field to breed"?
Your question is a little unclear as you haven't provided any context, so I'm going to assume this is an object oriented programming question. Suppose you have a Node class with a data field and some accessor methods that looks something like this:
public class Node {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Here is what creating a "dog node" and setting its "data field to breed" means:
Node dog = new Node();
dog.setData("breed");