大牛帮我看一段code# JobHunting - 待字闺中
h*d
1 楼
phone interview, 实现一个in-memory filesystem. 刚开始面试,还没有摸到门路,
请大牛指点
Write an in memory filesystem! This is a simplified file system that only
supports directories. Your code needs to read from a file and parse the
commands listed below.
cd - Changes the current working directory. The working
directory begins at '/'. The special characters '.' should not modify the
current working directory and '..' should move the current working directory
to the parent.
mkdir - Creates a new directory with the current working directory as
the parent.
rmdir - Removes a directory from the current working directories's
children. This is only allowed if the directory you are removing contains no
children.
pwd - Prints the current working directory's path from the root. Example: /
school/homework
ls - Lists the children of the current working directory. These are printed
on a single line with a single space character separating them. Example:
math history spanish
When started the only directory in your virtual filesystem should be the
root directory, '/', the current working directory should also point here.
Example Input file:
mkdir school
cd school
pwd
mkdir homework
cd homework
mkdir math
mkdir lunch
mkdir history
mkdir spanish
rmdir lunch
ls
pwd
cd ..
mkdir cheatsheet
ls
rmdir cheatsheet
cd ..
pwd
Example output from the above file:
/school
math history spanish
/school/homework
homework
cheatsheet
/
以下是我的code,时间比较紧,所以validation & exception 都没怎么做,
public class FileSystem {
private static FileSystem fileSystem;
private Directory root;
private HashMap dirMap;
private Directory current;
/*
* use Singleton Design pattern to keep global configuration in
FileSystem instance
*/
private FileSystem() {
root = new Directory("", null);
dirMap = new HashMap();
current = root;
}
public static FileSystem getInstance(){
if (fileSystem==null) fileSystem = new FileSystem();
return fileSystem;
}
/*
* for command "mkdir"
*/
public boolean createDirctory(String name) {
if (isValidDirName(name)) {
Directory d = new Directory(name, current);
boolean res = current.add(name, d);
if (res) {
dirMap.put(name, d) ;
return true;
}
return false;
}
return false;
}
/*
* for command "cd"
*/
public void changeWorkingDir(String dir) {
if (dir.equals(".")) return ;
if (dir.equals("..")) {
current = current == root ? root : current.parent;
} else {
Directory res = current.goSubDir(dir);
if (res!=null) current = res;
}
}
/*
* for command "pwd"
*/
public void getCurrentPath(){
if (current==root) System.out.println("/");
else System.out.println(current.getPath());
}
/*
* for command "ls"
*/
public void listDirs() {
if (current.isEmptyDir()) {
System.out.println("it is a empty directoyr");
} else {
Set res = current.getChildren();
for (String name : res) {
System.out.print(name + " ");
}
System.out.println();
}
}
/*
* for command "rmdir"
*/
public void removeDir(String name) {
if (!dirMap.containsKey(name)) return;
else {
boolean res = current.removeSubDir(name);
if (res) dirMap.remove(name);
}
}
/*
* directory name must be 0-9 and A-Z or a-z
*/
private boolean isValidDirName(String name){
if (!Pattern.matches("[a-zA-Z0-9-_]{1,200}", name)) {
System.out.println(name + " is not valid. Please use a valid
name for directory" );
return false;
}
return true;
}
}
public class Directory {
private String name;
private String path;
protected Directory parent;
private HashMap children;
public Directory(String n, Directory d) {
name = n;
path = d==null ? "" : d.getPath() + "/" + name;
parent = d;
children = new HashMap();
}
/*
* get path for this directory
*/
public String getPath() {
return path;
}
/*
* get name for this directory
*/
public String getName() {
return name;
}
/*
* check whether is directory is empty
*/
public boolean isEmptyDir() {
return children.size()==0;
}
/*
* remove sub dircetory
*/
public boolean removeSubDir(String name) {
if (children.containsKey(name)) {
Directory dir = children.get(name);
if (dir.isEmptyDir()) {
children.remove(name);
return true;
}
return false;
}
return false;
}
/*
* return a set for all sub directory name
*/
public Set getChildren(){
return children.keySet();
}
/*
* add a new directory
*/
public boolean add(String name, Directory child){
if (children.containsKey(name)) return false;
children.put(name, child);
return true;
}
/*
* go to sub directory
*/
public Directory goSubDir(String child){
if (children.containsKey(child)) {
return children.get(child);
} else {
return null;
}
}
}
请大牛指点
Write an in memory filesystem! This is a simplified file system that only
supports directories. Your code needs to read from a file and parse the
commands listed below.
cd
directory begins at '/'. The special characters '.' should not modify the
current working directory and '..' should move the current working directory
to the parent.
mkdir
the parent.
rmdir
children. This is only allowed if the directory you are removing contains no
children.
pwd - Prints the current working directory's path from the root. Example: /
school/homework
ls - Lists the children of the current working directory. These are printed
on a single line with a single space character separating them. Example:
math history spanish
When started the only directory in your virtual filesystem should be the
root directory, '/', the current working directory should also point here.
Example Input file:
mkdir school
cd school
pwd
mkdir homework
cd homework
mkdir math
mkdir lunch
mkdir history
mkdir spanish
rmdir lunch
ls
pwd
cd ..
mkdir cheatsheet
ls
rmdir cheatsheet
cd ..
pwd
Example output from the above file:
/school
math history spanish
/school/homework
homework
cheatsheet
/
以下是我的code,时间比较紧,所以validation & exception 都没怎么做,
public class FileSystem {
private static FileSystem fileSystem;
private Directory root;
private HashMap
private Directory current;
/*
* use Singleton Design pattern to keep global configuration in
FileSystem instance
*/
private FileSystem() {
root = new Directory("", null);
dirMap = new HashMap
current = root;
}
public static FileSystem getInstance(){
if (fileSystem==null) fileSystem = new FileSystem();
return fileSystem;
}
/*
* for command "mkdir"
*/
public boolean createDirctory(String name) {
if (isValidDirName(name)) {
Directory d = new Directory(name, current);
boolean res = current.add(name, d);
if (res) {
dirMap.put(name, d) ;
return true;
}
return false;
}
return false;
}
/*
* for command "cd"
*/
public void changeWorkingDir(String dir) {
if (dir.equals(".")) return ;
if (dir.equals("..")) {
current = current == root ? root : current.parent;
} else {
Directory res = current.goSubDir(dir);
if (res!=null) current = res;
}
}
/*
* for command "pwd"
*/
public void getCurrentPath(){
if (current==root) System.out.println("/");
else System.out.println(current.getPath());
}
/*
* for command "ls"
*/
public void listDirs() {
if (current.isEmptyDir()) {
System.out.println("it is a empty directoyr");
} else {
Set
for (String name : res) {
System.out.print(name + " ");
}
System.out.println();
}
}
/*
* for command "rmdir"
*/
public void removeDir(String name) {
if (!dirMap.containsKey(name)) return;
else {
boolean res = current.removeSubDir(name);
if (res) dirMap.remove(name);
}
}
/*
* directory name must be 0-9 and A-Z or a-z
*/
private boolean isValidDirName(String name){
if (!Pattern.matches("[a-zA-Z0-9-_]{1,200}", name)) {
System.out.println(name + " is not valid. Please use a valid
name for directory" );
return false;
}
return true;
}
}
public class Directory {
private String name;
private String path;
protected Directory parent;
private HashMap
public Directory(String n, Directory d) {
name = n;
path = d==null ? "" : d.getPath() + "/" + name;
parent = d;
children = new HashMap
}
/*
* get path for this directory
*/
public String getPath() {
return path;
}
/*
* get name for this directory
*/
public String getName() {
return name;
}
/*
* check whether is directory is empty
*/
public boolean isEmptyDir() {
return children.size()==0;
}
/*
* remove sub dircetory
*/
public boolean removeSubDir(String name) {
if (children.containsKey(name)) {
Directory dir = children.get(name);
if (dir.isEmptyDir()) {
children.remove(name);
return true;
}
return false;
}
return false;
}
/*
* return a set for all sub directory name
*/
public Set
return children.keySet();
}
/*
* add a new directory
*/
public boolean add(String name, Directory child){
if (children.containsKey(name)) return false;
children.put(name, child);
return true;
}
/*
* go to sub directory
*/
public Directory goSubDir(String child){
if (children.containsKey(child)) {
return children.get(child);
} else {
return null;
}
}
}