Может мне не достаточно знаний и я не вижу какой-то огромный недочет?
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
private static ArrayList<ProductFileVersion> allProducts = new ArrayList<ProductFileVersion>();
private static String fileName;
public static void main(String[] args) {
try(BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in))){
fileName = buffer.readLine();
}catch(IOException ex){
ex.printStackTrace();
}
switch(args[0]){
case "-u":{
int findId = Integer.parseInt(args[1]);
initializeAllProsucts();
String name = "";
for (int i = 2; i < args.length - 2; i++) {
name += args[i] + " ";
}
if (name.length() > 30) {
name = name.substring(0, 30);
}
String price = args[args.length - 2];
if (price.length() > 8) {
price = price.substring(0, 8);
}
String quantity = args[args.length - 1];
if (quantity.length() > 4) {
quantity = quantity.substring(0, 4);
}
ProductFileVersion productToUpdate = null;
for (ProductFileVersion product : allProducts) {
if (product.getId() == findId) productToUpdate = product;
}
if (productToUpdate != null) {
productToUpdate.productName = name;
productToUpdate.price = price;
productToUpdate.quantity = quantity;
}
for(int a = 0; a < allProducts.size(); a++){
if(allProducts.get(a).getId() == findId){
allProducts.set(a, productToUpdate);
break;
}
}
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName, true));
BufferedWriter bufferedCleaner = new BufferedWriter(new FileWriter(fileName))){
for(ProductFileVersion product: allProducts){
bufferedCleaner.write("");
bufferedWriter.write(product.toString());
bufferedWriter.write("\n");
}
}catch(IOException ex){
ex.printStackTrace();
}
break;
}
case"-d":{
int findId = Integer.parseInt(args[1]);
initializeAllProsucts();
for(int a = 0; a < allProducts.size(); a++){
if(allProducts.get(a).getId() == findId){
allProducts.remove(a);
break;
}
}
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName, true));
BufferedWriter bufferedCleaner = new BufferedWriter(new FileWriter(fileName))){
for(ProductFileVersion product: allProducts){
bufferedCleaner.write("");
bufferedWriter.write(product.toString());
bufferedWriter.write("\n");
}
}catch(IOException ex){
ex.printStackTrace();
}
break;
}
default:{
System.out.println("Пожалуйста, проверьте настройки ввода.");
}
}
}
private static void initializeAllProsucts(){
try{
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
while(bufferedReader.ready()){
ProductFileVersion product = parseProduct(bufferedReader.readLine());
allProducts.add(product);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static ProductFileVersion parseProduct(String product){
int id = Integer.parseInt(product.substring(0, 8).trim());
String productName = product.substring(8, 38);
String price = product.substring(38, 46);
String quantity = product.substring(46, 50);
return new ProductFileVersion(id, productName, price, quantity);
}
}
class ProductFileVersion{
private int id;
public String productName;
public String price;
public String quantity;
public ProductFileVersion(int id, String productName, String price, String quantity){
this.id = id;
this.productName = productName.trim();
this.price = price.trim();
this.quantity = quantity.trim();
}
@Override
public String toString() {
return String.format("%-8o%-30s%-8s%-4s", id, productName, price, quantity);
}
public int getId() {
return this.id;
}
}