-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disk.java
103 lines (87 loc) · 2.51 KB
/
Disk.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package labs.lab5;
import java.util.ArrayList;
/**
* Created by User on 07.05.2017.
*/
/**
* Диск містить інформацію про записані на ньому альбоми
*/
public class Disk {
private ArrayList<Album> albums;
Disk() {
albums = new ArrayList<>();
}
/**
* Записати альбом на диск
*/
public void addAlbum(Album album) {
albums.add(album);
}
/**
* Підраховує загальну тривалість всіх альбомів диску
*/
public int getTotalDuration() {
int duration = 0;
for (Album a :
albums) {
duration += a.getDuration();
}
return duration;
}
public void showTotalDuration() {
int duration = getTotalDuration();
System.out.println("Total duration: " + duration / 3600 + ':' + duration % 3600 / 60 + ':' + duration % 60);
}
private void sort(Album[] a) {
Album t;
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i].getGenre().compareTo(a[j].getGenre()) > 0) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
/**
* Сортує альбоми за жанрами
*/
public void sortByGenre() {
Album[] arr = new Album[albums.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = albums.get(i);
}
albums.clear();
sort(arr);
for (int i = 0; i < arr.length; i++) {
albums.add(arr[i]);
}
}
/**
* Повертає всі треки, що підходять під заданий діапазон тривалості(в секундах)
*/
public ArrayList<Track> searchByDuration(int from, int to) {
ArrayList<Track> list = new ArrayList<>();
for (Album a :
albums) {
for (Track t :
a.searchByDuration(from, to)) {
list.add(t);
}
}
return list;
}
public void print() {
for (Album a :
albums) {
a.print();
}
}
public void print(ArrayList<Track> tracks) {
for (Track t :
tracks) {
t.print();
}
}
}