Hi, I'm still working on this task but I'm getting a string out of bounds error on line 33 and can't figure out why. Help will be appreciated. Thanks.
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
//BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//String fileName = reader.readLine();
updateProduct("/home/seferi/test", "2", "productName", "43.8", "322");
}
public static void updateProduct(String fileName, String id, String productName, String price, String quantity) throws IOException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<Product> productList = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(fileName);
BufferedReader lineReader = new BufferedReader(new InputStreamReader(inputStream));
String data;
while ((data = lineReader.readLine()) != null) {
lines.add(data);
}
inputStream.close();
lineReader.close();
for (String line : lines) {
String oldId = line.substring(0, 8).trim();
String oldProductName = line.substring(9, 38).trim();
String oldPrice = line.substring(39, 46).trim();
String oldQuantity = line.substring(47, 50).trim();
productList.add(new Product(oldId, oldProductName, oldPrice, oldQuantity));
}
FileOutputStream outputStream = new FileOutputStream(fileName);
for (Product product : productList) {
if (id.equals(product.getId())) {
product.setProductName(productName);
product.setPrice(price);
product.setQuantity(quantity);
}
String productString = String.format("%-8s%-30s%-8s%-4s\n", product.getId(), product.getProductName(), product.getPrice(), product.getQuantity());
outputStream.write(productString.getBytes());
}
outputStream.close();
}
public static void deleteProduct(String fileName, String id) throws IOException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<Product> productList = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(fileName);
BufferedReader lineReader = new BufferedReader(new InputStreamReader(inputStream));
String data;
while ((data = lineReader.readLine()) != null) {
lines.add(data);
}
inputStream.close();
lineReader.close();
for (String line : lines) {
String oldId = line.substring(0, 8).trim();
String oldProductName = line.substring(8, 30).trim();
String oldPrice = line.substring(30, 38).trim();
String oldQuantity = line.substring(38, 42).trim();
productList.add(new Product(oldId, oldProductName, oldPrice, oldQuantity));
}
FileOutputStream outputStream = new FileOutputStream(fileName);
productList.removeIf(product -> id.equals(product.getId()));
for (Product product : productList) {
String productString = String.format("%-8s%-30s%-8s%-4s\n", product.getId(), product.getProductName(), product.getPrice(), product.getQuantity());
outputStream.write(productString.getBytes());
}
outputStream.close();
}
public static class Product {
private String id;
private String productName;
private String price;
private String quantity;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public Product(String id, String productName, String price, String quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
}
}