From b7b419dbb7e1647f8f592f330aabe5c4cae0cb04 Mon Sep 17 00:00:00 2001 From: CAMOPKAH Date: Sun, 23 Feb 2020 11:23:30 +0300 Subject: [PATCH] Homework 3 from Bimaev O.I. --- task3/src/main/java/FileUtility.java | 161 ++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 4 deletions(-) diff --git a/task3/src/main/java/FileUtility.java b/task3/src/main/java/FileUtility.java index a6d8ef6..83c465b 100644 --- a/task3/src/main/java/FileUtility.java +++ b/task3/src/main/java/FileUtility.java @@ -1,5 +1,7 @@ import java.io.*; +import java.sql.Time; import java.util.*; +import java.time.LocalDate; public class FileUtility { @@ -16,7 +18,42 @@ public class FileUtility { * 5 2 4 1 3 */ public void sortEvenElements(File in, File out) { - //TODO + + ArrayList ArrInt = new ArrayList<>(); + ArrayList ArrPos = new ArrayList<>(); + ArrayList ArrMod = new ArrayList<>(); + + try { + Scanner sc = new Scanner(in); + int FirstNum= Integer.parseInt(sc.nextLine()); //Видимо для получение общего кол-ва элементов в массиве + + while (sc.hasNext()) { + int i=sc.nextInt(); + ArrInt.add(i); + //Если значение чётное, записываем в массив с чётными + if (i%2==0) { + ArrMod.add(i); + ArrPos.add(ArrInt.size()-1); + } + + } + + ArrMod.sort(Comparator.naturalOrder()); //Сортировка чётных значений + + //Возвращаем на место с отсортированными значениями + for (int i = 0; i < ArrMod.size(); i++) ArrInt.set(ArrPos.get(i), ArrMod.get(i)); + + //Записываем в файл через пробел + FileWriter fW = new FileWriter(out); + for (Integer I : ArrInt) fW.write(I.toString() + " "); + fW.flush(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + } @@ -30,9 +67,69 @@ public void sortEvenElements(File in, File out) { * для каждого пользователя */ + //Генерируем пароль + public String passGeneration(){ + int asciiForLoA = 97; + int asciiCount = 25; + int asciiForHiA = 65; + char[] chars = {'*','!','%'}; + + StringBuilder newPass = new StringBuilder(); + //Находим длину + int len = (int) (Math.random() * 5)+7; + int rndC = (int)( Math.random()*3); + for (int i = 0; i < len-1; i++) { + //выбираем набор 1 из 4х + int indChars=Math.floorMod(rndC,4); //(int)( Math.random()*3); + rndC++; + + if (indChars==0) { + newPass.append( chars[(int)(Math.random()*2)] ); + } + else if (indChars==1) { + newPass.append((char)((int)(Math.random()*asciiCount) +asciiForLoA)); + } + else if (indChars==2) { + newPass.append((char)((int)(Math.random()*asciiCount) +asciiForHiA)); + } + else if (indChars==3) { + + newPass.append( String.valueOf((int)(Math.random()*9)) ); + } + + + } + + return newPass.toString(); + } + //Добаволяем пароль если его нет + public String GetIsNotExistPass(ArrayList Arr){ + + String newPass=passGeneration(); + while (Arr.indexOf(newPass)>-1) { + newPass=passGeneration(); + } + Arr.add(newPass); + return newPass; + } + public void passwordGen(File in, File out) { - //TODO + ArrayList passArr= new ArrayList<>(); + try { + Scanner sc = new Scanner(in);//Читаем + FileWriter fW = new FileWriter(out); //Записываем + while (sc.hasNext()) + { + String pass = GetIsNotExistPass(passArr); //Генерируем новый уникальный пароль + fW.write(sc.nextLine().trim() + " " + pass+"\n"); //Сохраняем в файл + } + fW.flush();//Скидываем буфер + } + + catch (IOException e) { + e.printStackTrace(); + } } /* @@ -41,6 +138,45 @@ public void passwordGen(File in, File out) { * */ public void appender(File file, List records) { + try { + //Считываем то, что есть в файле; + ArrayList arr= new ArrayList<>(); + Scanner sc = new Scanner(file);//Читаем + while (sc.hasNext()) + { + arr.add(sc.nextLine()); + } + + boolean Test=true; + int ln=arr.size()-1; + for (int i = records.size() -1; i >-1 ; i--) { + + if (!arr.get(ln).equals(records.get(i))) { + Test=false; + break; + } + ln--; + } + sc.close(); + if (Test) {return; } + + FileWriter fW = new FileWriter(file); + for (int i = 0; i < records.size(); i++) { + arr.add(records.get(i)); + + } + for (int i = 0; i < arr.size(); i++) { + fW.write(arr.get(i)+"\n"); + + } + fW.flush(); + + + + } catch (IOException e) { + e.printStackTrace(); + } + } /* @@ -53,8 +189,25 @@ public void appender(File file, List records) { * Альтернативное решение: использование очереди или стека * */ public List getNString(String pathToFile, int n) { - //TODO - return null; + ArrayList arr= new ArrayList<>(); + try { + RandomAccessFile rAF = new RandomAccessFile(pathToFile, "r"); + rAF.seek(rAF.length()-80*n); + String ln; + while ( (ln=rAF.readLine())!=null ) + { + arr.add(ln); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + return arr; } + public static void main(String[] args) { + + + } }