Hi there,
First, is the logic of this solution right?
Second, what is not working in this piece of code:
int index = 0;
while(index < products.size()){
Product product = new Product(products.get(index).id, products.get(index).productName, products.get(index).price
, products.get(index).quantity);
if(product.id==Integer.parseInt(args[1])){
products.remove(product);
break;
}
index++;
}
I tought this will remove the product from the list products but is not the case.
Thankspackage com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) throws IOException {
/*
args[0] - -u / -d
args[1] - id
args[2] - productName
args[3] - price
args[4] - quantity
*/
args = new String[]{"-d", "2", "CHAQUETA", "300", "5"};
if(args.length==0) return;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = null;
while(fileName == null || fileName.isEmpty()){
try{
fileName = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
reader.close();
List<Product> products = new ArrayList<>();
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
while(fileReader.ready()){
products.add(getProduct(fileReader.readLine()));
}
fileReader.close();
switch(args[0]){
case "-u": // update
for(Product product : products){
if(String.valueOf(product.id).equals(args[1])){
String name = args[2].length() > 30? args[2].substring(0, 30).trim():args[2].trim();
String price = args[3].length() > 8? args[3].substring(0, 8).trim():args[3].trim();
String quantity = args[4].length() > 4? args[4].substring(0, 4).trim(): args[4].trim();
product.setProductName(name);
product.setPrice(price);
product.setQuantity(quantity);
}
}
break;
case "-d": // delete
int index = 0;
while(index < products.size()){
Product product = new Product(products.get(index).id, products.get(index).productName, products.get(index).price
, products.get(index).quantity);
if(product.id==Integer.parseInt(args[1])){
products.remove(product);
break;
}
index++;
}
break;
}
FileWriter fileWriter = new FileWriter(fileName);
for(Product product : products){
fileWriter.write(product.toString());
fileWriter.write("\n");
}
fileWriter.close();
}
public 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;
}
public void setProductName(String productName){
this.productName = productName;
}
public void setPrice(String price){
this.price = price;
}
public void setQuantity(String quantity){
this.quantity = quantity;
}
@Override
public String toString() {
return String.format("%-8d %-30s %-8s %-4s", id, productName, price, quantity);
}
}
public static Product getProduct(String string){
int id = Integer.parseInt(string.substring(0, 8).trim());
String productName = string.substring(8, 38).trim();
String price = string.substring(38, 46).trim();
String quantity = string.substring(46, 50).trim();
return new Product(id, productName, price, quantity);
}
}