Advertisement
heavenriver

2012_02_22.java (ex. 2)

Jan 9th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. // Esame del 22 febbraio 2012, esercizio 2
  2. // Ricordarsi di specificare sempre, nei commenti al codice, l'utilità di ogni metodo.
  3.  
  4. class FSInstance
  5.     {
  6.     String name;
  7.     public FSInstance(String name);
  8.     public String getName();
  9.     }
  10.  
  11. class Folder extends FSInstance
  12.     {
  13.     LinkedList<FSInstance> content;
  14.     int children;
  15.     public Folder(String name);
  16.     public List<FSInstance> getChildren();
  17.     public void add(FSI child);
  18.     public void remove(String name);
  19.     }
  20.  
  21. class File extends FSInstance
  22.     {
  23.     long size;
  24.     public File(String name, long size);
  25.     public long getSize();
  26.     }
  27.  
  28. class Link extends FSInstance
  29.     {
  30.     String path;
  31.     public Link(String name, String path);
  32.     public String getPath();
  33.     }
  34.  
  35. class FileSystem
  36.     {
  37.     FSInstance root;
  38.     public FSInstance getRoot();
  39.     }
  40.  
  41. public List<String> search(String name)
  42.     {
  43.     List<String> out = new LinkedList<String>();
  44.     String route = "";
  45.     search(name, root, out, route);
  46.     return out;
  47.     }
  48.  
  49. public void search(String name, FSInstance elem, List<String> out, String route)
  50.     {
  51.     if(elem == null || elem instanceof Link) return;
  52.     route.concat(elem.getName());
  53.     if(elem instanceof File)
  54.         {
  55.         if(elem.getName().equals(name)) out.add(route);
  56.         return;
  57.         }
  58.     else if(elem instanceof Folder)
  59.         {
  60.         for(FSInstance child : elem.getChildren())
  61.             search(name, child, out, route);
  62.         }
  63.     }
  64.  
  65. public long getSize(FSInstance elem)
  66.     {
  67.     long totalLength = 0;
  68.     getSize(elem, totalLength);
  69.     return totalLength;
  70.     }
  71.  
  72. public void getSize(FSInstance elem, long totalSize)
  73.     {
  74.     if(elem == null || elem instanceof Link) return;
  75.     else if(elem instanceof File) totalSize += elem.getSize();
  76.     else if(elem instanceof Folder)
  77.         {
  78.         for(FSInstance child : elem.getChildren())
  79.             getSize(elem, totalSize);
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement