|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Objects; |
| 5 | + |
| 6 | +// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, |
| 7 | +// then press Enter. You can now see whitespace characters in your code. |
| 8 | +public class Main { |
| 9 | + public static void main(String[] args) { |
| 10 | + SocialNetwork socialnetwork = new SocialNetwork(); |
| 11 | + |
| 12 | + //- Registrar un usuario por defecto con nombre e identificador único. |
| 13 | + socialnetwork.registerUser("u123", "alexander guerrero"); |
| 14 | + socialnetwork.registerUser("u456", "sandra estacio"); |
| 15 | + socialnetwork.registerUser("u467", "carolina maigual"); |
| 16 | + socialnetwork.registerUser("u897", "mileidy cruz"); |
| 17 | + socialnetwork.registerUser("u230", "daniel botina"); |
| 18 | + socialnetwork.registerUser("u239", "diego botina"); |
| 19 | + socialnetwork.registerUser("u348", "gloria maigual"); |
| 20 | + socialnetwork.registerUser("u438", "teresa maigual"); |
| 21 | + socialnetwork.registerUser("u489", "alejandra rojas"); |
| 22 | + |
| 23 | + //- Seleccionar un usuario |
| 24 | + User u1 = socialnetwork.getUser("u489"); |
| 25 | + //- Un usuario puede seguir/dejar de seguir a otro. |
| 26 | + u1.followUser("u348"); |
| 27 | + u1.followUser("u467"); |
| 28 | + u1.followUser("u897"); |
| 29 | + |
| 30 | + //- Mostrar usuarios |
| 31 | + for (User user : socialnetwork.getUsers()) { |
| 32 | + System.out.println(user.toString()); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class SocialNetwork{ |
| 38 | + private static HashSet<User> users; |
| 39 | + public SocialNetwork() { |
| 40 | + users = new HashSet<>(); |
| 41 | + } |
| 42 | + public void registerUser(String id, String name){ |
| 43 | + users.add(new User(id, name)); |
| 44 | + } |
| 45 | + public User getUser(String id){ |
| 46 | + for(User user: users){ |
| 47 | + if(user.getId().equals(id)){ |
| 48 | + return user; |
| 49 | + } |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | + public void unfollowUser(){} |
| 54 | + public HashSet<User> getUsers() { |
| 55 | + return users; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class User{ |
| 60 | + private String id; |
| 61 | + private String name; |
| 62 | + private HashSet<String> followers; |
| 63 | + private HashSet<String> following; |
| 64 | + public void followUser(String followIdUser){ |
| 65 | + followers.add(followIdUser); |
| 66 | + } |
| 67 | + |
| 68 | + public User(String id, String name) { |
| 69 | + this.id = id; |
| 70 | + this.name = name; |
| 71 | + this.followers = new HashSet<String>(); |
| 72 | + this.following = new HashSet<String>(); |
| 73 | + } |
| 74 | + |
| 75 | + public String getId() { |
| 76 | + return id; |
| 77 | + } |
| 78 | + |
| 79 | + public String getName() { |
| 80 | + return name; |
| 81 | + } |
| 82 | + |
| 83 | + public HashSet<String> getFollowers() { |
| 84 | + return followers; |
| 85 | + } |
| 86 | + |
| 87 | + public HashSet<String> getFollowing() { |
| 88 | + return following; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String toString() { |
| 93 | + return "User{" + |
| 94 | + "id='" + id + '\'' + |
| 95 | + ", name='" + name + '\'' + |
| 96 | + ", followers=" + followers + |
| 97 | + ", following=" + following + |
| 98 | + '}'; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean equals(Object o) { |
| 103 | + if (this == o) return true; |
| 104 | + if (o == null || getClass() != o.getClass()) return false; |
| 105 | + User user = (User) o; |
| 106 | + return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(followers, user.followers) && Objects.equals(following, user.following); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int hashCode() { |
| 111 | + return Objects.hash(id, name, followers, following); |
| 112 | + } |
| 113 | +} |
0 commit comments