├── Main.java ├── README.md ├── Node.java ├── LinkedList.java ├── HelperFunctions.java └── FileSystemTree.java /Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | FileSystemTree tree = new FileSystemTree(); 6 | 7 | while(true) { 8 | Scanner scanner = new Scanner(System.in); 9 | System.out.print("user@recursionist: "); 10 | String input = scanner.nextLine(); 11 | HelperFunctions.conductCommand(input, tree); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File Directory System Simulator 2 | A console application that simulates command line interface. Utilized graph structure and Linked List. 3 | 4 | ## Capable Commands 5 | **touch [fileName]** - create a file with [fileName] in the current directory
6 | **mkdir [dirName]** - create a child directory with [dirName] in the current directory
7 | **ls** - output all the files and child directories in the current directory
8 | **cd [targetPath]** - move to the [targetPath]
9 | **pwd** - output the current working directory
10 | **print [fileName]** - output the content of [fileName]
11 | **setContent [fileName] [newContent]** - set the [fileName]'s content to [newContent]
12 | **rm [fileName]** - remove [fileName] in the current directory
13 | 14 | ## Language 15 | Java 16 | -------------------------------------------------------------------------------- /Node.java: -------------------------------------------------------------------------------- 1 | public class Node { 2 | private String name; 3 | private Node parent; 4 | private String type; 5 | private String content; 6 | private LinkedList children; 7 | private Node next; 8 | 9 | public Node(String name, Node parent, String type) { 10 | this.name = name; 11 | this.parent = parent; 12 | this.type = type; 13 | this.content = "empty"; 14 | this.children = new LinkedList(); 15 | this.next = null; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public Node getParentNode() { 23 | return parent; 24 | } 25 | 26 | public String getParentName() { 27 | return parent.getName(); 28 | } 29 | 30 | public String getType() { 31 | return type; 32 | } 33 | 34 | public String getContent() { 35 | return content; 36 | } 37 | 38 | public LinkedList getChildren() { 39 | return children; 40 | } 41 | 42 | public String getChildrenString(){ 43 | return this.children.toString(); 44 | } 45 | public String getChildrenStringAll(){ 46 | return this.children.toStringAll(); 47 | } 48 | public String getChildrenStringReverse(){ 49 | return this.children.toStringReverse(); 50 | } 51 | 52 | public Node getNext() { 53 | return next; 54 | } 55 | 56 | public void setParent(Node parent) { 57 | this.parent = parent; 58 | } 59 | 60 | public void setContent(String content) { 61 | this.content = content; 62 | } 63 | 64 | public void addChild(Node childNode) { 65 | this.children.add(childNode); 66 | } 67 | 68 | public boolean isChildExist(String name) { 69 | return children.getNode(name) != null; 70 | } 71 | 72 | public Node getChild(String name) { 73 | if (!this.isChildExist(name)) { 74 | return null; 75 | } 76 | return children.getNode(name); 77 | } 78 | 79 | public void setNext(Node next) { 80 | this.next = next; 81 | } 82 | } -------------------------------------------------------------------------------- /LinkedList.java: -------------------------------------------------------------------------------- 1 | public class LinkedList { 2 | public Node head; 3 | public Node tail; 4 | 5 | public LinkedList() { 6 | this.head = null; 7 | this.tail = null; 8 | } 9 | 10 | public void add(Node node) { 11 | if (this.head == null) { 12 | this.head = node; 13 | } else if (this.tail == null) { 14 | this.tail = node; 15 | this.head.setNext(this.tail); 16 | } else { 17 | this.tail.setNext(node); 18 | this.tail = this.tail.getNext(); 19 | } 20 | } 21 | 22 | public String removeNode(String nodeName) { 23 | // remove head 24 | if (this.head.getName().equals(nodeName)) { 25 | this.head = this.head.getNext(); 26 | return nodeName; 27 | } 28 | Node iterator = this.head; 29 | if (iterator.getNext() == null) { 30 | return "No File"; 31 | } 32 | 33 | while (iterator.getNext() != null) { 34 | if (iterator.getNext().getName().equals(nodeName)) { 35 | // remove tail 36 | if (iterator.getNext() == this.tail) { 37 | this.tail = iterator; 38 | } 39 | // remove middle 40 | iterator.setNext(iterator.getNext().getNext()); 41 | return nodeName; 42 | } 43 | iterator = iterator.getNext(); 44 | } 45 | return "No File"; 46 | } 47 | 48 | public Node getNode(String nodeName) { 49 | Node iterator = this.head; 50 | while (iterator != null) { 51 | if (iterator.getName().equals(nodeName)) { 52 | return iterator; 53 | } 54 | iterator = iterator.getNext(); 55 | } 56 | return null; 57 | } 58 | 59 | public String toString() { 60 | String str = ""; 61 | Node iterator = this.head; 62 | while (iterator != null) { 63 | if (iterator.getName().charAt(0) != '.') { 64 | str += iterator.getName() + " "; 65 | } 66 | iterator = iterator.getNext(); 67 | } 68 | return str; 69 | } 70 | 71 | public String toStringAll() { 72 | String str = ""; 73 | Node iterator = this.head; 74 | while (iterator != null) { 75 | if (iterator.getName().charAt(0) != '.') { 76 | str = iterator.getName() + " " + str; 77 | } 78 | iterator = iterator.getNext(); 79 | } 80 | return str; 81 | } 82 | 83 | public String toStringReverse() { 84 | String str = ""; 85 | Node iterator = this.head; 86 | while (iterator != null) { 87 | str = iterator.getName() + " " + str; 88 | iterator = iterator.getNext(); 89 | } 90 | return str; 91 | } 92 | } -------------------------------------------------------------------------------- /HelperFunctions.java: -------------------------------------------------------------------------------- 1 | public class HelperFunctions { 2 | public static String appendError(String errorType) { 3 | String msg = ""; 4 | if (errorType.equals("command")) { 5 | msg = "invalid input. Commands are 'cd','print','rm', 'pwd', 'ls' 'mkdir', 'touch, move'"; 6 | } else if (errorType.equals("sameName")) { 7 | msg = "you already have the file or directory."; 8 | } 9 | return msg; 10 | } 11 | 12 | public static void touch(String[] arr, FileSystemTree tree) { 13 | if (arr.length == 2) { 14 | tree.touch(arr[1]); 15 | } else if (arr.length == 3) { 16 | tree.touchPath(arr[1], arr[2]); 17 | } else { 18 | HelperFunctions.appendError("touch"); 19 | } 20 | } 21 | 22 | public static void mkdir(String[] arr, FileSystemTree tree) { 23 | if (arr.length == 2) { 24 | tree.mkdir(arr[1]); 25 | } else if (arr.length == 3) { 26 | tree.mkdirPath(arr[1], arr[2]); 27 | } else { 28 | HelperFunctions.appendError("mkdir"); 29 | } 30 | } 31 | 32 | public static void ls(String[] arr, FileSystemTree tree) { 33 | if (arr.length == 1) { 34 | System.out.println(tree.ls()); 35 | } else if (arr.length == 2 && arr[1].equals("-a")) { 36 | System.out.println(tree.lsAll()); 37 | } else if (arr.length == 2 && arr[1].equals("-r")) { 38 | System.out.println(tree.lsReverse()); 39 | } else if (arr.length == 2) { 40 | System.out.println(tree.lsChild(arr[1])); 41 | } 42 | } 43 | 44 | public static void cd(String[] arr, FileSystemTree tree) { 45 | if (arr.length != 2) { 46 | HelperFunctions.appendError("cd"); 47 | } else { 48 | tree.cd(arr[1]); 49 | } 50 | } 51 | 52 | public static void pwd(String[] arr, FileSystemTree tree) { 53 | if (arr.length != 1) { 54 | HelperFunctions.appendError("pwd"); 55 | } else { 56 | System.out.println(tree.pwd()); 57 | } 58 | } 59 | 60 | public static void print(String[] arr, FileSystemTree tree) { 61 | if (arr.length != 2) { 62 | HelperFunctions.appendError("print"); 63 | } else { 64 | System.out.println(tree.print(arr[1])); 65 | } 66 | } 67 | 68 | public static void setContent(String[] arr, FileSystemTree tree) { 69 | if (arr.length != 3) { 70 | HelperFunctions.appendError("setContent"); 71 | } else { 72 | tree.setContent(arr[1], arr[2]); 73 | } 74 | } 75 | 76 | public static void rm(String[] arr, FileSystemTree tree) { 77 | if (arr.length != 2) { 78 | HelperFunctions.appendError("rm"); 79 | } else { 80 | tree.rm(arr[1]); 81 | } 82 | } 83 | 84 | // public static void move(String[] arr, FileSystemTree tree) { 85 | // System.out.println("Move"); 86 | // } 87 | 88 | public static void conductCommand(String command, FileSystemTree tree) { 89 | String[] commandArray = command.split(" "); 90 | if (commandArray.length == 0) { 91 | HelperFunctions.appendError("command"); 92 | } else if (commandArray[0].equals("touch")) { 93 | HelperFunctions.touch(commandArray, tree); 94 | } else if (commandArray[0].equals("mkdir")) { 95 | HelperFunctions.mkdir(commandArray, tree); 96 | } else if (commandArray[0].equals("ls")) { 97 | HelperFunctions.ls(commandArray, tree); 98 | } else if (commandArray[0].equals("cd")) { 99 | HelperFunctions.cd(commandArray, tree); 100 | } else if (commandArray[0].equals("pwd")) { 101 | HelperFunctions.pwd(commandArray, tree); 102 | } else if (commandArray[0].equals("print")) { 103 | HelperFunctions.print(commandArray, tree); 104 | } else if (commandArray[0].equals("setContent")) { 105 | HelperFunctions.setContent(commandArray, tree); 106 | } else if (commandArray[0].equals("rm")) { 107 | HelperFunctions.rm(commandArray, tree); 108 | // } else if (commandArray[0].equals("move")) { 109 | // HelperFunctions.move(commandArray, tree); 110 | } else { 111 | HelperFunctions.appendError("command"); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /FileSystemTree.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class FileSystemTree { 4 | public static HashMap map = new HashMap(); 5 | public Node root; 6 | public Node current; 7 | 8 | public FileSystemTree() { 9 | this.root = new Node("root", null, "directory"); 10 | this.current = this.root; 11 | } 12 | 13 | public Node findNode(String filePath) { 14 | Node iterator = this.root; 15 | String[] pathArr = filePath.split("/", filePath.length()); 16 | if (pathArr[0] != "root") { 17 | return null; 18 | } 19 | for (int i = 1; i < pathArr.length; i++) { 20 | if (iterator == null) { 21 | return null; 22 | } 23 | iterator = iterator.getChild(pathArr[i]); 24 | } 25 | return iterator; 26 | } 27 | 28 | public void touch(String fileName) { 29 | if (map.get(fileName) != null) { 30 | HelperFunctions.appendError("sameName"); 31 | return; 32 | } 33 | this.current.addChild(new Node(fileName, this.current, "file")); 34 | map.put(fileName, fileName); 35 | } 36 | 37 | public void touchPath(String path, String fileName) { 38 | Node targetNode = findNode(path); 39 | if (targetNode == null) { 40 | HelperFunctions.appendError("pathError"); 41 | } else { 42 | targetNode.addChild(new Node(fileName, targetNode, "file")); 43 | } 44 | } 45 | 46 | public void mkdir(String dirName) { 47 | if (map.get(dirName) != null) { 48 | HelperFunctions.appendError("sameName"); 49 | return; 50 | } 51 | this.current.addChild(new Node(dirName, this.current, "directory")); 52 | map.put(dirName, dirName); 53 | } 54 | 55 | public void mkdirPath(String path, String dirName) { 56 | Node targetNode = this.findNode(path); 57 | if (targetNode == null) { 58 | HelperFunctions.appendError("pathError"); 59 | } else { 60 | targetNode.addChild(new Node(dirName, targetNode, "directory")); 61 | } 62 | } 63 | 64 | public String ls() { 65 | return this.current.getChildrenString(); 66 | } 67 | 68 | public String lsChild(String nameOrPath) { 69 | Node child = this.current.getChild(nameOrPath); 70 | Node targetNode = this.findNode(nameOrPath); 71 | 72 | if (child != null) { 73 | return child.getChildrenString(); 74 | } else if (targetNode != null) { 75 | return targetNode.getChildrenString(); 76 | } 77 | return nameOrPath + "does not exist"; 78 | } 79 | 80 | public String lsAll() { 81 | return this.current.getChildrenStringAll(); 82 | } 83 | 84 | public String lsReverse() { 85 | return this.current.getChildrenStringReverse(); 86 | } 87 | 88 | public void cd(String dirNameOrPath) { 89 | Node child = this.current.getChild(dirNameOrPath); 90 | Node targetNode = this.findNode(dirNameOrPath); 91 | 92 | if (dirNameOrPath.equals("..") && this.current != this.root) { 93 | this.current = this.current.getParentNode(); 94 | } else if (child != null && child.getType().equals("directory")) { 95 | this.current = child; 96 | } else if (targetNode != null && targetNode.getType().equals("directory")) { 97 | this.current = targetNode; 98 | } 99 | return; 100 | } 101 | 102 | public String pwd() { 103 | String path = ""; 104 | Node iterator = this.current; 105 | while (iterator != null) { 106 | path = iterator.getName() + "/" + path; 107 | if (iterator == this.root) { 108 | return "/" + path; 109 | } 110 | iterator = iterator.getParentNode(); 111 | } 112 | return path; 113 | } 114 | 115 | public String print(String fileNameOrPath) { 116 | Node child = this.current.getChild(fileNameOrPath); 117 | Node targetNode = this.findNode(fileNameOrPath); 118 | 119 | if (child != null && child.getType().equals("file")) { 120 | return child.getContent(); 121 | } else if (targetNode != null && targetNode.getType().equals("file")) { 122 | return targetNode.getContent(); 123 | } else { 124 | return "No File"; 125 | } 126 | } 127 | 128 | public void setContent(String fileNameOrPath, String content) { 129 | Node child = this.current.getChild(fileNameOrPath); 130 | Node targetNode = this.findNode(fileNameOrPath); 131 | 132 | if (child != null && child.getType().equals("file")) { 133 | child.setContent(content); 134 | } else if (targetNode != null && targetNode.getType().equals("file")) { 135 | targetNode.setContent(content); 136 | } 137 | } 138 | 139 | public String rm(String nameOrPath) { 140 | Node child = this.current.getChild(nameOrPath); 141 | Node targetNode = this.findNode(nameOrPath); 142 | 143 | if (child != null) { 144 | return this.current.getChildren().removeNode(nameOrPath); 145 | } else if (targetNode != null) { 146 | String[] name = nameOrPath.split("/"); 147 | // return targetNode.getParentNode().children.remove(name[]) 148 | return "Need to work on here"; 149 | } else { 150 | return "There is no such path"; 151 | } 152 | } 153 | 154 | public void move(String targetPath, String goalPath) { 155 | Node targetNode = this.findNode(targetPath); 156 | Node goalNode = this.findNode(goalPath); 157 | 158 | if (targetNode == null || goalNode == null || goalNode.getType().equals("file")) { 159 | return; 160 | } 161 | if (this.rm(targetPath) != null) { 162 | targetNode.setParent(goalNode); 163 | goalNode.addChild(targetNode); 164 | } 165 | return; 166 | } 167 | } --------------------------------------------------------------------------------