Пробовал запускать со своим файлом. Информация о товаре с необходимым id меняется. Валидатор не принимает. Где туплю?
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;
}
ArrayList<Product> products = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
/*String f = reader.readLine();
String sId = reader.readLine();*/
FileReader fr = new FileReader(fileName);
BufferedReader bfr = new BufferedReader(fr);
while (bfr.ready()) {
String s = bfr.readLine();
products.add(getProduct(s));
}
reader.close();
fr.close();
bfr.close();
switch (args[0]) {
case ("-u") : {
/*String sName = reader.readLine();
String sPrice = reader.readLine();
String sAmount = reader.readLine();*/
int id = Integer.parseInt(args[1].trim());
for (Product product : products) {
if (product.id == id) {
product.name = args[2];
product.price = Double.parseDouble(args[3]);
product.amount = Integer.parseInt(args[4]);
}
}
break;
}
case ("-d") : {
int id = Integer.parseInt(args[1].trim());
for (Product product : products) {
if (product.id == id) {
products.remove(product);
}
}
break;
}
}
printProduct(fileName, products);
}
public static Product getProduct(String s) {
int id = Integer.parseInt(s.substring(0, 8).trim());
String name = s.substring(8, 38).trim();
Double price = Double.parseDouble(s.substring(38, 46).trim());
int amount = Integer.parseInt(s.substring(46, 50).trim());
return new Product(id, name, price, amount);
}
public static void printProduct(String fileName, ArrayList<Product> products) throws IOException {
FileWriter fw = new FileWriter(fileName);
BufferedWriter bfw = new BufferedWriter(fw);
for (Product product : products) {
bfw.write(String.format("%-8d%-30s%-8s%-4s\n", product.id, product.name, product.price, product.amount));
}
bfw.close();
fw.close();
}
}