Всем привет! Тестил задачу, всё работает, строку с товаром обновляет.
Но валидатор почему-то не пропускает.
Не могу понять почему?
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
public static class Product {
int id;
String productName;
String price;
int quantity;
public Product(int id, String productName, String price, int quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
public String toString() {
return String.format("%-8s%-30s%-8s%-4s",
(String.valueOf(id).length() > 8) ? String.valueOf(id).substring(0, 8) : id,
(productName.length() > 30) ? productName.substring(0, 30) : productName,
(price.length() > 8) ? price.substring(0, 8) : price,
(String.valueOf(quantity).length() > 4) ? String.valueOf(quantity).substring(0, 4) : quantity);
}
}
public static void main(String[] args) {
String fileDestination;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileReader = new BufferedReader(new FileReader(fileDestination = reader.readLine()))) {
if (args.length == 0) {
return;
}
List<String> listOfLines = new ArrayList<>();
String fileLine = fileReader.readLine();
while (fileLine != null) {
listOfLines.add(fileLine);
fileLine = fileReader.readLine();
}
switch (args[0]) {
case "-u":
Product product = getProduct(args);
for (int i = 0; i < listOfLines.size(); i++) {
if (listOfLines.get(i).contains(args[1])) {
listOfLines.set(i, product.toString());
}
}
break;
case "-d":
for (int i = 0; i < listOfLines.size(); i++) {
if (listOfLines.get(i).contains(args[1])) {
listOfLines.remove(i);
break;
}
}
}
try (FileWriter fileWriter = new FileWriter(fileDestination)) {
for (String element : listOfLines) {
fileWriter.write(element);
fileWriter.write("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Product getProduct(String[] list) {
StringBuilder productName = new StringBuilder();
StringBuilder price = new StringBuilder();
StringBuilder quantity = new StringBuilder();
for (int i = 2; i < list.length; i++) {
if (list[i].matches("^[a-zA-Z]+")) {
productName.append(list[i]).append(" ");
}
if (list[i].matches("(\\d+\\.\\d+)")) {
price.append(list[i]);
}
if (list[i].matches("\\d+")) {
quantity.append(list[i]);
}
}
return new Product(Integer.parseInt(list[1]), productName.toString(),
price.toString(), Integer.parseInt(quantity.toString()));
}
}