Last active
September 30, 2016 23:50
-
-
Save tipochka/387461214187a4a5d9681023b531f91b to your computer and use it in GitHub Desktop.
Создать классы Directory, File, которые имеют метод getSize(). Файл имеет фиксированный размер, размер директории считается по содержимому. В директорию можно вкладывать как файл, так и другую директорию. Зацикливания недопустимы.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class File{ | |
private int size; | |
public File(int size) { | |
this.size = size; | |
} | |
public int getSize() { | |
return size; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Создать классы 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