Skip to content

Instantly share code, notes, and snippets.

@tipochka
Last active September 30, 2016 23:50
Show Gist options
  • Save tipochka/387461214187a4a5d9681023b531f91b to your computer and use it in GitHub Desktop.
Save tipochka/387461214187a4a5d9681023b531f91b to your computer and use it in GitHub Desktop.
Создать классы Directory, File, которые имеют метод getSize(). Файл имеет фиксированный размер, размер директории считается по содержимому. В директорию можно вкладывать как файл, так и другую директорию. Зацикливания недопустимы.
public class Directory {
private List<Directory> directoryList = new ArrayList<>();
private List<File> fileList = new ArrayList<>();
private static int dirId = 1;
private static int fId = 1;
public Directory(File file, File file1) {
add(file);
add(file1);
}
public Directory() {}
public Directory(File file) {
add(file);
}
public Directory(Directory directory, File file1) {
add(directory);
add(file1);
}
public Directory add(Directory directory) {
checkDirectory(directory);
directoryList.add(directory);
return this;
}
public Directory add(File file) {
fileList.add(file);
return this;
}
public List<Directory> getDirectoryList() {
return directoryList;
}
public List<File> getFileList() {
return fileList;
}
private void checkDirectory(Directory directory) {
checkThisDirectory(directory);
checkDirectoryList(directory);
}
private void checkThisDirectory(Directory directory) {
if (this == directory) {
throw new IllegalArgumentException("Incorrect directory");
}
}
public void checkDirectoryList(Directory directory) {
for (Directory dir : directoryList) {
if (dir == directory) {
throw new IllegalArgumentException("Incorrect directory");
}
dir.checkDirectoryList(directory);
}
}
public String getSize(){
String result = "root\n";
result += getDirString(this, "");
return result;
}
public String getDirString(Directory directory, String prefix){
String result = "";
for (Directory dir : directory.getDirectoryList()) {
result += prefix+"|--- dir"+dirId + "\n";
dirId++;
result += dir.getDirString(dir, "| "+prefix);
}
for (File file : directory.getFileList()) {
result += prefix+"|--- f"+fId+" ("+file.getSize()+" B)\n";
fId++;
}
return result;
}
}
public class File{
private int size;
public File(int size) {
this.size = size;
}
public int getSize() {
return size;
}
}
/*
Создать классы Directory, File, которые имеют метод getSize(). Файл имеет фиксированный размер, размер директории считается по содержимому. В директорию можно вкладывать как файл, так и другую директорию. Зацикливания недопустимы.
Создать иерархию директорий (клиентский код):
root
|--- dir1
| |--- f1 (10 B)
| |--- f2 (20 B)
|
|--- dir2
| |--- dir3
| | |--- f3 (30 B)
| |
| |--- f4 (40 B)
|
|--- f5 (50 B)
Посчитать размер директории root.
Пример клиентского кода: https://gist.github.com/anonymous/c8cfbd4b0692d80376a6.
*/
public class FsRunner {
public static void main(String[] args) {
Directory root = new Directory();
Directory d1 = new Directory(
new File(10),
new File(20)
);
Directory d2 = new Directory(
new Directory(
new File(30)
),
new File(40)
);
root.add(d1).add(d2).add(new File(50));
System.out.println(root.getSize());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment