0

我在方法部分上方的评论中解释了我要做什么:

public int addPatron(String name) throws PatronException {
    int i = 0;
    //1. Iterate through a hashmap, and confirm the new name I am trying to add to the     record doesn't already exist in the hashmap
    for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
        Patron nameTest = entry.getValue();
        //2. If the name I am trying to add already exists, we want to throw an exception saying as much.
        if (nameTest.getName() == name) {
            throw new PatronException ("This patron already exists");
            //3. If the name is unique, we want to get the largest key value (customer number) already in the hash, an increment by one.
        } else if (nameTest.getName() != name) {
            Map.Entry<Integer,Patron> maxEntry = null;
            for(Map.Entry<Integer, Patron> entryCheck : patrons.entrySet()) {
                if (maxEntry == null || entryCheck.getKey() > maxEntry.getKey()) {
                    maxEntry = entryCheck;
                    i = maxEntry.getKey();
                    i++;
                }
            }

        } else {
            throw new PatronException("Something's not working!");
        }
        //4. If everything is ok up to this point, we want to us the name and the new customer id number, and use those to create a new Patron object, which then gets added to a hashmap for this class which contains all the patrons.
        Patron newPatron = new Patron(name, i);
        patrons.put(i, newPatron);
    }
    return i;
}

当我尝试运行一个简单的单元测试时,如果我连续两次成功为 addPatron 添加相同的名称,该单元测试将失败,测试失败。

try {
    testLibrary.addPatron("Dude");
    testLibrary.addPatron("Dude");
    fail("This shouldn't have worked");

测试失败,告诉我 addPatron 方法能够使用相同的名称两次。

@乔恩斯基特:

我的赞助人课程如下所示:

public class Patron {

//attributes
private String name = null;
private int cardNumber = 0;

//operations
public Patron (String name, int cardNumber){
    this.name = name;
    this.cardNumber = cardNumber;
}

public String getName(){
    return name;

}

public int getCardNumber(){
    return cardNumber;
}

}

4

4 回答 4

2

正如其他人所说,使用==for 比较字符串几乎肯定是不合适的。但是,它实际上不应该在您的测试用例中引起问题,因为您两次使用相同的常量字符串,所以==应该可以工作。当然,您仍然应该修复要使用的代码equals

也不清楚Patron构造函数或getName方法做了什么——其中任何一个都可能导致问题(例如,如果他们创建了字符串的副本——这会导致你的测试失败,但通常也是不必要的)。

让我更担心的是这条评论:

// 3. If the name is unique, we want to get the largest key value (customer number) 
// already in the hash, an increment by one.

此评论主循环中。所以到那时我们不知道这个名字是唯一的——我们只知道它在这个迭代中与顾客的名字不匹配。

更令人担忧的是——我刚刚注意到这一点——你也在迭代块中执行了添加。在我看来,你应该有更多这样的东西:

public int addPatron(String name) throws PatronException {
    int maxKey = -1;

    for (Map.Entry<Integer, Patron> entry : patrons.entrySet()) {
        if (entry.getValue().getName().equals(name)) {
            // TODO: Consider using IllegalArgumentException
            throw new PatronException("This patron already exists");
        }
        maxKey = Math.max(maxKey, entry.getKey());
    }
    int newKey = maxKey + 1;
    Patron newPatron = new Patron(name, newKey);
    patrons.put(newKey, newPatron);
    return newKey;
}

此外,听起来你真的想要一张从名字到赞助人的地图,可能还有从 id 到赞助人的地图。

于 2012-12-15T21:53:44.357 回答
0

你需要使用equals来比较java中的String对象,而不是==。所以替换:

if (nameTest.getName() == name) {

和:

if (nameTest.getName().equals(name)) {
于 2012-12-15T21:49:54.613 回答
0

尝试使用

nameTest.getName().equals(name)

代替

nameTest.getName() == name

因为现在您正在比较引用而不是字符串的值。 在这里解释

再次查看您的代码

好吧,我再次查看了您的代码,问题是,您的 HashMap 在测试开始时为空。所以循环永远不会运行 ==> 永远不会添加赞助人或抛出异常。

于 2012-12-15T21:52:36.153 回答
-1

问题的原因是您如何使用比较运算符==

当您对两个对象使用此运算符时,您测试的是该变量指向相同的引用

要测试两个对象的值是否相等,您应该使用equals()方法或compareTo如果可用。

对于 String 类,调用 ofequals就足以检查是否存储更多相同的字符。

什么是equals方法?

比较 Object 的值 问题在于如何比较名称。

于 2012-12-15T21:50:12.007 回答