-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.java
88 lines (77 loc) · 2.12 KB
/
FileSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
design a file system
*/
//package com.amazon
import java.util.*;
abstract class FileSystemElement{
//common attribute
protected String name;
//common method
public abstract void ls();
public String getName(){
return name;
}
public void setName(String newName){
name = newName;
}
}
class File extends FileSystemElement{
public File(String name){
super();
this.name = name;
}
public void ls(){
System.out.println(FileSystem.currentIndent + name);
}
}
class Directory extends FileSystemElement{
public Directory(String name){
super();
this.name = name;
}
private Set<Object> elements = new TreeSet<Object>(
new Comparator<Object>(){
public int compare(Object a, Object b){
return ((FileSystemElement)a).getName().compareTo(((FileSystemElement)b).getName());
}
}
);
public boolean add(Object obj){
if(elements.contains(obj) == true)
return false;
else
elements.add(obj);
return true;
}
public void ls(){
System.out.println(FileSystem.currentIndent + name);
FileSystem.currentIndent += " ";
Iterator it = elements.iterator();
while(it.hasNext() == true){
((FileSystemElement)it.next()).ls();
}
FileSystem.currentIndent = FileSystem.currentIndent.substring(0,FileSystem.currentIndent.length()-4);
}
}
public class FileSystem{
public static String currentIndent = "";
public static void main(String[] args){
Directory one = new Directory("dir111/");
Directory two = new Directory("dir222/");
Directory three = new Directory("dir333/");
File a = new File("a");
File b = new File("b");
File c = new File("c");
File d = new File("d");
File e = new File("e");
one.add(a);
one.add(two);
one.add(b);
two.add(c);
two.add(d);
two.add(three);
three.add(e);
//traverse file system
one.ls();
}
}