Подскажите тестовые данные на решение.
Со всеми своими, что пробовал - работает корректно, валидатор не пропускает.
package com.javarush.task.task18.task1827;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/*
Прайсы
*/
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
return;
}
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
if (args[0].equals("-c")) {
List<String> currentProducts = getListOfCurrentProducts(fileName);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 1; i < args.length - 2; i++) {
stringBuilder.append(args[i]);
stringBuilder.append(" ");
}
String productName = stringBuilder.toString().trim();
String price = args[args.length - 2];
String quantity = args[args.length - 1];
int productId = getMaxIdFromFile(fileName) + 1;
String productInfo = generateProductInfo(
String.valueOf(productId),
productName,
price,
quantity);
currentProducts.add(productInfo);
setListOfCurrentProducts(fileName, currentProducts);
}
}
private static void setListOfCurrentProducts(String fileName, List<String> currentProducts) throws IOException {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) {
for (String line : currentProducts) {
bufferedWriter.write(line);
bufferedWriter.write(System.lineSeparator());
}
}
}
private static List<String> getListOfCurrentProducts(String fileName) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
List<String> result = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result.add(line);
}
return result;
}
}
private static String generateProductInfo(String productId, String productName, String price, String quantity) {
String formattedProductId = productId.length() < 8 ? String.format("%-8s", productId) : productId.length() == 8 ? productId : productId.substring(0, 8);
String formattedProductName = productName.length() < 30 ? String.format("%-30s", productName) : productName.length() == 30 ? productName : productName.substring(0, 30);
String formattedPrice = price.length() < 8 ? String.format("%-8s", price) : price.length() == 8 ? price : price.substring(0, 8);
String formattedQuantity = quantity.length() < 4 ? String.format("%-4s", quantity) : quantity.length() == 4 ? quantity : quantity.substring(0, 4);
return formattedProductId + formattedProductName + formattedPrice + formattedQuantity;
}
private static int getMaxIdFromFile(String fileName) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
List<String> idList = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
idList.add(line.trim().substring(0, 8).trim());
}
if (idList.isEmpty()) {
return 0;
} else {
return Integer.parseInt(Collections.max(idList));
}
}
}
}