Ну я понимаю, может, где -то ошибка - но где???
Особенно мне "очень нравится" - Товар должен иметь следующий id, после максимального, найденного в файле. КАК ТАК-ТО???
Задолбало подгонять решения под валидатор...
package com.javarush.task.task18.task1827;
/*
Прайсы
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws Exception {
String fileName = new BufferedReader(new InputStreamReader(System.in)).readLine();
FileInputStream productFileR = new FileInputStream(fileName);
/* Значения параметров:
>>>>>> -c productName price quantity <<<<<<<<
где id - 8 символов.
productName - название товара, 30 chars (60 bytes).
price - цена, 8 символов.
quantity - количество, 4 символа.
-c - добавляет товар с заданными параметрами в конец файла,
генерирует id самостоятельно, инкрементируя максимальный id, найденный в файле. */
String file = "";
while(productFileR.available() > 0){
file += (char)productFileR.read();
}
productFileR.close();
String [] fileSplitted = file.split("\\n");
String lastLine = fileSplitted[fileSplitted.length - 1];
String lastId = lastLine.substring(0, 8);
String [] lastIdSpletted = lastId.split(" ");
int id = Integer.parseInt(lastIdSpletted[0]);
++id;
String idS = String.valueOf(id);
if (idS.length() < 8){
for(int x = 0; x < (9 - idS.length()); x++){
idS += " ";
}
}
else {
idS = idS.substring(0, 8);
}
// "-c Many Bitcoins 20000 10000" - arguments
// -c Куртка для сноубордистов, разм10173.991234
// 012345678901234567890123456789012345678901234567890
// -------8----------------------------30-------8---4
//
Pattern p = Pattern.compile("[^-c ]\\D+(?!\\d)");
Matcher m = p.matcher(args[0]);
String productName = "";
String price = "";
String quantity = "";
while(m.find()){
productName = m.group();
}
p = Pattern.compile("\\d+\\.\\d+");
m = p.matcher(args[0]);
while(m.find()){
price = m.group();
}
p = Pattern.compile("(?<=\\s)\\d+(?!\\.)$");
m = p.matcher(args[0]);
while(m.find()){
quantity = m.group();
}
if (productName.length() < 30){
for(int x = 0; x < (31 - productName.length()); x++){
productName += " ";
}
}
else {
productName = productName.substring(0, 30);
}
// System.out.println("productName = " + productName);
// System.out.println("price = " + price);
// System.out.println("quantity = " + quantity);
if (price.length() < 8){
for(int x = 0; x < (9 - price.length()); x++){
price += " ";
}
}
else {
price = price.substring(0, 8);
}
if (quantity.length() < 4){
for(int x = 0; x < (5 - quantity.length()); x++){
quantity += " ";
}
}
else {
quantity = quantity.substring(0, 4);
}
// "-c Bitcoins 20000 10000" - arguments
FileOutputStream productsFileW = new FileOutputStream(fileName, true);
productsFileW.write(10);
productsFileW.write(idS.getBytes());
productsFileW.write(productName.getBytes());
productsFileW.write(price.getBytes());
productsFileW.write(quantity.getBytes());
// 19846 Шорты пляжные синие 159.00 12
// 198478 Шорты пляжные черные с рисунко173.00 17
// 19847983Куртка для сноубордистов, разм10173.991234
// 1984798410MillionDollars 100000001
productsFileW.close();
}
}