/
javapractice
/
JavaPractice
Обзор
Документация
Войти
/
javapractice
/
JavaPractice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
develop
ApachePOI/src/main/java/utils/AllocationsDB.java
231 строка
10 KB
Zexa91x0
Apache POI - PriceGetterT2
30 май 2021, 20:52
30 май 2021, 20:52
258db9c
Код
Авторство
О чём код?
package utils; import entities.*; import org.apache.poi.ss.usermodel.Sheet; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class AllocationsDB { private Set<Allocation> allocations = new TreeSet<>(); public AllocationsDB(Sheet sheet, int dataSize) { for (int i = 1; i < dataSize; i++) { // получить владельца из XLSx String ownerString = sheet.getRow(i).getCell(0).getStringCellValue(); // получить Position из XLSx String position = sheet.getRow(i).getCell(1).getStringCellValue(); // получить Department из XLSx String department = sheet.getRow(i).getCell(2).getStringCellValue(); // получить Functional из XLSx String functional = sheet.getRow(i).getCell(3).getStringCellValue(); // получить номер из XLSx Long cellNumber = ((Double) sheet.getRow(i).getCell(4).getNumericCellValue()).longValue(); // получить Amount из XLSx Double amountString = sheet.getRow(i).getCell(5).getNumericCellValue(); // получить оператора из XLSx String operatorString = sheet.getRow(i).getCell(6).getStringCellValue(); // получить общую сумму из XLSx Long totalPriceString = ((Double) sheet.getRow(i).getCell(7).getNumericCellValue()).longValue(); // получить лимит из XLSx Long limitString = ((Double) sheet.getRow(i).getCell(8).getNumericCellValue()).longValue(); // получить перерасход String overdraftString = sheet.getRow(i).getCell(9).getStringCellValue(); // получить тип из XLSx String cellTypeString; try { cellTypeString = sheet.getRow(i).getCell(10).getStringCellValue(); } catch (NullPointerException e) { cellTypeString = "tempData"; } // получить комментарий из XLSx String cellCommentString; try { cellCommentString = sheet.getRow(i).getCell(11).getStringCellValue(); } catch (NullPointerException e) { cellCommentString = "tempData"; } // получить номер пластика из XLSx String ICCDString = sheet.getRow(i).getCell(12).getStringCellValue(); String[] ownerStrings = ownerString.split("; "); List<Owner> owners = new ArrayList<>(); for (String string : ownerStrings) { owners.add(new Owner( "idTemp", "LastNameTemp", "LastNameTemp", string, new Department(department, functional, position), "emailTemp", limitString.intValue() ) ); } CellPhone cellPhone = new CellPhone(cellNumber, operatorString, (cellTypeString.equals("Телефон") || cellTypeString.equals("")) ? (TypeSIM.CELL_PHONE) : (TypeSIM.MODEM), true, cellCommentString, ICCDString); Allocation allocation = new Allocation( owners , cellPhone, amountString); this.allocations.add(allocation); } } public Set<Allocation> getAllocations() { return allocations; } public Stream<Allocation> getAllocationStream() { return this.allocations.stream(); } public List<CellPhone> getNumbersListForOwner(Owner owner) { return this.allocations.stream().filter(a -> a.getOwnersList().stream().filter(owner1 -> owner1.getComment().equals(owner.getComment())).count() > 0) .collect(Collectors.toList()).stream().map(Allocation::getCellPhone).collect(Collectors.toList()); } public List<Owner> getOwnersListForNumber(CellPhone cellPhone) { Optional<Allocation> allocationOptional = this.allocations.stream().filter(a -> a.getCellPhone().equals(cellPhone)) .findFirst(); return allocationOptional.isPresent() ? allocationOptional.get().getOwnersList() : Collections.emptyList(); } public List<Allocation> getAllocationsForOperator(String operator) { return allocations.stream().filter(allocation -> allocation.getCellPhone().getOperator().equals(operator)) .collect(Collectors.toList()); } public Map<Long, Allocation> getAllocationMap(Sheet sheet, int dataSize) { Map<Long, Allocation> allocationMap = new HashMap<>(); for (int i = 1; i < dataSize; i++) { // получить владельца из XLSx String ownerString = sheet.getRow(i).getCell(0).getStringCellValue(); // получить Position из XLSx String position = sheet.getRow(i).getCell(1).getStringCellValue(); // получить Department из XLSx String department = sheet.getRow(i).getCell(2).getStringCellValue(); // получить Functional из XLSx String functional = sheet.getRow(i).getCell(3).getStringCellValue(); // получить номер из XLSx Long cellNumber = ((Double) sheet.getRow(i).getCell(4).getNumericCellValue()).longValue(); // получить Amount из XLSx Double amountString = sheet.getRow(i).getCell(5).getNumericCellValue(); // получить оператора из XLSx String operatorString = sheet.getRow(i).getCell(6).getStringCellValue(); // получить общую сумму из XLSx Long totalPriceString = ((Double) sheet.getRow(i).getCell(7).getNumericCellValue()).longValue(); // получить лимит из XLSx Long limitString = ((Double) sheet.getRow(i).getCell(8).getNumericCellValue()).longValue(); // получить перерасход String overdraftString = sheet.getRow(i).getCell(9).getStringCellValue(); // получить тип из XLSx String cellTypeString; try { cellTypeString = sheet.getRow(i).getCell(10).getStringCellValue(); } catch (NullPointerException e) { cellTypeString = "tempData"; } // получить комментарий из XLSx String cellCommentString; try { cellCommentString = sheet.getRow(i).getCell(11).getStringCellValue(); } catch (NullPointerException e) { cellCommentString = "tempData"; } // получить номер пластика из XLSx String ICCDString = sheet.getRow(i).getCell(12).getStringCellValue(); Owner owner = new Owner("idTemp", "LastNameTemp", "LastNameTemp", ownerString, new Department(department, functional, position), "emailTemp", limitString.intValue() ); CellPhone cellPhone = new CellPhone(cellNumber, operatorString, (cellTypeString.equals("Телефон") || cellTypeString.equals("")) ? (TypeSIM.CELL_PHONE) : (TypeSIM.MODEM), true, cellCommentString, ICCDString); Allocation allocation = new Allocation( new ArrayList<>(List.of(owner)) , cellPhone, amountString); allocationMap.put(cellPhone.getNumber(), allocation); } return allocationMap; } public List<CellPhone> getNumbersListForDepartment(Department department){ return this.allocations.stream().filter(a -> a.getOwnersList().stream().filter(owner1 -> owner1.getDepartment().equals(department)).count() > 0) .collect(Collectors.toList()).stream().map(Allocation::getCellPhone).collect(Collectors.toList()); } public AllocationsDB printAllocations() { allocations.forEach(allocation -> { System.out.printf("%s %g%n", allocation.getCellPhone(), allocation.getAmount()); allocation.getOwnersList().forEach(owner -> System.out.printf("\t---%s%n", owner)); }); return this; } public AllocationsDB printAllocations(CellPhone cellPhone) { System.out.printf("%s :%n", cellPhone.getNumber()); getOwnersListForNumber(cellPhone).forEach(owner -> System.out.printf("\t---%s%n", owner)); return this; } public AllocationsDB printAllocations(String operator) { System.out.printf("%s :%n", operator); getAllocationsForOperator(operator).forEach(allocation -> { System.out.printf("%s :%n", allocation.getCellPhone()); allocation.getOwnersList().forEach(owner -> System.out.printf("\t---%s%n", owner)); }); return this; } public void setPriceForNumber(Long number, Double price) { allocations.stream() .filter(allocation -> allocation.getCellPhone().getNumber().equals(number)) .findFirst().get().setAmount(price); } @Override public String toString() { return "Allocations{" + "allocations=" + allocations + '}'; } }