Home > Net >  Linkedlist with multi attributes
Linkedlist with multi attributes

Time:10-20

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) {
 
  }

}

Test Case

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

I am so confused about the input of the isValid method. So the node here is Payment and the input for the isValid is String of person which part of the data that payment holds, how do I check all the person are in there? Can a node hold more than 1 data ?

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

This is what I have done but seems only returning false all the time.

CodePudding user response:

I got what you meant.

What you want to do is search the string array contains names of persons to see if both to and from are in the array.

Here's a possible solution:

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

CodePudding user response:

The documentation is still poor, but I assume you have to check wether fromPerson is in persons and toPerson is in (element of) persons...

That would match your expectations and the test cases.

The current implementation currently discards any persons array of length > 2.

Improvement proposal (utilizing hashset):

public boolean isValid(String[] persons) {
    HashSet<String> util = new HashSet<>(Arrays.asList(persons));
    return util.contains(fromPerson) && util.contains(toPerson);
}
  • Related