forked from NeilAlishev/SpringCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicPlayer.java
More file actions
36 lines (29 loc) · 1.03 KB
/
MusicPlayer.java
File metadata and controls
36 lines (29 loc) · 1.03 KB
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
package ru.alishev.springcourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author Neil Alishev
*/
@Component
public class MusicPlayer {
private ClassicalMusic classicalMusic;
private RockMusic rockMusic;
@Autowired
public MusicPlayer(ClassicalMusic classicalMusic, RockMusic rockMusic) {
this.classicalMusic = classicalMusic;
this.rockMusic = rockMusic;
}
public void playMusic(MusicGenre genre) {
Random random = new Random();
// случайное целое число между 0 и 2
int randomNumber = random.nextInt(3);
if (genre == MusicGenre.CLASSICAL) {
// случайная классическая песня
System.out.println(classicalMusic.getSongs().get(randomNumber));
} else {
// случайная рок песня
System.out.println(rockMusic.getSongs().get(randomNumber));
}
}
}