Тоже не понимаю, из-за чего решение не принимается.
package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader filenameReader = new BufferedReader(new InputStreamReader(System.in));
String filename = filenameReader.readLine();
filenameReader.close();
if (args.length == 0) {
return;
}
List<String> fileContent = readFile(filename);
Map<Integer, String[]> productInfo = parseData(fileContent);
if ("-c".equals(args[0])) {
int nextId = getNextId(productInfo);
addProduct(filename, nextId, args[1], args[2], args[3]);
} else if ("-u".equals(args[0])) {
updateProduct(fileContent,
productInfo,
getId(args),
getProductName(args),
getPrice(args),
getQuantity(args));
writeToFile(filename, fileContent);
} else if ("-d".equals(args[0])) {
deleteProduct(fileContent, productInfo, getId(args));
writeToFile(filename, fileContent);
}
}
private static List<String> readFile(String filename) throws IOException {
BufferedReader fileReader = new BufferedReader(new FileReader(filename));
List<String> content = new ArrayList<>();
String line;
while ((line = fileReader.readLine()) != null) {
content.add(line);
}
fileReader.close();
return content;
}
private static Map<Integer, String[]> parseData(List<String> content) {
Map<Integer, String[]> parsedData = new HashMap<>();
for (String line : content)
parsedData.putAll(Objects.requireNonNull(parseLine(line)));
return parsedData;
}
private static Map<Integer, String[]> parseLine(String line) {
Matcher matcher = Pattern.compile("(?<id>.{8})(?<productName>.{30})(?<price>.{8})(?<quantity>.{4})").matcher(line);
if (matcher.find()) {
return Collections.singletonMap(
Integer.valueOf(matcher.group("id").trim()),
new String[]{
matcher.group("productName"),
matcher.group("quantity"),
matcher.group("price")});
}
return null;
}
private static int getId(String[] line) {
return Integer.valueOf(line[1]);
}
private static String getProductName(String[] line) {
int i = 2;
StringBuilder stringBuilder = new StringBuilder(line[i]);
while (++i < line.length - 2)
stringBuilder.append(" ").append(line[i]);
return String.valueOf(stringBuilder);
}
private static double getPrice(String[] line) {
return Double.valueOf(line[line.length - 2]);
}
private static int getQuantity(String[] line) {
return Integer.valueOf(line[line.length - 1]);
}
private static String wrapWithSpaces(String text, int maxLength) {
if (text.length() > maxLength) {
return text.substring(0, maxLength);
}
StringBuilder stringBuilder = new StringBuilder(text);
while (stringBuilder.length() < maxLength) {
stringBuilder.append(" ");
}
return String.valueOf(stringBuilder);
}
private static String wrapId(String id) {
return wrapWithSpaces(id, 8);
}
private static String wrapId(int id) {
return wrapId(String.valueOf(id));
}
private static String wrapProductName(String productName) {
return wrapWithSpaces(productName, 30);
}
private static String wrapPrice(String price) {
return wrapWithSpaces(price, 8);
}
private static String wrapPrice(Double price) {
return wrapPrice(String.valueOf(price));
}
private static String wrapQuantity(String quantity) {
return wrapWithSpaces(quantity, 4);
}
private static String wrapQuantity(int quantity) {
return wrapQuantity(String.valueOf(quantity));
}
private static int getNextId(Map<Integer, String[]> parsedData) {
return Collections.max(parsedData.keySet()) + 1;
}
private static void addProduct(String filename, int id,
String productName, String price, String quantity) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true));
writer.newLine();
String newProduct = wrapId(id)
+ wrapProductName(productName)
+ wrapPrice(price)
+ wrapQuantity(quantity);
writer.write(newProduct);
writer.close();
}
private static void updateProduct(List<String> fileContent, Map<Integer, String[]> productInfo,
int id, String productName, double price, int quantity) {
String updatedLine = wrapId(id)
+ wrapProductName(productName)
+ wrapPrice(price)
+ wrapQuantity(quantity);
productInfo.putAll(Objects.requireNonNull(parseLine(updatedLine)));
for (int i = 0; i < fileContent.size(); i++) {
String line = fileContent.get(i);
if (Objects.requireNonNull(parseLine(line)).keySet().contains(id)) {
fileContent.set(i, updatedLine);
break;
}
}
}
private static void deleteProduct(List<String> fileContent, Map<Integer, String[]> productInfo, int id) {
productInfo.remove(id);
fileContent.stream().filter(line -> Objects.requireNonNull(parseLine(line)).keySet().contains(id)).findFirst().ifPresent(fileContent::remove);
}
private static void writeToFile(String filename, List<String> fileContent) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (String line : fileContent) {
writer.write(line);
writer.newLine();
}
writer.close();
}
}