I have added an object oriented file system. I am looking for suggestions for how I could improve it further.
package oopdesign.fileSystem;
public abstract class Entry {
protected Directory parent;
protected long created;
protected long lastUpdated;
protected long lastAccessed;
protected String name;
public Entry(String n, Directory p){
name = n;
parent = p;
created = System.currentTimeMillis();
lastUpdated = System.currentTimeMillis();
lastAccessed = System.currentTimeMillis();
}
public boolean delete(Entry entry){
if(parent == null) return false;
return parent.deleteEntry(this);
}
public abstract int size();
public String getFullPath(){
if( parent == null ) return name;
else return parent.getFullPath() + "/" + name;
}
/* Getter and setter */
public long getCreationTime() { return created; }
public long getLastUpdatedTime() { return lastUpdated; }
public long getLastAccessed() { return lastAccessed; }
public void changeName(String n) { name = n; }
public String getName() { return name; }
}
package oopdesign.fileSystem;
public class File extends Entry {
private String content;
private int size;
public File(String entryName, Directory directory, int size) {
super(entryName, directory);
this.content = content;
this.size = size;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int size() {
return size;
}
}
package oopdesign.fileSystem;
import java.util.ArrayList;
public class Directory extends Entry {
ArrayList<Entry> fileList;
public Directory(String entryName, Directory directory) {
super(entryName, directory);
fileList = new ArrayList<Entry>();
}
public int size() {
int size = 0;
for (Entry e: fileList) {
size += e.size();
}
return size;
}
public int numberOfFiles() {
int count = 0;
for(Entry e : fileList){
if(e instanceof Directory){
count ++;
Directory d = (Directory) e;
count += d.numberOfFiles();
}else if(e instanceof File){
count ++;
}
}
return count;
}
public void addEntry(Entry entry){
fileList.add(entry);
}
public boolean deleteEntry(Entry entry){
return fileList.remove(entry);
}
protected ArrayList<Entry> getContents() { return fileList; }
}
FileSystem
with an actual use, you should attempt to write a FileSystem-extension. Have a look atjava.nio.file.FileSystems
andjava.nio.file.spi.FileSystemProvider
to get you started. \$\endgroup\$