当前位置: 代码迷 >> Java相关 >> (求助) The MusicExchangeCenter Class
  详细解决方案

(求助) The MusicExchangeCenter Class

热度:461   发布时间:2009-11-03 11:14:12.0
(求助) The MusicExchangeCenter Class
Implement a class called MusicExchangeCenter which represents
the company website that users log in and out of in order to download
music. The class should have one attribute:

• users - an ArrayList<User> of all registered users
(both logged on and not logged on)

Create the following methods:

• A zero-parameter constructor.

• A toString() method that returns a string representation of the
music center showing the number of users currently online as
well as the number of songs currently available.
(e.g., "Music Exchange Center (3 clients on line, 15 songs available)").

• A userWithName(String s) method that finds and returns the user object with the given name if it
is in the list of users. If not there, null should be returned.

• A registerUser(User x) method that adds a given User to the music center’s list of users,
provided that there are no other users with the same username. If there are other users with the
same username, then this method does nothing. Use the userWithName() method above.

Go back to the User class and add these methods:

• A register(MusicExchangeCenter m) that makes us of the registerUser() method that you just
wrote to register the user into the given MusicExchangeCenter m. (Note that this should be a
one-line method).

• A logon(MusicExchangeCenter m) that simulates a user going online with the given music
center. (Assume that users can log onto at most one music center at a time). Make use of the
userWithName() method you wrote earlier. Only allow the user to log on if he/she has already
registered.

• A logoff(MusicExchangeCenter m) that simulates a user going offline from the given music
center. Make use of the userWithName() method you wrote earlier.

Go back to the MusicExchangeCenter and create these methods for listing clients and songs:

• An onlineUsers() method that returns an ArrayList<User> of all users that are currently online.
This method does NOT display anything!!

• An allAvailableSongs() method that returns a new ArrayList<Song> of all songs currently
available for download (i.e., all songs that are available from all logged-on users). Note that this
method creates and returns a new ArrayList each time it is called. This method does NOT
display anything!!

• An availableSongsByArtist(String artist) method that returns a new ArrayList<Song> of all
songs currently available for download by the specified artist. Note that this method creates and
returns a new ArrayList each time it is called. This method does NOT display anything!!

Now we will test our code. Since we will be testing our classes with the same users, we will add the
following static methods to the User class (Each method creates and returns a unique user with some
songs in their song list):


Code:
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;
}

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;
}

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;
}

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;
}

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;
}

Now test your code with the following test program:
Code:

class MusicExchangeTestProgram {
         public static void main(String args[]) {
         // Create a new music exchange center
          MusicExchangeCenter mec = new MusicExchangeCenter();

         // Create some users and give them some songs
         User discoStew = User.DiscoStew();
         User sleepingSam = User.SleepingSam();
         User ronnieRocker = User.RonnieRocker();
         User countryCandy = User.CountryCandy();
         User peterPunk = User.PeterPunk();

         // Register the users, except SleepingSam
         discoStew.register(mec);
         ronnieRocker.register(mec);
         countryCandy.register(mec);
         peterPunk.register(mec);

         // Display the state of things before anyone logs on
         System.out.println("Status: " + mec);
         System.out.println("On-Line Users: " + mec.onlineUsers());
         System.out.println("Available Songs: " + mec.allAvailableSongs() + "\n");


          // Attempt to log on two registered users and one unregistered user
          discoStew.logon(mec);
           sleepingSam.logon(mec); // Should not work
           ronnieRocker.logon(mec);
           System.out.println("Status: " + mec);
           System.out.println("On-Line Users: " + mec.onlineUsers());
           System.out.println("Available Songs: " + mec.allAvailableSongs() + "\n");


            // Log on two more users
            countryCandy.logon(mec);
            peterPunk.logon(mec);
            System.out.println("Status: " + mec);
            System.out.println("On-Line Users: " + mec.onlineUsers());
            System.out.println("Available Songs: " + mec.allAvailableSongs());
            System.out.println("Available Songs By Jaw: " +
            mec.availableSongsByArtist("Jaw") + "\n");

            // Log off three users (one is not even logged in)
            countryCandy.logoff(mec);
            discoStew.logoff(mec);
            sleepingSam.logoff(mec);
            System.out.println("Status: " + mec);
            System.out.println("On-Line Users: " + mec.onlineUsers());
            System.out.println("Available Songs: " + mec.allAvailableSongs());
            System.out.println("Available Songs By Jaw: " +
            mec.availableSongsByArtist("Jaw") + "\n");

            // Log off the last two users
            peterPunk.logoff(mec);
            ronnieRocker.logoff(mec);
            System.out.println("Status: " + mec);
            System.out.println("On-Line Users: " + mec.onlineUsers());
            System.out.println("Available Songs: " + mec.allAvailableSongs());
            System.out.println("Available Songs By Jaw: " +
            mec.availableSongsByArtist("Jaw") + "\n");
      }
}


