一些不完美的解决方案是建立朋友地图:
Map<String, List<String>> friends = new HashMap();
List<String> hansFriends = new ArrayList();
hansFriends.add("Peter");
hansFriends.add("Thomas");
//and so on for each person
//...
//then add friends for "Hans"
peopleFriends.put("Hans", hansFriends);
//repeat for each person: Kate, Thomas
katesFriends = new ArrayList();
katesFriends.add("Thomas");
//...
thomasFriends = new ArrayList();
这将转化为以下示例:
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class WhoFriends
{
/**
* This is for reading friends. Peter,Hans or Peter; Hans or Peter, Hans works.
*/
public static final String FRIENDS_PATTERN = "(\\w+)\\W+(\\w+)";
/**
* We store friends to a map person -> {friends list}
*/
Map<String, List<String>> friends = new HashMap<String, List<String>>();
/**
* Getter for friends list.
*/
public Map<String, List<String>> getFriends()
{
return friends;
}
/**
* Factory method for friends list.
*/
public List<String> newFriendsList()
{
return new ArrayList<String>();
}
/**
* Show friends information.
*/
public void printInfo(String person1, String person2)
{
System.out.format("Are %s and %s friends? Answer: %b.%n", person1, person2, areFriends(person1, person2));
}
/**
* Show all we have information
*/
public void showFriends()
{
for(String person : getFriends().keySet())
{
System.out.format("%s is friends with: %s%n", person, getFriendsList(person).toString());
}
}
/**
* This also adds an empty friends list if no list found for the person.
*/
public List<String> getFriendsList(String person)
{
// have an empty list if no friends.
List<String> friendsList = newFriendsList();
if (getFriends().containsKey(person))
{
friendsList = getFriends().get(person);
}
else
{
// add empty friends list
getFriends().put(person, friendsList);
}
return friendsList;
}
/**
* Check if a person is friend with another person.
*/
public boolean isFriendOf(String person, String possiblyAFriend)
{
return getFriendsList(person).contains(possiblyAFriend);
}
public void makeFriends(String person1, String person2)
{
addFriend(person1, person2);
addFriend(person2, person1);
}
public void addFriend(String person, String friend)
{
getFriendsList(person).add(friend);
}
/**
* Two people are friends if they are to each other friends.
*/
public boolean areFriends(String person1, String person2)
{
return isFriendOf(person1, person2) && isFriendOf(person2, person1);
}
public void readFriends(String line)
{
Pattern friendsExpression = Pattern.compile(FRIENDS_PATTERN);
Matcher matcher = friendsExpression.matcher(line);
//Read pairs: Person1, Person2
if (matcher.find() && (2 == matcher.groupCount()))
{
String person1 = matcher.group(1);
String person2 = matcher.group(2);
System.out.format("Making friends from %s: %s and %s are friends now.%n",
matcher.group(0),
person1,
person2);
makeFriends(person1, person2);
}
}
/**
* Demonstration :)
*/
public static void main(String[] args)
{
WhoFriends wf = new WhoFriends();
/**
* Predefine some friendships.
*/
wf.makeFriends("Hans" , "Peter");
wf.makeFriends("Thomas", "Peter");
wf.makeFriends("Hans" , "Thomas");
wf.makeFriends("Kate" , "Thomas");
wf.printInfo("Hans" , "Peter");
wf.printInfo("Thomas", "Peter");
wf.printInfo("Hans" , "Thomas");
wf.printInfo("Kate" , "Thomas");
wf.printInfo("Kate" , "Peter");
wf.printInfo("Peter" , "Thomas");
/**
* Make friends
*/
Console console = System.console();
String line = null;
System.out.println("Reading friends. Type in each line Name1,Name2. When you don't want to make more friends type q on the line.");
/**
*/
while (!"q".equals(line = console.readLine()))
{
wf.readFriends(line);
}
wf.showFriends();
}
}