具有多属性的链表


public class Payment { 
public String fromPerson; // name of Person paying
public String toPerson; // name of Person receiving payment
public double amount; // amount payed
public Timestamp timestamp; // unique time stamp (provided at the time of creation)
public Payment next; // reference to next payment in appropriate Ledger Linked List
/**
* Basic empty constructor for a payment
*/
public Payment() {
// Provided - not to be changed
this.fromPerson = null;
this.toPerson = null;
this.amount = 0;
this.timestamp = null;
this.next = null;
} 
/**
* Payment constructor with all necessary fields to be inserted in Ledger
* 
* @param fromPerson is the person paying
* @param toPerson   is the person receiving the payment
* @param amount     is the amount payed
* @param timestamp  is time at creation of payment
* @param next       is the next payment
*/
public Payment(String fromPerson, String toPerson, double amount, Payment next) {
// Provided - not to be changed
this.fromPerson = fromPerson;
this.toPerson = toPerson;
this.amount = amount;
this.timestamp = new Timestamp(); // time stamp is automatically set at creation
this.next = next;
}
/**
* A Payment is valid if it is done between registered persons provided in the
* array persons
* 
* 
* @param persons - array of persons who can make and receive payment
* 
* @return true if the Payment is valid based on the criteria listed above,
*         false otherwise
*/
public boolean isValid(String[] persons) {

}
}

测试用例

public void testPaymentIsValid() {
assertTimeoutPreemptively(Duration.ofMillis(1000), () -> {
String[] registeredPeopleV1 = { "a", "b", "c", "d", "e", "f", "g" };
String[] registeredPeopleV2 = { "Brian", "Thomas", "Elizabeth", "Sakura", "Lucy", "Tyson" };
// Case 1: Payment from unregistered person
Payment invalid1 = new Payment("t", "a", 10.0, null);
assertFalse(invalid1.isValid(registeredPeopleV1));
Payment invalid2 = new Payment("Gray", "Brian", 4.0, null);
assertFalse(invalid2.isValid(registeredPeopleV2));
// Case 2: Payment to unregistered person
Payment invalid3 = new Payment("a", "t", 10.0, null);
assertFalse(invalid3.isValid(registeredPeopleV1));
Payment invalid4 = new Payment("Brian", "Gray", 4.0, null);
assertFalse(invalid4.isValid(registeredPeopleV2));
// Case 3: Unregistered people involved in the payment
Payment invalid5 = new Payment("Sai", "Mei", 22.1, null);
assertFalse(invalid5.isValid(registeredPeopleV2));
// Case 4: Valid payment between registered people
Payment valid1 = new Payment("a", "b", 100.0, null);
assertTrue(valid1.isValid(registeredPeopleV1));
Payment valid2 = new Payment("Sakura", "Thomas", 20.0, null);
assertTrue(valid2.isValid(registeredPeopleV2));
Payment validCase = new Payment("sakura", "thomas", 220, null);
assertTrue(validCase.isValid(registeredPeopleV2)); // The names should not be case-sensitive
// Case 5: Payment whose next is NOT null (To ensure it only checks current
// payment and NOT the next one)
Payment valid3 = new Payment("Lucy", "Thomas", 20.0, valid2);
assertTrue(valid3.isValid(registeredPeopleV2));
Payment valid4 = new Payment("Lucy", "Thomas", 20.0, invalid2); // doesn't matter even if next payment is invalid
assertTrue(valid4.isValid(registeredPeopleV2));
Payment invalid6 = new Payment("t", "a", 10.0, valid1); // doesn't matter even if next payment is valid
assertFalse(invalid6.isValid(registeredPeopleV1));
Payment invalid7 = new Payment("t", "a", 10.0, invalid1);
assertFalse(invalid7.isValid(registeredPeopleV1));
});
currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
}

我对isValid方法的输入很困惑。所以这里的节点是Payment,isValid的输入是String of person,Payment持有的数据的哪一部分,我如何检查所有人都在那里?一个节点可以容纳1个以上的数据吗?

public boolean isValid(String[] persons) {
for(int i=0;i<persons.length;i++) {
if(this.fromPerson.equals(persons[i])&&this.toPerson.equals(persons[i])) {
return true;
}
}
return false;
}

这就是我所做的,但似乎一直都是假的。

我明白你的意思了。

您要做的是搜索包含人名的字符串数组,看看to和from是否都在数组中。

这里有一个可能的解决方案:

class Payment {
String from, to;
Payment(String f, String t){ from = f;  to = t;}

boolean isValid(String[] persons) {
if (from.equals(to)) return false;  
boolean toFound = false;
boolean fromFound = false;
for(String p: persons){
if (to.equals(p))   toFound = true;
if (from.equals(p)) fromFound = true;
}
return toFound && fromFound;
}
}
public class Main{
public static void main(String[] args) {
String[] persons = {"a", "b", "c"};
Payment[] payments = { 
new Payment("a", "b"), new Payment("a", "a"),
new Payment("a", "x"), new Payment("x", "y"),
new Payment("b", "c")};
for(Payment p: payments)
System.out.println(p.isValid(persons));
}
}

相关内容

  • 没有找到相关文章

最新更新