Early in the morning, your leader rushed over to find you: "Quick, quick, urgent task!" Recently ChinaJoy is about to start, and the boss asks for an intuitive way to see the number of people on the line in each of our new online games. ”
You looked at the date, didn't you! This where is to start immediately, clearly is already started! How could it be too late?
"It's okay. "Your leader Comfort you:" The function is really very simple, the interface has been provided, you just need to call the line. ”
Well, you are reluctant to accept that you are accustomed to this sudden new demand.
Your leader to give you a specific description of the demand, your game currently has three, a dress has been open for some time, two and three clothes are new to open clothes. The design of the interface is very lightweight, you only need to call Utility.getonlineplayercount (int), the corresponding value of each suit can be used to get the corresponding number of online players, such as the introduction of 1, two into the 2, the three are incoming 3. If you pass in a nonexistent suit, you will return-1. Then you just have to assemble the data into XML, and the specific display function is done by your leader.
Well, it doesn't sound very complicated, so if you start starting now it seems like there's still time, so you hit the code right away.
First, define an interface Playercount for counting the number of online people, the following code:
Public interface Playercount {
String getservername ();
int Getplayercount ();
}
Then the definition of three statistical classes implements the Playercount interface, corresponding to three different suits, as follows:
public class ServerOne implements Playercount {
@Override public
String getServerName () {return
"one Suit";
}
@Override public
int Getplayercount () {return
utility.getonlineplayercount (1);
}
}
public class Servertwo implements Playercount {
@Override public
String getServerName () {return
"two suits"; c14/>}
@Override public
int Getplayercount () {return
utility.getonlineplayercount (2);
}
}
public class Serverthree implements Playercount {
@Override public
String getServerName () {return
"three suits";
}
@Override public
int Getplayercount () {return
utility.getonlineplayercount (3);
}
}
Then you define a Xmlbuilder class that encapsulates the data in each service into an XML format, as follows:
public class Xmlbuilder {public
static String buildXML (Playercount player) {
StringBuilder builder = new Stringbu Ilder ();
Builder.append ("<root>");
Builder.append ("<server>"). Append (Player.getservername ()). Append ("</server>");
Builder.append ("<player_count"). Append (Player.getplayercount ()). Append ("</player_count>");
Builder.append ("</root>");
return builder.tostring ();
}