Advertisement
hmelnyk

test

Feb 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. public class FileCSVReader {
  2.  
  3.  
  4. List<Category> listCategory = new ArrayList<>();
  5. List<Brand> listBrand = new ArrayList<>();
  6. private Category category;
  7. private Brand brand;
  8. private Product product;
  9.  
  10.  
  11. public static void main(String[] args) throws IOException {
  12.  
  13. FileCSVReader fileCSVReader = new FileCSVReader();
  14. fileCSVReader.readCSVFile("sample.csv");
  15.  
  16. }
  17.  
  18. public void readCSVFile(String fileName) throws IOException {
  19.  
  20. try (
  21. Reader reader = Files.newBufferedReader(Paths.get(fileName));
  22. CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
  23.  
  24. ) {
  25.  
  26. int rowNumber = 0;
  27. for (CSVRecord csvRecord : csvParser) {
  28. rowNumber++;
  29.  
  30. if (rowNumber >= 7) {
  31. listCategory.add(createCategory(csvRecord));
  32. createBrand(csvRecord);
  33. createProduct(csvRecord);
  34.  
  35. }
  36.  
  37.  
  38. }
  39. System.out.println(listCategory);
  40. }
  41.  
  42. }
  43.  
  44. public Category createCategory(CSVRecord csvRecord) {
  45.  
  46. category.setName(csvRecord.get(3));
  47. category.setNameLocaleUA(csvRecord.get(3));
  48. category.setNameLocaleRU(csvRecord.get(3));
  49. category.setCategoryPageUrl(csvRecord.get(52));
  50. category.setCategoryPageUrlLocaleUA(csvRecord.get(54));
  51. category.setCategoryPageUrlLocaleRU(csvRecord.get(55));
  52. category.setExternalId(csvRecord.get(53));
  53.  
  54. return category;
  55.  
  56. }
  57.  
  58. public Brand createBrand(CSVRecord csvRecord) {
  59. brand.setExternalId(csvRecord.get(48));
  60. brand.setName(csvRecord.get(49));
  61.  
  62. return brand;
  63.  
  64. }
  65.  
  66. public Product createProduct(CSVRecord csvRecord) {
  67.  
  68. product.setBrandExternalId(csvRecord.get(48));
  69. product.setCategoryExternalId(csvRecord.get(53));
  70. product.setEAN(csvRecord.get(10));
  71. product.setImageUrl(csvRecord.get(51));
  72. product.setName(csvRecord.get(50));
  73. product.setProductPageUrl(csvRecord.get(25));
  74. product.setExternalId(csvRecord.get(9));
  75.  
  76. return product;
  77.  
  78. }
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement