Write a function that provides change directory (cd) function for an abstract file system.Notes:Root path is '/'.Path separator is '/'.Parent directory is addressable as ...Directory names consist only of English alphabet letters (A-Z and a-z).For example, new Path(/a/b/c/d).cd(../x).getPath() should return /a/b/c/x.Note: The evaluation environment uses '\' as the path separator.My implementation is humble as below:public class Path {    private String path;    public Path(String path) {        this.path = path;    }    public String getPath() {        return path;    }    public Path cd(String newPath) {        //1step consume newPath        //newPath to list        //take 1 char, head pull        //locate it with this.path,        //          case / ../ ./        //          this.path end poll        //return changed path and newPath        //recursive cd again        if( .equals(this.path)||.equals(newPath)||!this.path.startsWith(/))            return this;        else if(newPath.startsWith(/)) {            this.path = /;            return this.cd(newPath.length()>1?newPath.substring(1):);        }        else{            if(newPath.startsWith(...))                return this;            else if(newPath.startsWith(../)) {                this.cdup1();                return this.cd(newPath.length()>3?newPath.substring(3):);            }else if(newPath.startsWith(./)) {                return this.cd(newPath.length()>2?newPath.substring(2):);            }            else if(newPath.startsWith(..)) {                this.cdup1();                return this.cd(newPath.length()>2?newPath.substring(2):);            }else if(newPath.startsWith(.)) {                return this.cd(newPath.length()>1?newPath.substring(1):);            }            else {                String [] strarr = newPath.split(/);                if(this.path.endsWith(/))                    this.path = this.path+strarr[0];                else                    this.path = this.path+/+strarr[0];                String newPath2 =;                for (int i = 1; i < strarr.length; i++) {                    String s = strarr[i];                    newPath2=newPath2+s+/;                }                return this.cd(newPath2);            }         }    }    private void cdup1(){        if(/.equals(this.path)) return ;        String [] patharr=this.path.split(/);        System.out.println(patharr.length);        StringBuffer s = new StringBuffer(/);        for (int i = 0; i < patharr.length-1; i++) {            if(!.equals(patharr[i]))                s.append( patharr[i]).append(/);        }        String endpath = s.toString();        if(!/.endsWith(endpath)&&endpath.endsWith(/)) endpath=endpath.substring(0,endpath.length()-1);        this.path = endpath;        System.out.println(this.path);        return;    }    public static void main(String[] args) {        Path path = new Path(/a/b/c/d);        System.out.println(path.cd(../../../x).getPath());    }}It works, but I feel there must be better way to do that?
Simulate the cd command of a file system
java;strings;file system