Доброго дня!
Убил невероятное множество попыток, просрал почти 50 дней без остановки решения хотя б одной задачи и таки решил запросить помощь.
В моей IDEA запускаю с следующими тестовыми параметрами:
-c "Сапоги резиновые детские утеплённые Фиксики" 120.99 12
Запускаю несколько раз на чистый файл. У меня всё выполняется и в файле всё выглядит так:
1 Сапоги резиновые детские утепл120.99 12
2 Сапоги резиновые детские утепл120.99 12
3 Сапоги резиновые детские утепл120.99 12
4 Сапоги резиновые детские утепл120.99 12
Всё работает, как ожидается, но компилятор JavaRush мне сообщает:
При запуске программы с параметрами "-c productName price quantity" в конец файла должна добавится новая строка с товаром.
Что ему ему не нравится?
Благодарю!package com.javarush.task.task18.task1827;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/*
Прайсы
*/
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length > 0 && "-c".equals(args[0])) {
String fileName = new BufferedReader(new InputStreamReader(System.in)).readLine();
List<Product> list = new ArrayList<>();
try (FileReader fileReader = new FileReader(fileName)) {
list = getProduct(fileReader);
}
int newId = 1;
if (!list.isEmpty()) {
newId = getNewId(list);
}
try (FileWriter fileWriter = new FileWriter(fileName, true)) {
write(fileWriter, new Product(newId, args[1], Float.parseFloat(args[2]), Integer.parseInt(args[3])));
}
}
}
private static List<Product> getProduct(FileReader fileReader) throws IOException {
BufferedReader reader = new BufferedReader(fileReader);
List<Product> products = new ArrayList<>();
while (reader.ready()) {
String stringLine = reader.readLine();
int id = Integer.parseInt((stringLine.substring(0, 8)).trim());
String productName = stringLine.substring(8, 38).trim();
float price = Float.parseFloat((stringLine.substring(38, 46)).trim());
int quantity = Integer.parseInt((stringLine.substring(46, 50)).trim());
products.add(new Product(id, productName, price, quantity));
}
return products;
}
private static int getNewId(List<Product> products) {
int maxId = 0;
for (Product product : products)
maxId = Math.max(product.id, maxId);
return ++maxId;
}
private static void write(FileWriter fileWriter, Product product) throws IOException {
fileWriter.write(String.format(Locale.ENGLISH, "%-8d%-30.30s%-8.2f%-4d",
product.id, product.productName, product.price, product.quantity));
fileWriter.write("\n");
}
public static class Product {
int id;
String productName;
float price;
int quantity;
public Product(int id, String productName, float price, int quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
}
}