如何让java检查人们是否是朋友?

时间:2015-04-21 12:07:09

标签: java arraylist bufferedreader

如何检查人们是否是朋友。

输入看起来像

Hans Peter
Thomas Peter
Hans Thomas
Kate Thomas

然后我将检查Hans Thomas和Peter是否是预期输出为“是”的朋友

我的想法是创建一个读取行的bufferedreader和一个stringtokenizer,它接受来自bufferedreader的输入并将它们放入Arraylist中。

3 个答案:

答案 0 :(得分:2)

一些不完美的解决方案是建立朋友地图:

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

答案 1 :(得分:2)

您可以通过将人员包装在Person对象中来维护每个对象的朋友列表,从而尝试更多面向对象的方法。提供API来添加,检索和检查两个人之间的友谊。这样的事情可能是:

public interface FriendShip <T>{
    public boolean isFriend(T t);
    public List<T> getFriends();
    public void addFriend(T t);
}


public class Person implements FriendShip<Person> {
    List<Person> friendList = null;

    @Override
    public boolean isFriend(Person t) {
        return friendList.contains(t);
    }

    @Override
    public List<Person> getFriends() {
        return friendList;
    }

    @Override
    public void addFriend(Person newFriend) {
        friendList.add(newFriend);
        if(!newFriend.isFriend(this)){
            newFriend.addFriend(this);
        }
    }
}

答案 2 :(得分:1)

  

我的想法是创建一个读取行的buffereader和一个从buffereader获取输入的stringtokenizer并放置......

到目前为止非常好!

  

......他们在Arraylist中。

瞄准你的目标,你可以想到一个更好的结构......(但此时我已经可以欢迎你使用图论了!): - )