Here is the output that you should see:

Status: Music Exchange Center (0 users on-line, 0 songs available)
On-Line Users: []
Available Songs: []


Status: Music Exchange Center (2 users on-line, 8 songs available)
On-Line Users: [Disco Stew: 4 songs (online), Ronnie Rocker: 4 songs (online)]
Available Songs: ["Hey Jude" by The Beatles 4:35, "Barbie Girl" by Aqua 3:54, "Only You Can Rock
Me" by UFO 4:59, "Paper Soup Cats" by Jaw 4:18, "Rock is Cool" by Yeah 4:17, "My Girl is Mean to
Me" by Can't Stand Up 3:29, "Only You Can Rock Me" by UFO 4:52, "We're Not Gonna Take It" by
Twisted Sister 3:9]

Status: Music Exchange Center (4 users on-line, 16 songs available)
On-Line Users: [Disco Stew: 4 songs (online), Ronnie Rocker: 4 songs (online), Country Candy: 3
songs (online), Peter Punk: 5 songs (online)]
Available Songs: ["Hey Jude" by The Beatles 4:35, "Barbie Girl" by Aqua 3:54, "Only You Can Rock
Me" by UFO 4:59, "Paper Soup Cats" by Jaw 4:18, "Rock is Cool" by Yeah 4:17, "My Girl is Mean to
Me" by Can't Stand Up 3:29, "Only You Can Rock Me" by UFO 4:52, "We're Not Gonna Take It" by
Twisted Sister 3:9, "If I Had a Hammer" by Long Road 4:15, "My Man is a 4x4 Driver" by Ms. Lonely
3:7, "This Song is for Johnny" by Lone Wolf 4:22, "Bite My Arms Off" by Jaw 4:12, "Where's My
Sweater" by The Knitters 3:41, "Is that My Toenail ?" by Clip 4:47, "Anvil Headache" by Clip 4:34,
"My Hair is on Fire" by Jaw 3:55]
Available Songs By Jaw: ["Paper Soup Cats" by Jaw 4:18, "Bite My Arms Off" by Jaw 4:12, "My Hair is
on Fire" by Jaw 3:55]


Status: Music Exchange Center (2 users on-line, 9 songs available)
On-Line Users: [Ronnie Rocker: 4 songs (online), Peter Punk: 5 songs (online)]
Available Songs: ["Rock is Cool" by Yeah 4:17, "My Girl is Mean to Me" by Can't Stand Up 3:29,
"Only You Can Rock Me" by UFO 4:52, "We're Not Gonna Take It" by Twisted Sister 3:9, "Bite My Arms
Off" by Jaw 4:12, "Where's My Sweater" by The Knitters 3:41, "Is that My Toenail ?" by Clip 4:47,
"Anvil Headache" by Clip 4:34, "My Hair is on Fire" by Jaw 3:55]
Available Songs By Jaw: ["Bite My Arms Off" by Jaw 4:12, "My Hair is on Fire" by Jaw 3:55]
Status: Music Exchange Center (0 users on-line, 0 songs available)
On-Line Users: []
Available Songs: []
Available Songs By Jaw: []

竞赛的一道题, 比较长,希望有耐心的朋友帮我看看

搜索更多相关的解决方案: Class  The  

----------------解决方案--------------------------------------------------------
这个我估计写要帮你写半天,如果你要参加竞赛,建议你先自己看看,比赛的时候你不能带我的是吧,
这个只不过是前边程序的拓展。添加了注册,登陆。分配歌曲资源,登陆后显示用户的名称,这个简单
用session存一下就可以了。你先写,用eclipse建一个工程。把思路找到。
----------------解决方案--------------------------------------------------------
  相关解决方案