Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package gen_csv;
  7.  
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.Random;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17.  
  18. /**
  19.  *
  20.  * @author Кристина
  21.  */
  22. public class Main {
  23.  
  24.     /**
  25.      * @param args the command line arguments
  26.      */
  27.     static Random random = new Random(new Date().getTime());
  28.  
  29.     public static void main(String[] args) {
  30.         // TODO code application logic here
  31.         String args0 = "-col";
  32.         String args1 = "500";
  33.         String args2 = "-row";
  34.         String args3 = "9871";
  35.         String args4 = "-len";
  36.         String args5 = "30";
  37.         String args6 = "-out";
  38.         String args7 = "out.csv";
  39.  
  40.         try {
  41.             checkArgs(args0, args1, args2, args3, args4, args5, args6, args7);
  42.             genRandomFile(args1, args3, args5, args7);
  43.  
  44.         }
  45.         catch(IllegalArgumentException event) {
  46.             System.err.println(event.getMessage());
  47.         }
  48.     }
  49.  
  50.     public static void checkArgs(String col, String numberCol, String row, String numberRow, String len, String numberLen, String out, String outfileName){
  51.         Pattern integerPat = Pattern.compile("^[1-9](\\d+)?");
  52.         Pattern filenamePat = Pattern.compile("[\\w\\d]+\\.csv");
  53.         int testLong;
  54.  
  55.         if ((col.equals("-col"))&&(row.equals("-row"))&&(len.equals("-len"))&&(out.equals("-out"))){
  56.             Matcher m = filenamePat.matcher(outfileName);
  57.             boolean b = m.matches();
  58.             if (b==false) fail("gen_csv: неправильно заданно имя выходящего файла!");
  59.             //File outFile = new File(outfileName);
  60.            // if (outFile.exists()) fail("gen_csv: заданный файл уже существует в этой дериктории: " + outfileName);
  61.  
  62.  
  63.             m = integerPat.matcher(numberCol);
  64.             b = m.matches();
  65.             if (b==false) fail("gen_csv: некоректно задано значение аргумента col");
  66.             testLong = Integer.parseInt(numberCol);
  67.             if (!(testLong <= 1000)) fail("gen_csv: максимальное количество столбцов не должно превышать 1000");
  68.  
  69.  
  70.             m = integerPat.matcher(numberRow);
  71.             b = m.matches();
  72.             if (b==false) fail("gen_csv: некоректно задано значение аргумента row");
  73.             testLong = Integer.parseInt(numberRow);
  74.             if (!(testLong <= 1000000)) fail("gen_csv: максимальное количество строк не должно превышать 1000000");
  75.  
  76.  
  77.             m = integerPat.matcher(numberLen);
  78.             b = m.matches();
  79.             if (b==false) fail("gen_csv: некоректно задано значение аргумента len");
  80.             testLong = Integer.parseInt(numberLen);
  81.             if (!(testLong <= 300)) fail("gen_csv: максимальная длина строки не должна превышать 300");
  82.         }
  83.         else fail("gen_csv: некоректно заданы названия аргументов!");
  84.     }
  85.  
  86.     public static void genRandomFile(String numberCol, String numberRow, String numberLen, String outfileName){
  87.         File outFile = new File(outfileName);
  88.         try {
  89.             PrintWriter out = new PrintWriter(new FileWriter(outFile));
  90.              ArrayList<String> types = new ArrayList<String>();
  91.             for (int i = 0; i < Integer.parseInt(numberCol); i++){
  92.                 types.add(i, genRandomType());
  93.                 if (i < Integer.parseInt(numberCol)-1) out.print(genRandomString("20") + " " + types.get(i) + "; ");
  94.                 else out.print(genRandomString("20") + " " + types.get(i));
  95.             }
  96.           /* for (int i = 0; i < Integer.parseInt(numberRow); i++){
  97.                 out.println("");
  98.                 for (int j = 0; j < Integer.parseInt(numberCol); j++){
  99.                     if (types.get(j).equals("Integer")) out.print(genRandomInt() + ";");
  100.                     if(types.get(j).equals("Float")) out.print(genRandomFloat() + ";");
  101.                     if(types.get(j).equals("String")) out.print(genRandomString(numberLen) + ";");
  102.                     if(types.get(j).equals("Date")) out.print(genRandomDate() + ";");
  103.                 }
  104.             }*/
  105.             out.close();
  106.     }
  107.         catch(IOException e){
  108.             System.err.println(e.getMessage());
  109.     }
  110.     }
  111.  
  112.     public static int genRandomInt(){
  113.         int randomInt = random.nextInt();
  114.         return randomInt;
  115.     }
  116.     public static float genRandomFloat(){
  117.         float randomFloat = random.nextFloat()* random.nextInt(500000)-random.nextInt(500000);
  118.         return randomFloat;
  119.     }
  120.     public static String genRandomString(String longStr){
  121.         String str="qwertyuiopasdfghjklzxcvbnm";
  122.         String strOut = "";
  123.         int longString = random.nextInt(Integer.parseInt(longStr));
  124.         int j;
  125.         for(int i = 0; i <= longString; i++){
  126.             j = random.nextInt(str.length());
  127.             strOut += str.charAt(j);
  128.         }
  129.         return strOut;
  130.     }
  131.     public static String genRandomDate(){
  132.         String str = "";
  133.         int year = 1900 +random.nextInt(111);
  134.         int month = 1 + random.nextInt(12);
  135.  
  136.         int day = 1;
  137.         if ((month == 1)|(month == 3)|(month == 5)|(month == 7)|(month == 8)|(month == 10)|(month == 12)){
  138.             day = random.nextInt(31);
  139.         }
  140.         if ((month == 4)|(month == 6)|(month == 9)|(month == 11)){
  141.             day = random.nextInt(30);
  142.         }
  143.         if (month == 2){
  144.             if (year%4==0) day = random.nextInt(29);
  145.             else day = random.nextInt(28);
  146.         }
  147.         if (month <= 9) str += day + ".0" + month + "." + year;
  148.         else str +=day + "." + month + "." + year;
  149.         return str;
  150.     }
  151.     public static String genRandomType(){
  152.         String str = "";
  153.         int randType = random.nextInt(4);
  154.         if (randType == 0) str += "Float";
  155.         if (randType == 1) str += "String";
  156.         if (randType == 2) str += "Integer";
  157.         if (randType == 3) str += "Date";
  158.         return str;
  159.     }
  160.  
  161.     protected static void fail(String msg) throws IllegalArgumentException {
  162.         throw new IllegalArgumentException(msg);
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement