Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created March 30, 2017 21:28
Show Gist options
  • Save rokon12/f96be532fb95d785261b3e613b79c153 to your computer and use it in GitHub Desktop.
Save rokon12/f96be532fb95d785261b3e613b79c153 to your computer and use it in GitHub Desktop.
package oop;
import java.util.ArrayList;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class Directory extends Entry {
protected ArrayList<Entry> contents;
public Directory(Directory parent, String name) {
super(parent, name);
this.contents = new ArrayList<>();
}
@Override
public int size() {
return contents.stream()
.mapToInt(Entry::size)
.sum();
}
public int numberOfFiles() {
int count = 0;
for (Entry content : contents) {
if (content instanceof Directory) {
count++;/// Directory counts as a file
Directory directory = (Directory) content;
count += directory.numberOfFiles();
} else if (content instanceof File) {
count++;
}
}
return count;
}
public void addEntry(Entry entry) {
contents.add(entry);
}
protected boolean deleteEntry(Entry entry) {
return contents.remove(entry);
}
public ArrayList<Entry> getContents() {
return contents;
}
}
package oop;
import java.time.LocalDateTime;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public abstract class Entry {
private Directory parent;
private LocalDateTime created;
private LocalDateTime lastUpdated;
private LocalDateTime lastAccessed;
private String name;
public Entry(Directory parent, String name) {
this.parent = parent;
this.name = name;
this.created = LocalDateTime.now();
this.lastAccessed = LocalDateTime.now();
this.lastAccessed = LocalDateTime.now();
}
public String getFullPath() {
if (parent == null) {
return name;
} else {
return parent.getFullPath() + "/" + name;
}
}
public boolean delete() {
if (parent == null) return false;
return parent.deleteEntry(this);
}
public abstract int size();
public LocalDateTime getCreated() {
return created;
}
public LocalDateTime getLastUpdated() {
return lastUpdated;
}
public LocalDateTime getLastAccessed() {
return lastAccessed;
}
public void changeName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setLastUpdated(LocalDateTime lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
package oop;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class File extends Entry {
private String content;
private int size;
public File(Directory parent, String name, int size) {
super(parent, name);
this.size = size;
}
@Override
public int size() {
return size;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package oop;
/**
* @author Bazlur Rahman Rokon
* @since 3/31/17.
*/
public class FileDemo {
public static void main(String[] args) {
Directory root = new Directory(null, "root");
File file = new File(root, "movie.mpg", 10);
root.addEntry(file);
System.out.println(root.size());
System.out.println(file.getFullPath());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment