title: What is wrong with my Java program for Role based access control?
body: <p>In the attached program I am only looking to get the first part of the output. It should look like this:</p>
  <pre><code>employee:(Permissions: [rw public, r secret], Subjects: [alice, bob], Parents: [])
  manager:(Permissions: [w secret, r top-secret], Subjects: [carol], Parents: [employee])
  ceo:(Permissions: [w top-secret], Subjects: [eve], Parents: [manager])
  </code></pre>
  <p>But when I run the attached program I get the following output:</p>
  <pre><code> rw public  r secret  alice  bob 
    rw public  r secret  alice  bob   w secret  r top-secret  carol 
     rw public  r secret  alice  bob   w secret  r top-secret  carol   w top-secret  eve 
  </code></pre>
  <p>I know it has to do with the first toString method but I dont know why I can't get the correct format. Also the addParent method, while correct, does not show up at all when I run the current toString method. All of the changes need to be made in the ### class...I think. Please let me know if you can help. Thank you in advance for all the help!</p>
  <p>Here is the code:</p>
  <pre><code>package lecture9
   
  import java.util.ArrayList; 
  import java.util.List;
  import java.util.Scanner; 
  
  /*
   * A role contains a list of permission objects
   *                 a list of subjects (as strings)
   *                 a list of parents (which roles does this role inherits from)
   *                 a role name
   */
  class Role {
      List&lt;Role&gt; parents = new ArrayList&lt;&gt;();
      List&lt;Permission&gt; permissions = new ArrayList&lt;&gt;();
      List&lt;String&gt; subjects = new ArrayList&lt;&gt;();
      String name;
      
      // a role must have a name
      Role(String name) {
          this.name = name;
      }
      // add parent role and return 'this' object (to allow method chaining)
      Role addParent(Role parent) {
          parents.add(parent);
          
          return this;
      }
      // add permission object and return 'this' object
      Role addPermission(Permission permission) {
          permissions.add(permission);
          
          return this;
      }
      // add subject (so that the 'subject' will have this role) and return 'this' object
      Role addSubject(String subject) {
          subjects.add(subject);
          
          return this;
      }
      
      // check whether this role has the permission that grants access 'rights' to 'object'
      /*boolean checkPermission(String object, String rights) {
          // TODO
      }
      
      // check whehther 'subject' has the access 'rights' to 'objects'
      boolean checkPermission(String subject, String object, String rights) {
          // TODO
      }*/
      
      // return a string representation of this role (need to include all information)
      public String toString() {
          String par = &quot;&quot;;
          String per = &quot;&quot;;
          String sub = &quot;&quot;;
          
          for(Role r: parents) {
              par = par + &quot; &quot; + r + &quot; &quot;;
              
          }
          
          for(Permission p : permissions){
              per = per + &quot; &quot; + p + &quot; &quot;;
            }
         
          for(String s : subjects) {
              sub = sub + &quot; &quot; + s + &quot; &quot;;
          }
          return par + per + sub;
         
      }
  }
  
  /*
   * permission object specifies the access 'rights' to an 'object'
   * both 'rights' and 'object' are strings
   */
  class Permission {
      final String object;
      final String rights;
      
      Permission(String object, String rights) {
          this.object = object;
          this.rights = rights;
      }
      // return true if 'object' is the 'object' in this permission and 'rights' is a substring of the 'rights' in this permission
      boolean checkPermission(String object, String rights) {
          return this.object.equals(object) &amp;&amp; this.rights.indexOf(rights) &gt;= 0;
      }
      // return string representation of this permission object
      public String toString() {
          return rights + &quot; &quot; + object;
      }
  }
    
  /*
   * represents a RBAC policy 
   */
  public class RBAC {
      // stores all roles in a list
      List&lt;Role&gt; roles = new ArrayList&lt;Role&gt;(); 
      
      // add a new role to the list
      RBAC addRole(Role role) {
          roles.add(role);
          return this;
      }
      // check whether 'subject' has the access 'rights' to 'object'
      /*boolean checkPermission(String subject, String object, String rights) {
          // TODO
      }*/
      // return string representation of the RBAC policy  
      public String toString() {
          String ret = &quot;&quot;;
          for(Role r: roles) {
              ret = ret + r + &quot;\n&quot;;
          }
          return ret;
      }
      
      public static void main(String[] args) {
          RBAC rbac = new RBAC();
          
          Role employee = new Role(&quot;employee&quot;)
              .addSubject(&quot;alice&quot;)
              .addSubject(&quot;bob&quot;)
              .addPermission(new Permission(&quot;public&quot;, &quot;rw&quot;))
              .addPermission(new Permission(&quot;secret&quot;, &quot;r&quot;));
          Role manager = new Role(&quot;manager&quot;)
              .addSubject(&quot;carol&quot;)
              .addPermission(new Permission(&quot;secret&quot;, &quot;w&quot;))
              .addPermission(new Permission(&quot;top-secret&quot;, &quot;r&quot;))
              .addParent(employee);
          Role ceo = new Role(&quot;ceo&quot;)
              .addSubject(&quot;eve&quot;)
              .addPermission(new Permission(&quot;top-secret&quot;, &quot;w&quot;))
              .addParent(manager);
  
          rbac.addRole(employee).addRole(manager).addRole(ceo);
          
          System.out.println(rbac);
          
          /*Scanner in = new Scanner(System.in);
  
          System.out.println(&quot;Enter your command (e.g. alice r secret):&quot;);
          String cmd = in.nextLine().trim(); 
  
          while(!cmd.equals(&quot;&quot;)) {
              String[] triple = cmd.split(&quot; &quot;);
              if(triple.length != 3) {
                  System.out.println(&quot;Illegal command. Try again&quot;);
              }
              else {
                  String subj = triple[0], op = triple[1], obj = triple[2];
                  try {
                      if(rbac.checkPermission(subj, obj, op)) {
                          System.out.printf(&quot;%s is allowed to perform %s operation on %s\n&quot;, subj, op, obj);
                      }
                      else {
                          System.out.printf(&quot;%s is NOT allowed to perform %s operation on %s\n&quot;, subj, op, obj);
                      }
                  } catch(Exception e) { System.out.println(e); }
              }
              System.out.println(&quot;\nEnter your command&quot;);
              cmd = in.nextLine().trim();
          }
  
          in.close();*/
      }
  }
  </code></pre>