Java abstract to handle two objects, like the same in a tree

My purpose is to program the file system using folder objects and file objects, and then use the file system object to manage two objects in the same tree. My problem is that I cannot figure out how to treat both objects as one and the same.

The assignment says: "You may find it useful to have an abstract class that inherits the file and folder so that you can relate to them the same way." But I get errors all the time.

import java.util.*; public class FileSys { private Node firstFolder; public void newFolder(String loc) { if (firstFolder == null) { // If there are no folders Node folder = new Folder(loc); firstFolder = folder; } else { // If there are folders String s = loc; String[] folders = s.split("\\\\"); // Each file/folder name is put into an array boolean found; // Flag if found Node current = firstFolder; //Sets the first folder to the current int n = 0; while (folders.length - 1 > n) { // To find the folder being added to int i = 0; found = false; // Resets flag while (current.size > i) { // To search through the names of the folders if (current.next[i].name.equalsIgnoreCase(folders[n])) { current = current.next[i]; found = true; // Raises flag break; } i++; } if ( !found) // incomplete. Add Exception. System.out.println("ERROR"); n++; } Node folder = new Folder(folders[folders.length - 1]); current.next[current.size] = folder; } } abstract class Node { String name; Node(String name) { this.name = name; } } private class File extends Node { String data; File(String nm, String data) { super(nm); this.data = data; } } private class Folder extends Node { private static final int ARRAYSIZE = 20; // default array size private int size = 0; private Node[] next = new Node[ARRAYSIZE]; public Folder(String nm) { super(nm); next[0] = null; } } // Main method omitted } 

I appreciate any help in the right direction! I feel that this is a very simple mistake, but I lack experience with objects and abstracts to find out what happened. I tried casting, but this leads to more errors at runtime. Thanks!

Edit:

  FileSys.java:55: error: cannot find symbol while(current.size > i) ^ symbol: variable size location: variable current of type FileSys.Node FileSys.java:57: error: cannot find symbol if(current.next[i].name.equalsIgnoreCase(folders[n])) ^ symbol: variable next location: variable current of type FileSys.Node FileSys.java:59: error: cannot find symbol current = current.next[i]; ^ symbol: variable next location: variable current of type FileSys.Node FileSys.java:76: error: cannot find symbol current.next[current.size] = folder; ^ symbol: variable next location: variable current of type FileSys.Node FileSys.java:76: error: cannot find symbol current.next[current.size] = folder; ^ symbol: variable size location: variable current of type FileSys.Node 5 errors 
+5
source share
3 answers

When a variable is declared as a class, it can only be called using the methods defined for this class.

 Node n = new Folder(); n.name; //Fine, all nodes have the name attribute n.next; //Not fine, nodes do not have a next attribute 

Decision . The casting type is a way to tell the compiler: “I know this object will be of this type at runtime.”

 Folder f = (Folder) n; //We are telling the compiler that n is a Folder f.next; //Fine, because folders have the next attribute 

There is one last problem: we need to distinguish between folders and files. We can use the instanceof operator.

 if(n instanceof Folder){ doFolderStuff(); }else if (n instanceof File){ doFileStuff() } 
+1
source

Here you do current.size and current.next . Both size and next are private in the Folder class. This means that you cannot access them using instances, they are private to the class and must be used inside the class.

So, to access them, use getters defined using the public access specifier in your class, for example

 public int getSize() { //return size; } 

Hope this helps.

0
source

As for the organization, you might want to unify object contracts, which you find similar:

 public interface FSFilter{ public boolean accept(FSNode node); } public interface FSNode { public String getPath(); public FSNode getParent(): public Volume getVolume(); public boolean isFile(); public boolean isFolder(); public boolean isVolume(); public boolean exists(); public boolean delete(); public List<FSNode> getSubNodes(boolean recursive, int depth, FSFilter filter); public int deleteSubNodes(boolean recursive, int depth, FSFilter filter); public long getSize(boolean recursive, int depth, FSFilter filter); } 

The above methods with various combinations of parameters would provide most of the operations that may be required from the file system tree. To add certain functions to certain types of file system objects, you can provide an implementation for each of them. For example, the Volume class may have an additional diskUsage() method, and the File class may have getInputStream() , etc.

 public class File implements FSNode { //... implementation...//} public class Folder implements FSNode { //... implementation...//} public class Volume implements FSNode { //... implementation...//} 

Then you can put all the utility logic that applies to all of them as a static function without saving, for example, a recursive list of files, deletion, creation and modification. Since these operations usually concern basic OS calls. These will be the equivalent of OS commands, such as ls , rm , mount , etc., in your program. The above implementations will call these utility functions internally. In addition, most of the complex file system code will be located here:

 public final class FSUtil{ public static List<FSNode> list(boolean recursive, int depth, FSFilter filter){//... logic...//} public static long byteSize(FSNode node, boolean recursive, depth, FSFilter filter){//... logic...//} public static boolean delete(FSNode node,boolean recursive, FSFilter filter){//... logic...//} ... and more ... } 

For reference, you can look at existing implementations such as apache.commons.io .

0
source

Source: https://habr.com/ru/post/1205004/


All Articles