package sqlconnect;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static String path = "/Users/yanshao/PycharmProjects/untitled2/Books/";
    public static String suffixname(String name){
        if(name.indexOf('.')==-1) return null;
        else return name.substring(name.lastIndexOf(".") + 1);
    }
    public static String getIntroduce(String path){
        path += ".txt";
        StringBuffer ret=new StringBuffer();
        try{
            File file=new File(path);
            Scanner fin=new Scanner(file);
            while(fin.hasNext()){
                ret.append(fin.next()+"\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret.toString();
    }
    public static void main(String[] args){
        try{
            Database.init();
            File file=new File(path);
            File files[]=file.listFiles();
            if(files==null) return;
            ArrayList<File> Type=new ArrayList<File>();
            for(int i=0;i<files.length;i++){
                if(files[i].isDirectory())
                    Type.add(files[i]);
            }
            for(int i=0;i<Type.size();i++){
                File tp[]=Type.get(i).listFiles();
                for(File f:tp){
                    String name=f.getName();
                    if(suffixname(name)==null){
                        //好了,在这里的就是每一本书的信息了
                        Scanner fin=new Scanner(f);
                        String BookID=fin.next();
                        String BookName=fin.next();
                        String Price=fin.next();
                        String Author=fin.next();
                        String Introduce=getIntroduce(f.getAbsolutePath());
                        String typed=String.valueOf(i);
                        ArrayList<String> data=new ArrayList<String>();
                        data.add(BookID);
                        data.add(BookName);
                        data.add(Author);
                        data.add(Price);
                        data.add(Introduce);
                        data.add(typed);
                        Add.insert("Book",data);
                    }
                }
            }
            Database.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
