I have a class named Utilizador created with the following attributes:
private String nome;
private String email;
private String cidadeAtual;
private List<String> checkins = new ArrayList<String>();
private List<String> amigos = new ArrayList<String>();
In my main class I have a list of Utilizadores with the followinf format:
Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: []
Nickname: Maria | Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: []
What I'm trying to do is, type one name and then the other and add a friendship. For example:
I type Daniel, press enter. I type Maria, press enter. The list amigos must now have each others name like this:
Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: [Maria]
Nickname: Maria | Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: [Daniel]
But the method I developed puts both names inside the list Amigos like this:
Nickname: Daniel | Email: dd | Cidade atual: Maia | Checkins: [Maia] | Amigos: [Maria, Daniel]
Nickname: Maria| Email: dd2 | Cidade atual: Porto | Checkins: [Porto] | Amigos: [Maria, Daniel]
Here is the method:
public static void adicionarAmizade() {
Scanner cu = new Scanner(System.in);
System.out.println("Introduza o nome dos utilizadores!");
System.out.println("\n1º:");
String amigo1 = cu.next();
System.out.println("\n2º:");
String amigo2 = cu.next();
for (Utilizador u : listaUtilizadores) {
if ((u.getNome()).equals(amigo1)) {
u.getAmigos().add(amigo2);
System.out.println("Adicionou");
System.out.println(u.toString());
break;
}
}
for (Utilizador u : listaUtilizadores) {
if ((u.getNome()).equals(amigo2)) {
u.getAmigos().add(amigo1);
System.out.println("Adicionou");
System.out.println(u.toString());
break;
}
}
}
Something like this is a start.
HashMap<String, String> Amigos = new HashMap<>();
//Create Friendship between Marian and Daniel.
Amigos.put("Maria", "Daniel");
//Get Maria's Friend.
System.out.println(Amigos.get("Maria"))
You can also take an object approach.
Person Object
import java.util.ArrayList;
public class Person {
String name;
ArrayList<String> friends = new ArrayList();
Person(String name)
{
this.name = name;
}
String getName()
{
return this.name;
}
void addFriend(String friendName)
{
friends.add(friendName);
}
boolean isFriend(String friendName)
{
for(String friend : this.friends)
{
if(friend.equals(friendName))
{
return true;
}
}
return false;
}
String getFriendInPosition(int position)
{
return this.friends.get(position);
}
int getFriendPosition(String friendName)
{
int i = 0;
for(String friend : this.friends)
{
if(friend.equals(friendName))
{
return i;
}
i++;
}
return -1;
}
ArrayList<String> getAllFriends()
{
return this.friends;
}
}
Main Class
public static void main(String[] args) {
Person maria = new Person("Maria");
Person daniel = new Person("Daniel");
maria.addFriend(daniel.getName());
daniel.addFriend("Maria");//You could create this to add Person objects as friends
maria.addFriend("me");//You could create this to add Person objects as friends
maria.addFriend("you");//You could create this to add Person objects as friends
System.out.println(maria.getFriendInPosition(0));
System.out.print("Are Maria and Daniel friends? ");
System.out.println(maria.isFriend(daniel.getName()));
for(String friend : maria.getAllFriends())
{
System.out.println("Maria's friend: " + friend);
}
for(String friend : daniel.getAllFriends())
{
System.out.println("Daniel's friend: " + friend);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.