MCPcopy Create free account
hub / github.com/careercup/ctci / Directory

Class Directory

java/Chapter 8/Question8_9/Directory.java:5–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import java.util.ArrayList;
4
5public class Directory extends Entry {
6 protected ArrayList<Entry> contents;
7
8 public Directory(String n, Directory p) {
9 super(n, p);
10 contents = new ArrayList<Entry>();
11 }
12
13 protected ArrayList<Entry> getContents() {
14 return contents;
15 }
16
17 public int size() {
18 int size = 0;
19 for (Entry e : contents) {
20 size += e.size();
21 }
22 return size;
23 }
24
25 public int numberOfFiles() {
26 int count = 0;
27 for (Entry e : contents) {
28 if (e instanceof Directory) {
29 count++; // Directory counts as a file
30 Directory d = (Directory) e;
31 count += d.numberOfFiles();
32 } else if (e instanceof File) {
33 count++;
34 }
35 }
36 return count;
37 }
38
39 public boolean deleteEntry(Entry entry) {
40 return contents.remove(entry);
41 }
42
43 public void addEntry(Entry entry) {
44 contents.add(entry);
45 }
46}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected