Untitled
unknown
java
3 years ago
1.2 kB
5
Indexable
context UserList {
role [] Users {
public void print() {
// How to make a variable for the current user?
//var user = Users[index]
Output.outputList(
Users[index].name() + " <" +
Users[index].email() + ">"
)
if(index < lastIndex)
Users[index+1].print()
}
} requires {
String name() const;
String email() const;
}
role Output {
public void outputList(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>
*/
Editor is loading...