01: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
02: // Read license.txt in this directory.
03:
04: package eg.music;
05:
06: import java.io.*;
07: import java.util.*;
08:
09: public class MusicLibrary {
10: static Music looking = null;
11: static Music library[] = {};
12:
13: static void load(String name) throws Exception {
14: List music = new ArrayList();
15: BufferedReader in = new BufferedReader(new FileReader(name));
16: in.readLine(); // skip column headings
17: while (in.ready()) {
18: music.add(Music.parse(in.readLine()));
19: }
20: in.close();
21: library = (Music[]) music.toArray(library);
22: }
23:
24: static void select(Music m) {
25: looking = m;
26: }
27:
28: static void search(double seconds) {
29: Music.status = "searching";
30: Simulator.nextSearchComplete = Simulator.schedule(seconds);
31: }
32:
33: static void searchComplete() {
34: Music.status = MusicPlayer.playing == null ? "ready"
35: : "playing";
36: }
37:
38: static void findAll() {
39: search(3.2);
40: for (int i = 0; i < library.length; i++) {
41: library[i].selected = true;
42: }
43: }
44:
45: static void findArtist(String a) {
46: search(2.3);
47: for (int i = 0; i < library.length; i++) {
48: library[i].selected = library[i].artist.equals(a);
49: }
50: }
51:
52: static void findAlbum(String a) {
53: search(1.1);
54: for (int i = 0; i < library.length; i++) {
55: library[i].selected = library[i].album.equals(a);
56: }
57: }
58:
59: static void findGenre(String a) {
60: search(0.2);
61: for (int i = 0; i < library.length; i++) {
62: library[i].selected = library[i].genre.equals(a);
63: }
64: }
65:
66: static void findYear(int a) {
67: search(0.8);
68: for (int i = 0; i < library.length; i++) {
69: library[i].selected = library[i].year == a;
70: }
71: }
72:
73: static int displayCount() {
74: int count = 0;
75: for (int i = 0; i < library.length; i++) {
76: count += (library[i].selected ? 1 : 0);
77: }
78: return count;
79: }
80:
81: static Music[] displayContents() {
82: Music displayed[] = new Music[displayCount()];
83: for (int i = 0, j = 0; i < library.length; i++) {
84: if (library[i].selected) {
85: displayed[j++] = library[i];
86: }
87: }
88: return displayed;
89: }
90:
91: }
|