2

I'm trying to get a return null when my method does not find what its looking for. The method is looking for the most recent submission before the deadline (the parameter). Can someone help me how to get this when there is no submission before the parameter date. Thanks!

    // students is a LinkedList

    @Override
    public Submission getSubmissionBefore(String unikey, Date deadline) {
        // TODO Implement this, ideally in better than O(n)

        if (unikey == null || deadline == null) {
            throw new IllegalArgumentException("Unikey or deadline was null");
        }

        Date tempDate = null;
        int k = -1;

        for (int i = 0; i < this.students.size(); i++) {
            if (students.get(i).getUnikey().equals(unikey)) {
                if (tempDate == null) {
                    tempDate = students.get(i).getTime();
                    k = i;
                } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                    k = i;
                }
            }
        }

        return students.get(k);
    }
FatimahFatCakes
  • 331
  • 1
  • 3
  • 11
  • 2
    Do not use `==` to compare strings. You are doing that here: `students.get(i).getUnikey() == unikey`. Use `equals()` instead. See: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper Sep 20 '17 at 08:00
  • You are missing `tempDate = ...` in your `else if` case. – tobias_k Sep 20 '17 at 08:05

5 Answers5

3

You can also solve the problem in a functional way, with streams (of course if you are using java 8).

public static Submission getSubmisionBefore(String unikey, Date deadline) {
    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Optional<Submission> any = students.stream()
            .filter(submission -> submission.getUniKey().equals(unikey) && submission.getTime().before(deadline))
            .max(Comparator.comparing(Submission::getTime));

    return any.orElse(null);

}
Schidu Luca
  • 3,897
  • 1
  • 12
  • 27
3

There are a few problems with your code:

  • your first if condition is wrong; it will match any submission with matching unikey, even if it's after the deadline
  • your second else if is also wrong, as you set k, but do not set tempDate
  • before return, you should check whether k is still -1, e.g. using a ternary return k == -1 ? null : students.get(k); however:
  • instead of iterating the indices, you can iterate the Submissions directly using a for-each loop, making the entire code a lot more compact
  • by storing the result itself, you need neither k nor tempDate and do not have to check whether the index is still -1 at the end

Using above advice, you can simplify the loop to this:

Submission res = null;
for (Submission sub : this.students) {
    if (sub.getUnikey().equals(unikey)) {
        if (sub.getTime().before(deadline) && 
                (res == null || sub.getTime().after(res.getTime()))) {
            res = sub;
        }
    }
}
return res;

Or this more compact form, using Java 8 Streams:

return this.students.stream()                        // iterate all students
        .filter(s -> s.getUnikey().equals(unikey))   // get entries with matching key
        .filter(s -> s.getTime().before(deadline))   // get entries before deadline
        .max(Comparator.comparing(s -> s.getTime())) // from those, get the latest one
        .orElse(null);                               // or null if none exists
tobias_k
  • 81,265
  • 12
  • 120
  • 179
1
public Submission getSubmissionBefore(String unikey, Date deadline) {
    // TODO Implement this, ideally in better than O(n)

    if (unikey == null || deadline == null) {
        throw new IllegalArgumentException("Unikey or deadline was null");
    }

    Submission sub = null;
    Date tempDate = null;

    for (int i = 0; i < this.students.size(); i++) {
        if (unikey.equals(students.get(i).getUnikey())) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                sub = students.get(i);
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                sub = students.get(i);
            }
        }
    }

    return sub;
}
Endre Börcsök
  • 477
  • 1
  • 8
  • 19
  • 1
    Now that you don't need `i`, you can simplify this a lot by using a for-each loop. Also, `tempDate` is only updated in one of the cases; best combine those two conditions into one `if` block. – tobias_k Sep 20 '17 at 08:06
1

you can do it by adding controls

@Override
        public Submission getSubmissionBefore(String unikey, Date deadline) {
            // TODO Implement this, ideally in better than O(n)

            if (unikey == null || deadline == null) {
                throw new IllegalArgumentException("Unikey or deadline was null");
            }

            Date tempDate = null;
            int k = -1;

            for (int i = 0; i < this.students.size(); i++) {
                if (students.get(i).getUnikey().equals(unikey)) {
                    if (tempDate == null) {
                        tempDate = students.get(i).getTime();
                        k = i;
                    } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                        k = i;
                    }
                }
            }
            if(k!=-1)
            return students.get(k);
            else
            return null;
        }
UMKalaycı
  • 83
  • 9
1

You can compare current date and return null with if statement

    Date current = new Date();
    if(current.before(deadline))
           return null;
    else{  for (int i = 0; i < this.students.size(); i++) {
        if (students.get(i).getUnikey().equals(unikey)) {
            if (tempDate == null) {
                tempDate = students.get(i).getTime();
                k = i;
            } else if (students.get(i).getTime().before(deadline) && students.get(i).getTime().after(tempDate)) {
                k = i;
            }
        }
    }

    return students.get(k); 
                   }
Junaid Ahmed
  • 144
  • 2
  • 4