title: File System using composite pattern
body: <p>I'm trying to build a File system such that contains the following classes: <code>FileSystem</code>, <code>Directory</code>, <code>File</code>.<br />
  My code is:</p>
  <pre><code>public class FileSystem {
      private List&lt;Directory&gt; ld;
      public FileSystem() {
          ld = new ArrayList&lt;Directory&gt;();
      }
      public void addDirectory(Directory dir) {
          ld.add(dir);
      }
      public void showAll() {
          for (Directory directory : ld) {
              directory.showAll();
          }
      }
  }
  
  public class File {
      private String fileName;
  
      public File(String fileName) {
          this.fileName = fileName;
      }
      public String getName() {
          return fileName;
      }
      public void setName(String name) {
          this.fileName = fileName;
      }
  }
  
  public class Directory {
      private String directoryName;
      private List&lt;File&gt; files;
      private List&lt;Directory&gt; subDirs;
  
      public Directory(String directoryName) {
          this.directoryName = directoryName;
          files = new ArrayList&lt;File&gt;();
          subDirs = new ArrayList&lt;Directory&gt;();
      }
      public String getName() {
          return this.directoryName;
      }
      public void setName(String name) {
          this.directoryName = name;
      }
      // Add and remove files
      public void addFile(File file) {
          files.add(file);
      }
      public void removeFile(File file) {
          files.remove(file);
      }
      // Add and remove directories
      public void addDirectory(Directory dir) {
          subDirs.add(dir);
      }
      public void removeDirectory(Directory dir) {
          subDirs.remove(dir);
      }
  
      public void showAll() {
          System.out.println(this.directoryName);
          for (File f : files) {
              System.out.println(&quot;    &quot; + f.getName());
          }
      }
  }
  
  public class Create {
      public static void main(String[] args) {
          FileSystem fs = new FileSystem();
          Directory d1 = new Directory(&quot;Directory1&quot;);
  
          d1.addFile(new File(&quot;FileX&quot;));
          Directory d11 = new Directory(&quot;Directory11&quot;);
          d11.addFile(new File(&quot;File1&quot;));
          d11.addFile(new File(&quot;File2&quot;));
          d1.addDirectory(d11);
  
          fs.addDirectory(d1);
          fs.showAll();
      }
  }  
  </code></pre>
  <p>But when I try to print all the content using the method <code>showAll</code> it prints only 'Directory1' and 'FileX', but I want to print:<br />
  Directory1<br />
  FileX<br />
  Directory11<br />
  File1<br />
  File2</p>