-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiles.java
105 lines (92 loc) · 2.64 KB
/
Files.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.yurii.salimov.lesson09.task04;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Files implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private boolean isFile;
private List<Files> list = new ArrayList<>();
private Files fileSystem;
public Files(final String name) {
this.name = name;
}
public Files(final String name, final boolean isFile) {
this(name);
this.isFile = isFile;
}
public void add(final Files file) {
if (!this.isFile) {
if (!find(file)) {
file.fileSystem = this;
this.list.add(file);
System.out.println("\"" + file + "\" has been added to \"" + this + "\"");
} else {
System.out.println("\"" + file + "\" is already added to \"" + file.fileSystem + "\"");
}
} else {
System.out.println("Can't add \"" + file + "\" into \"" + this + "\"!");
}
}
public void remove() {
if (this.fileSystem != null) {
this.fileSystem.list.remove(this);
System.out.println("Deleting completed.");
this.fileSystem = null;
} else {
System.out.println(this + " can't be removed.");
}
}
public String getPath() {
String path = "\\" + this;
if (this.fileSystem != null) {
path = this.fileSystem.getPath() + path;
}
return path;
}
public boolean find(Files file) {
if (this.list.contains(file)) {
return true;
} else {
for (Files files : this.list) {
if (files.find(file)) {
return true;
}
}
}
return false;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Files other = (Files) obj;
return (this.getPath().equals(other.getPath()));
}
@Override
public int hashCode() {
return Objects.hashCode(getPath()) + (this.isFile ? 1 : 0);
}
public void print() {
for (Files file : this.list) {
System.out.println(file.getPath());
file.print();
}
}
@Override
public String toString() {
return this.name;
}
}