It doesn't want to pass tests with -u argument which updates the product but it does on my computer :D Any ideas? Help much appreciated!
package pl.codegym.task.task18.task1828;
/*
Ceny 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
private static List<Product> productList = new ArrayList<>();
private static String fileName;
private static BufferedReader reader;
static {
reader = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) throws IOException {
fileName = reader.readLine();
fetchProducts();
switch (args[0]) {
case "-u":
int id = Integer.parseInt(args[1]);
String productName = args[2];
double price = Double.parseDouble(args[3]);
int quantity = Integer.parseInt(args[4]);
updateProduct(id, productName, price, quantity);
break;
case "-d":
removeProduct(Integer.parseInt(args[1]));
break;
}
writeToFile();
reader.close();
}
private static void fetchProducts() throws IOException {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line;
while ((line = reader.readLine()) != null) {
int productId = Integer.parseInt(line.replaceAll("\\s+", "").substring(0, 8).replaceAll("\\p{L}", ""));
String name = line.substring(8, 38).replaceAll("\\s+$", "");
double price = Double.parseDouble(line.substring(38, 46));
int quantity = Integer.parseInt(line.substring(46, 50).replaceAll("\\s+", ""));
productList.add(new Product(productId, name, price, quantity));
}
}
private static void writeToFile() throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
List<String> buffer = new ArrayList<>();
for (Product product : productList) {
buffer.add(
createCell(String.valueOf(product.getId()), 8) +
createCell(product.getName(), 30) +
createCell(String.valueOf(product.getPrice()), 8) +
createCell(String.valueOf(product.getQuantity()), 4));
}
for (String line : buffer) {
writer.write(line + "\n");
writer.flush();
}
writer.close();
}
private static void updateProduct(int id, String newProductName, double newPrice, int newQuantity) {
Product product = searchProduct(id);
product.setName(newProductName);
product.setPrice(newPrice);
product.setQuantity(newQuantity);
}
private static Product searchProduct(int productIndex) {
return productList.get(searchProductIndex(productIndex));
}
private static void removeProduct(int productId) {
int productIndex = searchProductIndex(productId);
productList.remove(productIndex);
}
private static int searchProductIndex(int productId) {
for (int i = 0; i < productList.size() - 1; i++) {
if (productList.get(i).getId() == productId)
return i;
}
return 0;
}
private static String createCell(String s, int maxChars) {
byte[] finalBytes = new byte[maxChars];
Arrays.fill(finalBytes, (byte) 32);
byte[] helperBytes = s.getBytes();
for (int i = 0; i < helperBytes.length; i++) {
finalBytes[i] = helperBytes[i];
}
return new String(finalBytes);
}
public static class Product {
private int id;
private String name;
private double price;
private int quantity;
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", quantity=" + quantity +
'}';
}
}
}