Question regarding ClassScheduling.java

I am currently writing the Tokio equivalent of ClassScheduling.java app.

As I was studying the Java code, I found the something potentially strange in the following code snippet.

// in method simulateStudents
try {
  // ...
  if (mood.equals("add")) {
    c = allClasses.get(rand.nextInt(allClasses.size()));
    signup(db, studentID, c);
    myClasses.add(c);
  } // ...
}

private static void signup(TransactionContext db, final String s, final String c) {
  db.run((Transaction tr) -> {
    // ...
    if (tr.get(rec).join() != null)
      return null; // already signed up

    // ...
  });
}

In the unlikely event where rand.nextInt returns the same number twice, wouldn’t we end up adding the same class to myClasses array twice?

Yeah, I believe you would. I think myClasses should probably be a Set instead of a List. It looks like the signup method already anticipates that you might already be signed up for a class, though the surrounding code doesn’t

Thanks for the reply @alloc :slight_smile:

Another scenario that I just encountered while debugging an error in the Tokio Class Scheduling App.

In the following code, suppose both oldC and newC contain classes that the student has already signed up for. Basically the student is trying to switch between two classes she has already signed up for.

private static void switchClasses(TransactionContext db, final String s, final String oldC, final String newC) {
  db.run((Transaction tr) -> {
    drop(tr, s, oldC);
    signup(tr, s, newC);
    return null;
  });
}

In this scenario, there is nothing to switch. However, from what I am understanding, we would be dropping oldC, and ignoring the second signup of newC.