Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. package io.github.jacekszmidt.service;
  2.  
  3. import io.github.jacekszmidt.model.Laptop;
  4. import io.github.jacekszmidt.model.PersonalComputer;
  5. import io.github.jacekszmidt.model.User;
  6. import org.apache.poi.ss.usermodel.*;
  7. import org.apache.poi.xssf.usermodel.XSSFFont;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.util.List;
  13.  
  14. public class ExcelComputerOutputWriter implements ComputerOutputWriter {
  15. private static final String EXCEL_EXTENSION = ".xlsx";
  16. private static final String LAP_ENDING = "LAP#";
  17. private static final String PC_ENDING = "PC#";
  18. private final List<String> userHeaders = List.of("Imie", "Nazwisko", "Telefon");
  19. private final List<String> laptopHeaders = List.of("Model", "Numer seryjny", "Inne", "Opis usterki", "Mozna kasowac dane?");
  20. private final List<String> personalComputerHeaders = List.of("CPU", "Plyta Glowna", "RAM", "Zasilacz", "Dysk", "Inne", "Opis usterki", "Mozna kasowac dane?");
  21.  
  22. @Override
  23. public void writeOutput(User user, Laptop laptop) {
  24. Workbook workbook = new XSSFWorkbook();
  25. Sheet sheet = workbook.createSheet("Order Details");
  26.  
  27. Row userHeader = sheet.createRow(0);
  28. for (int i = 0; i < userHeaders.size(); i++) {
  29. Cell cell = userHeader.createCell(i);
  30. cell.setCellValue(userHeaders.get(i));
  31. }
  32.  
  33. Row userRow = sheet.createRow(1);
  34. Cell nameCell = userRow.createCell(0);
  35. nameCell.setCellValue(user.getName());
  36. Cell lastNameCell = userRow.createCell(1);
  37. lastNameCell.setCellValue(user.getLastName());
  38. Cell phoneNumberCell = userRow.createCell(2);
  39. phoneNumberCell.setCellValue(user.getPhoneNumber());
  40.  
  41.  
  42. Row laptopHeader = sheet.createRow(3);
  43. for (int i = 0; i < laptopHeaders.size(); i++) {
  44. Cell cell = laptopHeader.createCell(i);
  45. cell.setCellValue(laptopHeaders.get(i));
  46. }
  47.  
  48. Row laptopRow = sheet.createRow(4);
  49. Cell modelCell = laptopRow.createCell(0);
  50. modelCell.setCellValue(laptop.getModel());
  51. Cell serialNumberCell = laptopRow.createCell(1);
  52. serialNumberCell.setCellValue(laptop.getSerialNumber());
  53. Cell otherInfoCell = laptopRow.createCell(2);
  54. otherInfoCell.setCellValue(laptop.getOtherInfo());
  55. Cell problemDescriptionCell = laptopRow.createCell(3);
  56. problemDescriptionCell.setCellValue(laptop.getProblemDescription());
  57. Cell cleanDataCell = laptopRow.createCell(4);
  58. cleanDataCell.setCellValue(laptop.getCleanData());
  59.  
  60. sheet.autoSizeColumn(0);
  61. sheet.autoSizeColumn(1);
  62. sheet.autoSizeColumn(2);
  63. sheet.autoSizeColumn(3);
  64. sheet.autoSizeColumn(4);
  65.  
  66. try {
  67. FileOutputStream file = new FileOutputStream(LAP_ENDING + user.getLastName() + "_" + (System.currentTimeMillis() / 1000) + EXCEL_EXTENSION);
  68. workbook.write(file);
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. @Override
  75. public void writeOutput(User user, PersonalComputer personalComputer) {
  76. Workbook workbook = new XSSFWorkbook();
  77. Sheet sheet = workbook.createSheet("Order Details");
  78.  
  79. Row userHeader = sheet.createRow(0);
  80. for (int i = 0; i < userHeaders.size(); i++) {
  81. Cell cell = userHeader.createCell(i);
  82. cell.setCellValue(userHeaders.get(i));
  83. }
  84.  
  85. Row userRow = sheet.createRow(1);
  86. Cell nameCell = userRow.createCell(0);
  87. nameCell.setCellValue(user.getName());
  88. Cell lastNameCell = userRow.createCell(1);
  89. lastNameCell.setCellValue(user.getLastName());
  90. Cell phoneNumberCell = userRow.createCell(2);
  91. phoneNumberCell.setCellValue(user.getPhoneNumber());
  92.  
  93.  
  94. Row personalComputerHeader = sheet.createRow(3);
  95. for (int i = 0; i < personalComputerHeaders.size(); i++) {
  96. Cell cell = personalComputerHeader.createCell(i);
  97. cell.setCellValue(personalComputerHeaders.get(i));
  98. }
  99.  
  100. Row personalComputerRow = sheet.createRow(4);
  101. Cell cpuCell = personalComputerRow.createCell(0);
  102. cpuCell.setCellValue(personalComputer.getCpu());
  103. Cell motherBoardCell = personalComputerRow.createCell(1);
  104. motherBoardCell.setCellValue(personalComputer.getMotherBoard());
  105. Cell memoryRamCell = personalComputerRow.createCell(2);
  106. memoryRamCell.setCellValue(personalComputer.getMemoryRam());
  107. Cell psuCell = personalComputerRow.createCell(3);
  108. psuCell.setCellValue(personalComputer.getPsu());
  109. Cell discCell = personalComputerRow.createCell(4);
  110. discCell.setCellValue(personalComputer.getDisc());
  111. Cell otherInfoCell = personalComputerRow.createCell(5);
  112. otherInfoCell.setCellValue(personalComputer.getOtherInfo());
  113. Cell problemDescriptionCell = personalComputerRow.createCell(6);
  114. problemDescriptionCell.setCellValue(personalComputer.getProblemDescription());
  115. Cell cleanDataCell = personalComputerRow.createCell(7);
  116. cleanDataCell.setCellValue(personalComputer.getCleanData());
  117.  
  118.  
  119. sheet.autoSizeColumn(0);
  120. sheet.autoSizeColumn(1);
  121. sheet.autoSizeColumn(2);
  122. sheet.autoSizeColumn(3);
  123. sheet.autoSizeColumn(4);
  124. sheet.autoSizeColumn(5);
  125. sheet.autoSizeColumn(6);
  126. sheet.autoSizeColumn(7);
  127.  
  128. try {
  129. FileOutputStream file = new FileOutputStream(PC_ENDING + user.getLastName() + "_" + (System.currentTimeMillis() / 1000) + EXCEL_EXTENSION);
  130. workbook.write(file);
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement