-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
166 lines (139 loc) · 5.07 KB
/
User.java
File metadata and controls
166 lines (139 loc) · 5.07 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.ArrayList;
public class User {
private String userName;
private boolean online;
// list of songs
private ArrayList<Song> songList;
public User() {
this("");
}
public User(String u) {
userName = u;
online = false;
// create empty list
songList = new ArrayList<>();
}
public String getUserName() {
return userName;
}
public boolean isOnline() {
return online;
}
public String toString() {
String s = "" + userName + ": " + songList.size() + " songs (";
if (!online)
s += "not ";
return s + "online)";
}
// add a song and set owner
public void addSong(Song s) {
songList.add(s);
s.setOwner(this);
}
// total song time
public int totalSongTime() {
int total = 0;
for (Song song : songList) {
total += song.getDuration();
}
return total;
}
public ArrayList<Song> getSongList() {
return songList;
}
public void register(MusicExchangeCenter m) {
m.registerUser(this);
}
// log on if not already logged and registered
public void logon(MusicExchangeCenter m) {
if (!this.online) {
User user = m.userWithName(userName);
if (user != null)
user.online = true;
}
}
// log on if logged in and registered
public void logoff(MusicExchangeCenter m) {
if (this.online) {
User user = m.userWithName(userName);
if (user != null)
user.online = false;
}
}
// This method should gather the list of all available songs from all users
// that are online at the given music exchange center
public ArrayList<String> requestCompleteSonglist(MusicExchangeCenter m) {
ArrayList<Song> allAvailableSongs = m.allAvailableSongs();
return songsToStrings(allAvailableSongs);
}
// This method should gather the list of all available songs by the given
// artist from all users that are online at the given music exchange cente
public ArrayList<String> requestSonglistByArtist(MusicExchangeCenter m, String artist) {
ArrayList<Song> allAvailableSongs = m.availableSongsByArtist(artist);
return songsToStrings(allAvailableSongs);
}
// helper method to convert songs to strings
private ArrayList<String> songsToStrings(ArrayList<Song> list) {
ArrayList<String> completeSongList = new ArrayList<>();
completeSongList.add(String.format("%4s %-30s %-20s %-10s %s", "", "TITLE", "ARTIST", "TIME", "OWNER"));
int i = 1;
for (Song song : list) {
completeSongList.add(String.format("%3d. %-30s %-20s %-10s %s", i++, song.getTitle(), song.getArtist(),
String.format("%d:%02d", song.getMinutes(), song.getSeconds()), song.getOwner().getUserName()));
}
return completeSongList;
}
// returns song with given title from list
public Song songWithTitle(String title) {
for (Song song : songList) {
if (song.getTitle().equals(title))
return song;
}
return null;
}
// simulates the downloading of one of the songs in the catalog
public void downloadSong(MusicExchangeCenter m, String title, String ownerName) {
Song song = m.getSong(title, ownerName);
if (song != null)
songList.add(song);
}
// Various Users for test purposes
public static User DiscoStew() {
User discoStew = new User("Disco Stew");
discoStew.addSong(new Song("Hey Jude", "The Beatles", 4, 35));
discoStew.addSong(new Song("Barbie Girl", "Aqua", 3, 54));
discoStew.addSong(new Song("Only You Can Rock Me", "UFO", 4, 59));
discoStew.addSong(new Song("Paper Soup Cats", "Jaw", 4, 18));
return discoStew;
}
public static User SleepingSam() {
User sleepingSam = new User("Sleeping Sam");
sleepingSam.addSong(new Song("Meadows", "Sleepfest", 7, 15));
sleepingSam.addSong(new Song("Calm is Good", "Waterfall", 6, 22));
return sleepingSam;
}
public static User RonnieRocker() {
User ronnieRocker = new User("Ronnie Rocker");
ronnieRocker.addSong(new Song("Rock is Cool", "Yeah", 4, 17));
ronnieRocker.addSong(new Song("My Girl is Mean to Me", "Can't Stand Up", 3, 29));
ronnieRocker.addSong(new Song("Only You Can Rock Me", "UFO", 4, 52));
ronnieRocker.addSong(new Song("We're Not Gonna Take It", "Twisted Sister", 3, 9));
return ronnieRocker;
}
public static User CountryCandy() {
User countryCandy = new User("Country Candy");
countryCandy.addSong(new Song("If I Had a Hammer", "Long Road", 4, 15));
countryCandy.addSong(new Song("My Man is a 4x4 Driver", "Ms. Lonely", 3, 7));
countryCandy.addSong(new Song("This Song is for Johnny", "Lone Wolf", 4, 22));
return countryCandy;
}
public static User PeterPunk() {
User peterPunk = new User("Peter Punk");
peterPunk.addSong(new Song("Bite My Arms Off", "Jaw", 4, 12));
peterPunk.addSong(new Song("Where's My Sweater", "The Knitters", 3, 41));
peterPunk.addSong(new Song("Is that My Toenail ?", "Clip", 4, 47));
peterPunk.addSong(new Song("Anvil Headache", "Clip", 4, 34));
peterPunk.addSong(new Song("My Hair is on Fire", "Jaw", 3, 55));
return peterPunk;
}
}