Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class fildir{
- int globalID;
- fildir()
- {
- Scanner input = new Scanner(System.in);
- HashMap<String, String> map;
- Directory head = null;
- Directory curr = null;
- int cse = 1;
- while(true)
- {
- String command = input.next();
- if(command.equals("end")) break;
- if(command.equals("init"))
- {
- System.out.println("File system #" + cse + ":\n");
- head = new Directory("home", 0, null);
- curr = head;
- map = new HashMap<String, String>();
- globalID++;
- }
- else if(command.equals("mkdir"))
- {
- String name = input.next();
- head = AddDirectory(head, name, curr);
- }
- else if(command.equals("mkfile"))
- {
- String name = input.next();
- head = AddFile(head, name, curr);
- }
- else if(command.equals("cd"))
- {
- String[] path = input.next().split("/");
- for(int i = 0; i < path.length; i++)
- {
- if(path[i].equals(".."))
- {
- //need to move up
- curr = curr.up;
- }
- else
- {
- //need to move forward.
- for(int j = 0; j < curr.sublist.size(); j++)
- {
- if(curr.sublist.get(j).name == path[i])
- curr = curr.sublist.get(j);
- }
- }
- }
- }
- else if(command.equals("ls"))
- {
- Directory temp = curr;
- String path = getPath(temp);
- System.out.println("Listing for " + path + ":");
- printDirectories(curr);
- printFiles(curr);
- System.out.println();
- }
- }
- }
- String getPath(Directory curr)
- {
- String path = "/" + curr.name;
- curr = curr.up;
- while(curr != null)
- {
- path = "/" + curr.name + path;
- }
- return path;
- }
- void printDirectories(Directory curr)
- {
- Collections.sort(curr.sublist);
- for(int i = 0; i < curr.sublist.size(); i++)
- System.out.println(curr.sublist.get(i).name);
- }
- void printFiles(Directory curr)
- {
- Collections.sort(curr.files);
- for(int i = 0; i < curr.files.size(); i++)
- System.out.println(curr.files.get(i));
- }
- Directory AddDirectory(Directory head, String name, Directory curr)
- {
- if(head.id == curr.id)
- {
- head.sublist.add(new Directory(name, globalID, head));
- globalID++;
- return head;
- }
- for(int i = 0; i < head.sublist.size(); i++)
- {
- Directory t = AddDirectory(head.sublist.get(i), name, curr);
- if(t != null)
- return head;
- }
- return null;
- }
- Directory AddFile(Directory head, String name, Directory curr)
- {
- if(head.id == curr.id)
- {
- head.files.add(name);
- return head;
- }
- for(int i = 0; i < head.sublist.size(); i++)
- {
- Directory t = AddFile(head.sublist.get(i), name, curr);
- if(t != null)
- return head;
- }
- return null;
- }
- public static void main(String[] args)
- {
- new fildir();
- }
- }
- class Directory implements Comparable<Directory>
- {
- String name;
- ArrayList<Directory> sublist;
- ArrayList<String> files;
- int id;
- Directory up;
- Directory(String name, int id, Directory up)
- {
- this.name = name;
- this.id = id;
- this.up = up;
- sublist = new ArrayList<Directory>();
- files = new ArrayList<String>();
- }
- @Override
- public int compareTo(Directory other)
- {
- return this.name.compareTo(other.name);
- }
- }
- /*
- init
- mkfile temp
- mkdir COURSES
- mkfile junk
- mkdir CONTESTS
- ls
- cd COURSES
- mkdir COP4020
- cd COP4020
- mkfile exam1
- mkfile exam3
- mkfile exam2
- mkfile EXAM1
- ls
- cd ..
- mkdir COT4810
- cd COT4810
- cd ../../CONTESTS
- mkdir REAL
- mkfile scoring
- mkdir PRACTICE
- ls
- cd REAL
- mkfile temp
- mkdir junk
- cd ../../COURSES/COT4810
- ls
- cd ..
- ls
- init
- mkfile aaa
- mkfile bbb
- mkdir Level1
- cd Level1
- mkdir Level2a
- mkdir Level2b
- cd Level2a
- mkdir Level3
- cd Level3
- mkdir Level4
- cd ../../..
- ls
- end
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement