(求助)The User class
(2) The User ClassImplement a class called User that represents a user of the Music Exchange Center
that logs in to download music. The class must have the following attributes:
• userName - a String indicating the user's name when on the system.
• online - a boolean indicating whether or not the user is on-line at the moment.
(An on-line user is able to download music and this user’s music is also made available to
other users who are logged on.)
• songList - an ArrayList<Song> containing all of the songs that this user has on his/her hard drive
to be made available to the Music Exchange Center community.
Create the following methods:
• A constructor that takes no parameters, and another constructor that takes the user name as a
parameter. When created, users are considered NOT to be online.
• A toString() method that shows the user's name, the number of files available in his/her song list
and whether or not he/she is currently on-line (e.g., BoppinBob: 14 songs (not online)).
• A method called addSong() which adds a given song to the user’s songList.
• A method called totalSongTime() that returns an integer indicating the total amount of time (in
seconds) that all of the user’s songs would require if played.
Test your code before continuing using this test program:
class UserTestProgram {
public static void main(String args[]) {
User discoStew, noNameFred, peterPunk;
// Create some users and give them some songs
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));
System.out.println(discoStew); // should display Disco Stew: 3 songs (not online)
System.out.println(discoStew.totalSongTime()); // should display 808
noNameFred = new User();
System.out.println(noNameFred); // should display : 0 songs (not online)
System.out.println(noNameFred.totalSongTime()); // should display 0
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));
System.out.println(peterPunk); // should display Peter Punk: 5 songs (not online)
System.out.println(peterPunk.totalSongTime()); // should display 1269
peterPunk.online = true;
System.out.println(peterPunk); // should display Peter Punk: 5 songs (online)
}
}