What am i doing wrong? Can't figure it out, please help
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static class Product {
public int id;
public String name;
public int price;
public int quantity;
public Product(int id, String name, int price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8d%-30s%-8d%-4d", id, name, price, quantity);
}
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
if (args[0].equals("-u")) {
update(fileName, args);
} else if (args[0].equals("-d")) {
delete(fileName, args);
}
}
public static void update(String fileName, String[] args) {
Set<Product> products = read(fileName);
for (Product product : products) {
if (product.id == Integer.parseInt(args[args.length - 4])) {
product.name = args[args.length - 3];
product.price = Integer.parseInt(args[args.length - 4]);
product.quantity = Integer.parseInt(args[args.length - 4]);
}
}
write(fileName, products);
}
public static void delete(String fileName, String[] args) {
Set<Product> products = read(fileName);
for (Product product : products) {
if (product.id == Integer.parseInt(args[args.length - 1])) {
products.remove(product);
}
}
write(fileName, products);
}
public static void write(String fileName, Set<Product> products) {
try (FileWriter fileWriter = new FileWriter(fileName, true)) {
for (Product product : products) {
fileWriter.write(product.toString() + "\n");
}
} catch (IOException e) {
}
}
public static Set<Product> read(String fileName) {
Set<Product> products = new HashSet<>();
try (BufferedReader fileReader = new BufferedReader(new FileReader(fileName))) {
while (fileReader.ready()) {
String line = fileReader.readLine();
int id = Integer.parseInt(line.substring(0, 8).trim());
String name = line.substring(8, 38).trim();
int price = Integer.parseInt(line.substring(38, 46).trim());
int quantity = Integer.parseInt(line.substring(46, 50).trim());
products.add(new Product(id, name, price, quantity));
}
} catch (IOException e) {
}
return products;
}
}