diff --git a/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java b/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java new file mode 100644 index 0000000..a341dcb --- /dev/null +++ b/src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java @@ -0,0 +1,70 @@ +package ru.dmitriymx.corrector1s; + +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@NoArgsConstructor +public class Corrector1S { + private class ExcelRecord { + private String name; + private String addition; + private Double data; + } + + @Setter + private File sourceFile; + @Setter + private File targetFile; + @Setter + private double honorarium; + + private Map mapRecords = new HashMap<>(19); // ожидается 19 записей для сохранения + + private String getFileExtension(File file) { + int i = file.getAbsolutePath().lastIndexOf('.'); + if (i > 0) { + return file.getAbsolutePath().substring(i+1); + } else { + return ""; + } + } + + public void check() throws Exception { + if (honorarium == 0.0d) throw new AssertionError("Honorarium not be 0.0%!"); + + if (sourceFile == null) throw new AssertionError("Source file not be null!"); + else if (!sourceFile.exists()) throw new FileNotFoundException(sourceFile.getAbsolutePath()); + else if (!getFileExtension(sourceFile).equalsIgnoreCase("xls") + && !getFileExtension(sourceFile).equalsIgnoreCase("xlsx")) throw new AssertionError("Source file must be XLS/XLSX format"); + + if (targetFile == null) throw new AssertionError("Target file not be null!"); + + Workbook workbook = WorkbookFactory.create(sourceFile); + Sheet sheet = workbook.getSheetAt(0); + try { + Cell cell = sheet.getRow(6).getCell(9); + if (!cell.getStringCellValue().equals("Больничный за счет работодателя")) { + throw new FormatFileException(); + } + + sheet.getRow(7).getCell(9); + sheet.getRow(8).getCell(9); + sheet.getRow(9).getCell(9); + } catch (FormatFileException e) { + throw e; + } catch (Exception e) { + throw new FormatFileException(e); + } + } +} diff --git a/src/main/java/ru/dmitriymx/corrector1s/FormatFileException.java b/src/main/java/ru/dmitriymx/corrector1s/FormatFileException.java new file mode 100644 index 0000000..f274d8a --- /dev/null +++ b/src/main/java/ru/dmitriymx/corrector1s/FormatFileException.java @@ -0,0 +1,15 @@ +/* + * DmitriyMX + * 2018-06-03 + */ +package ru.dmitriymx.corrector1s; + +public class FormatFileException extends Exception { + public FormatFileException() { + super(); + } + + public FormatFileException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/ru/dmitriymx/corrector1s/Main.java b/src/main/java/ru/dmitriymx/corrector1s/Main.java index 7652696..3eabc56 100644 --- a/src/main/java/ru/dmitriymx/corrector1s/Main.java +++ b/src/main/java/ru/dmitriymx/corrector1s/Main.java @@ -73,6 +73,12 @@ public class Main { log.debug("Source file: {} (exists: {})", sourceFile.getAbsolutePath(), Files.exists(sourceFile.toPath())); log.debug("Target file: {}", targetFile.getAbsolutePath()); log.debug("Honorarium: {}%", optionSet.valueOf("fee")); + + Corrector1S corrector1S = new Corrector1S(); + corrector1S.setSourceFile(sourceFile); + corrector1S.setTargetFile(targetFile); + corrector1S.setHonorarium((Double) optionSet.valueOf("fee")); + corrector1S.check(); } } }