public class User extends Generics {
// Class data
public static KeyTypes key = KeyType.title; // static initializer
public static void setOrder(KeyTypes key) {User.key = key;}
public enum KeyType implements KeyTypes {title, name, grade, role}
// Instance data
private final String name;
private final int grade;
private final String role;
// Constructor
User(String name, int grade, String role)
{
this.setType("User");
this.name = name;
this.grade = grade;
this.role = role;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return User.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString() {
String output="";
if (KeyType.name.equals(this.getKey())) {
output += this.name;
} else if (KeyType.grade.equals(this.getKey())) {
output += this.grade;
} else if (KeyType.role.equals(this.getKey())) {
output += this.role;
} else {
output = super.getType() + ": " + this.name + ", " + this.grade + ", " + this.role;
}
return output;
}
// Test data initializer
public static User[] users() {
return new User[]{
new User("Allie", 12, "Admin"),
new User("Soren", 12, "Admin"),
new User("Saathvika", 12, "Admin"),
};
}
public static void main(String[] args)
{
// Inheritance Hierarchy
User[] objs = users();
// print with title
User.setOrder(KeyType.title);
User.print(objs);
// print name only
User.setOrder(KeyType.name);
User.print(objs);
}
}
User.main(null);