import it.unito.di.javarole.*; class Job{ int numberPages; int getNumberPages(){ return numberPages;} Job(int n){ numberPages=n;} } class Login{ int login; Login(int n) {login=n;} } class Certificate{ int certificate; } role User playedby Accounted { int print(Job job) throws Exception; int getPrintedPages(); boolean resetCounter(SuperUser s); } interface Accounted { public Login getLogin(); } role SuperUser playedby Certified { int print(Job job); int getTotalPrintedPages(); boolean resetPrinterCounter(User user); } interface Certified extends Accounted { Certificate getCertificate(); } class AccountedPerson extends Object implements Accounted{ Login login; AccountedPerson(int n){ login = new Login(n);} public Login getLogin(){ return login;} } class CertifiedPerson implements Certified { public Login login; public Certificate certificate; public Login getLogin(){ return login;} public Certificate getCertificate() {return certificate;} CertifiedPerson (int l) { login= new Login(l);} } public class Printer { final static int MAX_PAGES_USER= 20; public String name; Printer(String name){ this.name=name; } Printer(){ this.name="default"; } private int totalPrintedPages = 0; private void print(Job job, Login login) { totalPrintedPages += job.getNumberPages(); // performs printing } private boolean validCertificate(Certificate cert) { return true; // checks the certificate cert } definerole User { // implementation of the role User private int counter = 0; public int print(Job job) throws Exception{ if (counter > MAX_PAGES_USER) throw new IllegalPrintException(); counter += job.getNumberPages(); Printer.this.print(job, that.getLogin()); return counter; } public int getPrintedPages(){ return counter; } public boolean resetCounter(SuperUser s){ if (s.resetPrinterCounter(this)) { counter=0; return true; } return false; } } definerole SuperUser { // implementation of the role SuperUser public SuperUser()throws Exception { //first, verify the identity of the player if (!validCertificate(that.getCertificate())) throw new Exception("You are not allowed to enter this role"); } public int print(Job job) { Printer.this.print(job, that.getLogin()); return totalPrintedPages; } public int getTotalPrintedPages() { return totalPrintedPages; } public boolean resetPrinterCounter(User user) { return true; } } class IllegalPrintException extends Exception{ public IllegalPrintException(){ System.err.println("Too pages have been printed"); } } public static void main(String[] args) throws Exception{ Job job1, job2, job3; job1=new Job(19); job2= new Job(1); job3= new Job(23); Printer laser = new Printer("laser"); AccountedPerson chris = new AccountedPerson(25); CertifiedPerson sergio = new CertifiedPerson(25); //laser.User c = laser.new User(chris); è un costrutto da implementare? non credo..f.f User c = laser.new User(chris); laser.new SuperUser(sergio); c.print(job1);//== ((laser.User) chris).print(job1); //System.out.println(c.equals((laser.User)chris)); true ((laser.SuperUser) sergio).print(job2); System.out.println("Chris has printed " + ((laser.User) chris).getPrintedPages() + " pages"); System.out.println("The printer laser has printed a total of " + ((laser.SuperUser) sergio).getTotalPrintedPages() + " pages"); User user = ((laser.User) chris); user.print(job3); System.out.println("Chris has printed " + ((laser.User) chris).getPrintedPages() + " pages"); ((RoleInterface)c).destroy();//==((RoleInterface)((laser.User)chris)).destroy(); //chris.removeRole((laser.User)chris, laser); NullRoleException //((laser.User) chris).print(job1); NullRoleException } }