package com.mavlushechka.notaryautomation; import java.io.FileInputStream; import java.io.FileOutputStream; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.*; public class App { private static final Scanner SCANNER = new Scanner(System.in); public static void main(String[] args) throws Exception { Person deadPerson; LocalDate dateOfDeath; String roleOfDeadPerson; ArrayList heirs = new ArrayList<>(); Person finalHeir; String roleOfFinalHeir; System.out.println("Погибший человек:\n"); deadPerson = createPerson(); System.out.print("Дата смерти (1990-12-31): "); dateOfDeath = LocalDate.parse(SCANNER.next()); System.out.print("Роль в семье (Мать, Отец и т.д.): "); roleOfDeadPerson = SCANNER.next().toLowerCase(); boolean isThereAnotherHeir; System.out.println("\nНаследникики:"); do { System.out.println(); heirs.add(createPerson()); System.out.print("\nЕсть ещё наследники? (Да/Нет) "); isThereAnotherHeir = "Да".equalsIgnoreCase(SCANNER.next()); } while (isThereAnotherHeir); System.out.println("\nВсе наследники:"); for (int i = 0; i < heirs.size(); i++) { System.out.println(i+1 + ". " + heirs.get(i).firstName() + " " + heirs.get(i).secondName() + " " + heirs.get(i).middleName()); } System.out.print("Окончательный наследник (введите число): "); finalHeir = heirs.get(SCANNER.nextInt() - 1); System.out.print("Роль в семье (Сын, Дочь и т.д.): "); roleOfFinalHeir = SCANNER.next().toLowerCase(); updateApplicationOfTheHeirToRenounceTheInheritanceShareDocument(deadPerson, dateOfDeath, roleOfDeadPerson, heirs, finalHeir, roleOfFinalHeir); } private static Person createPerson() { System.out.print("Введите имя: "); String firstName = SCANNER.next().toUpperCase(); System.out.print("Введите фамилию: "); String secondName = SCANNER.next().toUpperCase(); System.out.print("Введите отчество: "); SCANNER.nextLine(); String middleName = SCANNER.nextLine().toUpperCase(); System.out.print("Введите серию паспорта (AA): "); String serial = SCANNER.next().toUpperCase(); if (" ".equals(serial)) { serial = "AA"; } System.out.print("Введите номер паспорта (1234567): "); int number = SCANNER.nextInt(); System.out.print("Введите гражданство человека (Uzbekistan): "); String citizenship = SCANNER.next(); if (" ".equals(citizenship)) { citizenship = "Uzbekistan"; } System.out.print("Введите место выдачи паспорта (XORAZM VILOYATI URGANCH TUMANI IIB): "); SCANNER.nextLine(); String placeOfIssue = SCANNER.nextLine().toUpperCase(); if (" ".equals(placeOfIssue)) { placeOfIssue = "XORAZM VILOYATI URGANCH TUMANI IIB"; } System.out.print("Введите дату выдачи паспорта (1990-12-31): "); LocalDate dateOfIssue = LocalDate.parse(SCANNER.next()); System.out.print("Введите ЖШШИР (12345678901234): "); String personalIdentificationNumberOfThePhysicalPerson = SCANNER.next(); System.out.print("Введите место проживания: "); SCANNER.nextLine(); String placeOfResidence = SCANNER.nextLine(); return new Person(firstName, secondName, middleName, new Passport(serial, number, citizenship, placeOfIssue, dateOfIssue, personalIdentificationNumberOfThePhysicalPerson, placeOfResidence)); } private static void updateApplicationOfTheHeirToRenounceTheInheritanceShareDocument(Person deadPerson, LocalDate dateOfDeath, String roleOfDeadPerson, ArrayList heirs, Person finalHeir, String roleOfFinalHeir) throws Exception { FileInputStream fileInputStream = new FileInputStream("Application of the heir to renounce the inheritance share.docx"); try (XWPFDocument xwpfDocument = new XWPFDocument(OPCPackage.open(fileInputStream))) { List xwpfParagraphList = xwpfDocument.getParagraphs(); for (XWPFParagraph xwpfParagraph : xwpfParagraphList) { for (XWPFRun xwpfRun : xwpfParagraph.getRuns()) { String oldText = xwpfRun.getText(0); if (oldText == null) continue; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); String text = oldText .replace("${dateOfDeath}", dateOfDeath.format(dateTimeFormatter)) .replace("${currentDate}", LocalDate.now().format(dateTimeFormatter) .replace("${relationship1}", roleOfDeadPerson) .replace("${relationship2}", roleOfFinalHeir) .replace("${deadPerson.firstName}", deadPerson.firstName()) .replace("${deadPerson.secondName}", deadPerson.secondName()) .replace("${finalHeir.firstName}", finalHeir.firstName()) .replace("${finalHeir.secondName}", finalHeir.secondName()) .replace("${numberOfArticle}", "Отец".equalsIgnoreCase(roleOfDeadPerson) && "Мать".equalsIgnoreCase(roleOfFinalHeir) ? "1135" : "?")); for (int i = 1; i < heirs.size(); i++) { String personsFullInfo = "${personsFullInfo" + i + "}"; String personsLine = "${personsLine" + (i + 1) + "}"; Person person = heirs.get(i - 1); text = text .replace(personsFullInfo,person.passport().placeOfResidence() + "да\n" + "рўйхатда турувчи " + person.firstName() + " " + person.secondName() + " " + person.middleName() + "\n" + "(" + person.passport().citizenship() + " фуқаросининг биометрик паспорти " + person.passport().serial() + " " + person.passport().number() + ",\n" + person.passport().placeOfIssue() + " томонидан\n" + person.passport().dateOfIssue().format(dateTimeFormatter) + " йилда берилган, ЖШШИР " + person.passport().personalIdentificationNumberOfThePhysicalPerson() + ")\n" + (heirs.size() > i + 1 ? "${personsFullInfo" + (i + 1) + "}" : "")) .replace(personsLine,(i + 1) + "._______________________________________________________________________\n" + (heirs.size() > i + 1 ? "${personsLine" + (i + 2) + "}" : "")); } xwpfRun.setText(text, 0); } } try (FileOutputStream out = new FileOutputStream(finalHeir.firstName() + " " + finalHeir.secondName() + " " + finalHeir.middleName() + ", Меросхўрнинг мерос улушидан воз кечиш ҳақидаги аризаси.docx")) { xwpfDocument.write(out); } } } }