Bonjour,
Je suis débutant et j'ai un exercice à réaliser mais je ne comprends pas comment m'y prendre pour le réaliser et j'aurais besoin d'aide pour m'aiguiller.
Voici les différents codes sur lesquels je dois travailler :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package com.openclassrooms.magicgithub.api; import com.openclassrooms.magicgithub.model.User; import java.util.List; import static com.openclassrooms.magicgithub.api.FakeApiServiceGenerator.generateUsers; public class FakeApiService implements ApiService { private final List<User> users = generateUsers(); /** * Return a list of {@link User} * Those users must be generated by {@link FakeApiServiceGenerator} */ @Override public List<User> getUsers() { // TODO: A modifier return null; } /** * Generate a random {@link User} and add it {@link FakeApiService#users} list. * This user must be get from the {@link FakeApiServiceGenerator#FAKE_USERS_RANDOM} list. */ @Override public void generateRandomUser() { // TODO: A modifier } /** * Delete a {@link User} from the {@link FakeApiService#users} list. */ @Override public void deleteUser(User user) { // TODO: A modifier } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.openclassrooms.magicgithub.repository; import com.openclassrooms.magicgithub.api.ApiService; import com.openclassrooms.magicgithub.model.User; import java.util.List; public class UserRepository { private final ApiService apiService; // TODO: A utiliser private Object User; public UserRepository(ApiService apiService) { this.apiService = apiService; } public List<User> getUsers() { // TODO: A modifier return null; } public void generateRandomUser() { // TODO: A modifier } public void deleteUser(User user) { // TODO: A modifier } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package com.openclassrooms.magicgithub.model; import java.util.Objects; import java.util.Random; import androidx.annotation.Nullable; import static com.openclassrooms.magicgithub.api.FakeApiServiceGenerator.FAKE_USERS_RANDOM; public class User { private final String id; private final String login; private final String avatarUrl; public User(String id, String login, String avatarUrl) { this.id = id; this.login = login; this.avatarUrl = avatarUrl; } // --- GETTERS --- public String getId() { return id; } public String getLogin() { return login; } public String getAvatarUrl() { return avatarUrl; } /** * Generate random user */ public static User random(){ return FAKE_USERS_RANDOM.get(new Random().nextInt(FAKE_USERS_RANDOM.size())); } @Override public boolean equals(@Nullable Object obj) { if (obj == null) return false; if (obj == this) return true; if (!(obj instanceof User)) return false; return (((User) obj).avatarUrl == this.avatarUrl && ((User) obj).login == this.login); } @Override public int hashCode() { return Objects.hash(login, avatarUrl); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package com.openclassrooms.magicgithub.api; import com.openclassrooms.magicgithub.model.User; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; public abstract class FakeApiServiceGenerator { static List<User> generateUsers() { return new ArrayList<>(FAKE_USERS); } public static List<User> FAKE_USERS = Arrays.asList( new User("001", "Jake", "https://api.adorable.io/AVATARS/512/1.png"), new User("002", "Paul", "https://api.adorable.io/AVATARS/512/2.png"), new User("003", "Phil", "https://api.adorable.io/AVATARS/512/3.png"), new User("004", "Guillaume", "https://api.adorable.io/AVATARS/512/4.png"), new User("005", "Francis", "https://api.adorable.io/AVATARS/512/5.png"), new User("006", "George", "https://api.adorable.io/AVATARS/512/6.png"), new User("007", "Louis", "https://api.adorable.io/AVATARS/512/7.png"), new User("008", "Mateo", "https://api.adorable.io/AVATARS/512/8.png"), new User("009", "April", "https://api.adorable.io/AVATARS/512/9.png"), new User("010", "Louise", "https://api.adorable.io/AVATARS/512/10.png"), new User("011", "Elodie", "https://api.adorable.io/AVATARS/512/11.png"), new User("012", "Helene", "https://api.adorable.io/AVATARS/512/12.png"), new User("013", "Fanny", "https://api.adorable.io/AVATARS/512/13.png"), new User("014", "Laura", "https://api.adorable.io/AVATARS/512/14.png"), new User("015", "Gertrude", "https://api.adorable.io/AVATARS/512/15.png"), new User("016", "Chloé", "https://api.adorable.io/AVATARS/512/16.png"), new User("017", "April", "https://api.adorable.io/AVATARS/512/17.png"), new User("018", "Marie", "https://api.adorable.io/AVATARS/512/18.png"), new User("019", "Henri", "https://api.adorable.io/AVATARS/512/19.png"), new User("020", "Rémi", "https://api.adorable.io/AVATARS/512/20.png") ); public static List<User> FAKE_USERS_RANDOM = Arrays.asList( new User("021", "Lea", "https://api.adorable.io/AVATARS/512/21.png"), new User("022", "Geoffrey", "https://api.adorable.io/AVATARS/512/22.png"), new User("023", "Simon", "https://api.adorable.io/AVATARS/512/23.png"), new User("024", "André", "https://api.adorable.io/AVATARS/512/24.png"), new User("025", "Leopold", "https://api.adorable.io/AVATARS/512/25.png") ); }Dans le premier code j'ai 3 fonctions a modifier
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 package com.openclassrooms.magicgithub.api; import com.openclassrooms.magicgithub.model.User; import java.util.List; import androidx.annotation.Nullable; public interface ApiService { List<User> getUsers(); void generateRandomUser(); void deleteUser(User username); }
La première fonction doit retourner la liste d'utilisateur donc je suppose qu'il faut utiliser la variable users en faisant return users
La deuxième fonction dois générer un utilisateur aléatoirement, j'utilise à nouveau la variable users en faisant users.add(User.random()), j'utilise la fonction random() de la classe User pour générer un utilisateur aléatoire et j'utilise la fonction add() pour ajouter l'utilisateur à la liste dans users
Pour la troisième fonction je suppose que ç'est users.remove(user) mais je ne comprends pas le lien entre deleteUser(User user) de la classe FakeApiService avec la classe User et l'interface ApiService où la fonction est deleteUser(User username)
Dans le deuxième code je comprends que je dois utiliser la variable apiService mais je ne comprends pas quoi faire.
Et je ne comprends pas que fais ce bout de code, même si je sais que c'est un constructeur.
Merci d'avance pour votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 public UserRepository(ApiService apiService) { this.apiService = apiService; }
Partager