Решал задачу по аналогии с предыдущей. Есть подозрение что неправильно сравниваю id
package com.javarush.task.task18.task1828;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
public static class Product {
String id;
String name;
String price;
String quantity;
public Product(String id, String name, String price, String quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8s%-30s%-8s%-4s", id, name, price, quantity);
}
}
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = null;
try {
fileName = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
List<Product> products = new ArrayList<>();
try (BufferedReader fileReader = new BufferedReader(new FileReader(fileName))) {
while (fileReader.ready()) {
Product product = getProduct(fileReader.readLine());
products.add(product);
}
} catch (IOException e) {
e.printStackTrace();
}
switch (args[0]) {
case "-u":
for (Product product : products) {
if (args[1].equals(product.id)) {
product.name = args[2];
product.price = args[3];
product.quantity = args[4];
}
}
case "-d":
products.removeIf(product -> product.id.equals(args[1]));
}
try (FileWriter fileWriter = new FileWriter(fileName)) {
for (Product product : products) {
fileWriter.write(product.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Product getProduct(String string) {
String id = string.substring(0, 8).trim();
String name = string.substring(8, 38).trim();
String price = string.substring(38, 46).trim();
String quantity = string.substring(46, 50).trim();
return new Product(id, name, price, quantity);
}
}