-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFolders.rb
47 lines (44 loc) · 951 Bytes
/
Folders.rb
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
class NamedSizedItem
@name = nil
@size = 0
def initialize(name)
@name = name
end
def name()
return @name
end
def size()
return @size
end
end
class NamedFile < NamedSizedItem
def initialize(name)
@name = name
@size = 1
end
end
class NamedFolder < NamedSizedItem
@files = []
def initialize(name)
@name = name
@files = []
end
def addFile(file)
@files << file
end
def size()
return @files.inject(0) { |res,elm| res + elm.size() }
end
end
file1 = NamedFile.new("readme")
file2 = NamedFile.new("license")
file3 = NamedFile.new("a.out")
subFolder1 = NamedFolder.new("Sub1");
subFolder2 = NamedFolder.new("Sub2");
subFolder3 = NamedFolder.new("Sub3");
subFolder1.addFile(file1)
subFolder2.addFile(file2)
subFolder3.addFile(file3)
subFolder2.addFile(subFolder1)
subFolder3.addFile(subFolder2)
puts(subFolder3.size())