Hi Ive been working on this for a while and have everything wokring excep the movedown method which does not work properly. It wont allow me to go to a subdirectory and cannot seem to get it do do properly Ive tried nested loops and while loops none of which appear to be working correctly.
Hi I've been working on building this binary search tree and have been looking at other examples on how to implement the tree according to what I was instructed. If someone could help me with some code or psuedocode on where to start making all my methods form the tree it would be a great help.
FileSystem
public class FileSystemManager {
private FileTree tree;
public FileSystemManager() {
tree = new FileTree();
}
public void run() {
String command = " ";
Scanner in = new Scanner(System.in);
while (!command.equals("exit")) {
System.out.print(tree.getLocation() + "> ");
command = in.nextLine();
executeCommand(command);
}
}
private void executeCommand(String command) {
int breakPoint = command.indexOf(" ");
String argument = null;
if (breakPoint != -1) {
argument = command.substring(breakPoint + 1, command.length());
command = command.substring(0, breakPoint);
}
if (command.equals("cd")) {
if (!move(argument)) {
System.out.println("File not found.\n");
}
} else if (command.equals("ls")) {
System.out.println(tree.getChildren() + "\n");
} else if (command.equals("mkdir")) {
if (!tree.insert(argument)) {
System.out.println("Invalid file name.\n");
}
} else if (command.equals("rm")) {
if (!tree.remove(argument)) {
System.out.println("File not found.\n");
}
} else if (command.equals("tree")) {
// System.out.println(tree.getSubTree());
}else if (command.equals("exit")) {
} else {
System.out.println("'" + command + "' is not a recognized command");
}
}
private boolean move(String file) {
if (file == null) {
return false;
}
if (file.equals("~")) {
tree.goHome();
return true;
}
if (file.equals("..")) {
tree.moveUp();
return true;
}
return tree.moveDown(file);
}
}
Filetree
public class FileTree {
private Node root;
private Node current;
private String location;
public FileTree() {
root = new Node("H:");
location = root.getName();
current = root;
}
public String getLocation() {
return location;
}
String[] input = file.split("\\\\"); // breaks string into individual file names
ArrayList<Node> children = current.getChildren();
for (Node c : children) {
if (file.equals(c.getName())) {
current = c;
location += "\\" + current.getName();
return true;
if(file.equals(c.getName(file.split()))){
current = c;
location+= "\\" + current.getName();
return true;
}
}
}
return false;
}
public void moveUp() {
if (current != root) {
current = current.getParent();
int index = location.lastIndexOf("\\");
location = location.substring(0, index);
}
}
public void goHome() {
current = root;
location = current.getName();
}
public String getChildren() {
String files = new String();
ArrayList<Node> children = current.getChildren();
if (children != null) {
for (Node c : children) {
files += c.getName() + " ";
}
}
return files;
}
public boolean insert(String file) {
if (file != null && !file.equals(" ")) {
Node newFile = new Node(file);
newFile.setParent(current);
current.addChild(newFile);
return true;
}
return false;
}
public boolean remove(String file) {
if (file != null) {
ArrayList<Node> children = current.getChildren();
for (Node c : children) {
if (c.getName().equals(file)) {
children.remove(c);
return true;
}
}
}
return false;
}
Test
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileSystemManager m = new FileSystemManager();
m.run();
}
}
Node
public class Node {
private String name;
private Node parent;
private ArrayList<Node> children;
return name;
}
public ArrayList<Node> getChildren() {
return children;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void addChild(Node child) {
children.add(child);
}
public Node getParent() {
return parent;
}
}
public Node(String name) {
this.name = name;
children = new ArrayList<>();
}
public String getName() {