lundi 6 août 2018

Virtual file system simulator

It will be my still not a lot of experience, but lately I'm puzzling to implement a small simulator that I need for testing an app. This simulator must randomly create files and directories, as in a file system there will be some directories that will contain other directories or files, there will be empty directories and files that will be outside of all directories. So there will be a number of elements for each level as I explain in the drawing. I created the StructureFS class, in which I put (name, type, parent, level) of each element. I create a boolean isDir = rand.nextBoolean (), which causes the simulator to create a few elements of type = directory and some type = file. Regarding level 0, to name elements and level there is no problem creating everything, since no file or directory has parent, I create an arraylist mylist and with two nested for, one that cycles a max of levels and one that cycles a max of elements, I fill with mylist.add (new StructureFS (name, type, parent, level)). But by leveling down some files or directories must randomly take some directories that are at a higher level and thus always going down in level, and therefore the parent field of these elements must be set correctly accordingly. Do you have ideas and suggestions on how to do it? Since we are talking about levels, then fathers and children, I'm also trying with a tree and recursion, but I always jam on this parent. Thanks to those who will help me. Sorry for my english. I'm italian.My drawing

My code:

import java.util.*;

public class SimulateFS {

public static void main(String[] args) {

    ArrayList<ArrayList<StructFS>> mylist = new ArrayList<ArrayList<StructFS>>();
    ArrayList<StructFS> sub = new ArrayList<>();
    StructFS structFS = new StructFS();

    int maxLevel = 3;
    int maxElement = 4;
    Random rand = new Random();

    for (int i = 0; i < maxLevel; i++) {

        for (int j = 0; j < maxElement; j++) {

            boolean isDir = rand.nextBoolean();
            int isCont = rand.nextInt(2);

            if (i == 0) {
                sub.add(isDir ? new StructFS("Dir" + i + "_" + j, "directory", "null", isCont, i) : new StructFS("File" + i + "_" + j, "file", "null", 0, i));
            } else {//How to set the parent?

                sub.add(isDir ? new StructFS("Dir" + i + "_" + j, "directory", "???", isCont, i) : new StructFS("File" + i + "_" + j, "file", "???", 0, i));
            }

        }
        mylist.add(sub);

        sub = new ArrayList<>();

    }

    System.out.println(mylist);
}}

Class:

public class StructFS {

private String name;
private String type;
private String parent;
private int content;
private int level;

public StructFS() {
}

public StructFS(String name, String type, String parent, int content, int level) {
    super();
    this.name = name;
    this.type = type;
    this.parent = parent;
            this.content = content;
            this.level = level;
}

public int getContent() {
    return content;
}

public void setContent(int content) {
    this.content = content;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getParent() {
    return parent;
}

public void setParent(String parent) {
    this.parent = parent;
}

public int getLevel() {
    return level;
}

public void setId(int level){
        this.level = level;
    }

    @Override
public String toString() {
    return 
            "\n\nname=" + name  + 
            "\ntype=" + type +   
            "\nparent=" + parent  + "\ncontent=" + content +
            "\nlevel=" + level; 

}}




Aucun commentaire:

Enregistrer un commentaire