Помогите как решить одно условие
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
Прайсы 2
*/
public class Solution {
static class Product {
int id;
String productName;
String price;
String quantity;
public Product(int id, String productName, String price, String quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8d%-30s%-8s%-4s", id, productName, price, quantity);
}
}
public static void main(String[] args) throws IOException {
if (args.length == 0)
return;
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
scanner.close();
List<Product> productList = new ArrayList<>();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
while (bufferedReader.ready())
productList.add(getProduct(bufferedReader.readLine()));
}
switch (args[0]) {
case "-u": {
String productName = args[2];
if (productName.length() > 30)
productName = productName.substring(0, 30);
String price = args[3];
if (price.length() > 8)
price = price.substring(0, 8);
String quantity = args[4];
if (quantity.length() > 4)
quantity = quantity.substring(0, 4);
Product productToUpdate = null;
for (Product product : productList) {
if (product.id == Integer.parseInt(args[1])) {
productToUpdate = product;
}
if (productToUpdate != null) {
productToUpdate.productName = productName;
productToUpdate.price = price;
productToUpdate.quantity = quantity;
}
}
break;
}
case "-d": {
productList.removeIf(product -> product.id == Integer.parseInt(args[1]));
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName))) {
for (Product product : productList) {
bufferedWriter.write(product.toString());
bufferedWriter.write("\n");
}
}
}
}
private static Product getProduct(String s) {
String id = s.substring(0, 8).trim();
String productName = s.substring(8, 38).trim();
String price = s.substring(38, 46).trim();
String quantity = s.substring(46, 50).trim();
return new Product(Integer.parseInt(id), productName, price, quantity);
}
}