Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.2 kB
1
Indexable
Never
context UserList {
  role [] Users {
    public void print() {
      // How to make a variable for the current user?
      //var user = Users[index]
      Output.printUser(
        Users[index].name() + " <" +
        Users[index].email() + ">"
      )
      
      if(index < lastIndex)
        Users[index+1].print()
    }
  } requires {
    String name() const;
    String email() const;
  }

  role Output {
    public void printUser(String s) {
      println(s)
    }
  } requires {
    void println(String s);
  }

  public UserList(List<User> users) {
    Users = users;
    Output = System.out;
  }

  public void printAll() {
    Users[0].print()
  }
}

class User {
  public User(String name, String email) {
    name_ = name.clone;
    email_ = email.clone;
  }

  public String name() const { return name_ }
  public String email() const { return email_ }

  private String name_, email_
}

{
  List<User> users = new List<User>();
  users.add(new User("Mr. A", "a@example.com"))
  users.add(new User("Mr. B", "b@example.com"))

  UserList userList = new UserList(users)
  userList.printAll()
}

/* GOLD:
Mr. A <a@example.com>
Mr. B <b@example.com>
*/