Говорит, что не правильно обновляю данные в файле. Именно обновление
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length==0) return;
//*******************************
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
List<Product> products = new ArrayList<>();
//--------------------------------------------------------------------------------------
try (BufferedReader fileReader = new BufferedReader(new FileReader(fileName))) {
while (fileReader.ready()) {
Product product = Product.getProduct(fileReader.readLine());
products.add(product);
}
}
//--------------------------------------------------------------------------------------
if (args.length > 0) {
if (args[0].equals("-u"))
update(products, args, fileName);
if (args[0].equals("-d"))
delete(products, args, fileName);
}
}
public static void update(List<Product> products, String[] args, String fileName) throws Exception {
int id = Integer.parseInt(args[1]);
String name = "";
String price = args[args.length-2];
String quantity = args[args.length-1];
for (int i=1;i<args.length-2;i++) {name+=args[i]+" ";}
if (name.length()>30) name.substring(0,30);
if (price.length()>8) price.substring(0,8);
if (quantity.length()>4) quantity.substring(0,4);
//===========================================================
Product productU = null;
for (Product product:products)
if (product.id == id) productU = product;
if (productU != null) {
productU.name = name;
productU.price = price;
productU.quantity = quantity;
}
//===========================================================
try (FileWriter fileWriter = new FileWriter(fileName)) {
for (Product product:products) {
fileWriter.write(product.toString());
fileWriter.write("\n");
}
}
}
public static void delete(List<Product> products, String[] args, String fileName) throws Exception {
int id = Integer.parseInt(args[1]);
Product productToDelete = null;
for (Product product : products)
if (product.id == id) productToDelete = product;
if (productToDelete != null) products.remove(productToDelete);
//===========================================================
try (FileWriter fileWriter = new FileWriter(fileName)) {
for (Product product:products) {
fileWriter.write(product.toString());
fileWriter.write("\n");
}
}
}
}