Проверка входных данных
This commit is contained in:
70
src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java
Normal file
70
src/main/java/ru/dmitriymx/corrector1s/Corrector1S.java
Normal file
@@ -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<String, ExcelRecord> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-03
|
||||
*/
|
||||
package ru.dmitriymx.corrector1s;
|
||||
|
||||
public class FormatFileException extends Exception {
|
||||
public FormatFileException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public FormatFileException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user