/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Assignment;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter; 


 class CompositPatternTest {
    public static void main(String []as){
        Files[] file1={new Files("File_1.txt",01),new Files("File_2.txt",02)};
        Directory subDr=new Directory(file1,"C:/Users/MA/Desktop/Esubalew Assignment/Lab/SecondLab/src/Assignment/TestDir/SubDir");
        Files[] file2={new Files("File1.txt",1),new Files("File2.txt",2),new Files("File3.txt",3)};
        Directory Dr=new Directory(subDr, file2, "C:/Users/MA/Desktop/Esubalew Assignment/Lab/SecondLab/src/Assignment/TestDir");
       
   
        System.out.println(Dr);
        Files[] f1=Dr.getFile();
        if(f1!=null)
        for (int k = 0; k < f1.length; k++){
          System.out.println("File name: "+ f1[k].getFileName());
          String path=Dr.getFilePath();
          try{
              f1[k].openFile(path);
              f1[k].closeFile(path);
              f1[k].renameFile(path);
          }
          catch(FileNotFoundException ex){
              System.out.println("File not found.");
          }
           System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
        }
        System.out.println("\n~~~~Sub-directory~~~~~");
        Directory d2=Dr.getDr();
        System.out.println(d2);
        if(d2!=null){
            Files[] f2=d2.getFile(); 
            if(f2!=null)
            for (int k = 0; k < f2.length; k++){
              System.out.println("File name: "+ f2[k].getFileName());
              String path=subDr.getFilePath();
              try{
                  f2[k].openFile(path);
                  f2[k].closeFile(path);
                  f2[k].renameFile(path);
              }
              catch(FileNotFoundException ex){
                  System.out.println("File not found.");
              }
               System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
            }
       }
       System.out.println();
    }
}



class Files{
    private String fileName;
    private int fileNo;
    private Formatter output; 
    
    
    public Files(String name, int no){
        fileName=name;
        fileNo=no;
    }

    public String getFileName() {
        return fileName;
    }

    public int getFileNo() {
        return fileNo;
    }
    
    public void openFile(String path) throws FileNotFoundException{
        output=new Formatter(path+"/"+fileName);
        System.out.println(fileName+" is opened. ");
    }
    public void closeFile(String path) throws FileNotFoundException{
        if(output!=null){
            output.close();
        }
        System.out.println(fileName+" is closed. ");
    }
    public void renameFile(String path) throws FileNotFoundException{
        File oldFile=new File(path+"/"+fileName);
        oldFile.renameTo(new File(path+"/"+""+fileName));
        System.out.println(fileName+" is renamed to _"+fileName);
    }    
}





class Directory{
    private Directory dr;
    private String filePath;
    private Files[] file;

    public Directory(Directory dr,Files[] f, String path) {
        this(f,path);
        this.dr=dr;
    }

    public Directory(Files[] f, String path) {
        file=f;
        filePath=path;
    }

    public Directory getDr() {
        return dr;
    }

    public String getFilePath() {
        return filePath;
    }

    public Files[] getFile() {
        return file;
    }
    public String toString() {
       return  "Directory Path: "+filePath ;
   }
    
}




