-3
import java.util.ArrayList;

public class Folder extends AbstractFile
{
//Replace previous ArrayLists with a single
//ArrayList of AbstractFile references
private ArrayList<AbstractFile> files = new ArrayList();
       AbstractFile abstractfile;
/**
 * Constructor for objects of class Folder
 */
public Folder(String name)
{
    super();
    this.name = name;
}
// replace previous add methods
// with a single add(AbstractFile fileObject) method
public boolean add(AbstractFile fileObject)
{
    return files.add(fileObject);
}
@Override
public int size()
{
    int size =0; // size holds the running total
    for (AbstractFile file : files){ // for each AbsFile ref
       size+=file.size(); //call size() and update the running total
    }
    return size; // return the final value
}


@Override
public int getNumFiles(){
    int numFiles = 0;
    for(AbstractFile file: files)
    {
        numFiles += file.getNumFiles();


    }

    return numFiles;// default value
}
@Override
public int getNumFolders(){
    int numFolders = 0;

    // for(AbstractFile file: files)
    // {
        // if(file instanceof Folder)
        // {
        // numFolders += file.getNumFolders();
    // }

    // }
   // return numFolders;// default value
    for (Object e : files)
    {
        if (e instanceof Folder)
        {
             numFolders += e.getNumFolders();
        }
    }
      return numFolders;// default value
}
@Override
public AbstractFile find(String name){
    //TODO Later - not in mini assignment
    return null;
}
}

AbstractFile Class

public abstract class AbstractFile
{
// instance variables -
String name;

public abstract int size();
public abstract int getNumFiles();
public abstract int getNumFolders();
public abstract AbstractFile find(String name);

public String getName(){
    return name;
}

}

FileSystem Class- for testing

public class FileSystem
{
    public static void main(String[] args)
    {
        FileSystem fileSystem = new FileSystem();
        fileSystem.fileTest1();

    }

    public void fileTest1(){

        Folder documents = new Folder("Documents");
        Folder music = new Folder("Music");
        Folder photos = new Folder("Photos");
        documents.add(music);
        documents.add(photos);
        File assign1  = new File("assign1.doc");
        documents.add(assign1);
        Folder dylan = new Folder("Dylan");
        music.add(dylan);
        Folder band = new Folder("The Band");
        music.add(band);
        File family = new File("family.jpg");
        photos.add(family);
        File tambourine = new File("tambourine.mp3");
        dylan.add(tambourine);
        File dixie = new File("dixie.mp3");
        band.add(dixie);
        File weight = new File("weight.mp3");
        band.add(weight);

        String contents1 = ("Hey, mister, can you tell me "); 
        String contents2 = ("Hey Mr Tambourine Man");
        String contents3 = ("The night they drove old dixie down");
        String contents4 = ("fee fi fo fum");

        weight.setContents(contents1); // add contents to each File
        tambourine.setContents(contents2);
        dixie.setContents(contents3);
        assign1.setContents(contents4);

        //********test for size()****************
        int expected  = contents1.length() + contents2.length() + contents3.length() + contents4.length();
        int result = documents.size();

        if(result==expected){ // test fro equality
            System.out.println("size() works");

        }else{
            System.out.println("size() doesn't work");
        }

        //*****************************************

        //*****************test for getNumFiles()******************

        expected =5;// what value should expected be set to?
        result = documents.getNumFiles();

        if(result==expected){ // test fro equality
            System.out.println("NumFiles() works");

        }else{
            System.out.println("NumFiles() doesn't work");

        }

        //output the results of the test for equality

        // **************************************

        //*****************test for getNumFiles()******************

        expected = 5; // what value should expected be set to?
        result = documents.getNumFolders();  



        if(result==expected){ // test fro equality
            System.out.println("NumFolder() works");

        }else{
            System.out.println("NumFolder() doesn't work");
            System.out.printf("%d",result);
        }

        // **************************************


     }
  }

File Class

  public class File extends AbstractFile
   {
    private String contents;
/**
 * Constructor for objects of class File
 */
public File(String name)
{
    super();
    this.name = name;
}

public String getContents(){
    return contents;
}

public void setContents(String contents){
   this.contents = contents;
}

@Override
public int size()
{
    if(contents==null){ //handle situation where contents may not have been set
       return 0; 
    }
    return contents.length();
}
@Override
public int getNumFiles(){
    return 1; // a File object just returns 1 (it contains one File - itself)
}
@Override
public int getNumFolders(){
    //TODO
    return 0;
}
@Override
public AbstractFile find(String name){
    //TODO Later - not in mini assignment
    return null;
}

}

Hi, So Basically I'm having trouble with the getNumFolders(method) I have a test class created with 5 folders and these are stored in an ArrayList of type AbstractFile called files. I am trying to loop through this ArrayList to count the folders, bare in mind this ArrayList also contains files, so they must not be counted. I would appreciate any help. Thanks so much!

John
  • 43
  • 1
  • 7

1 Answers1

0

Simply if you want to count the number of Folder instances, then below should do the work.

public int getNumFolders() {

int numFolders = 0;

for(AbstractFile file: files) {
    if(file instanceof Folder) {
        numFolders++;
   }
}
 return numFolders;
}

and expectedValue should be 2 here accroding to your test case. in fileTest1() method of your test case,

expected = 2; // this should be two because there are two folders called music and photos in documents folder
result = documents.getNumFolders();
ThilankaD
  • 1,021
  • 1
  • 13
  • 24