I think my solution is working, it's updating passed id on the same line but for some reason validation is not passing. Maybe there is something that i missed or don't understand. Every bit of help is appreciated :)
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) throws Exception {
if(args.length > 0 ){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = reader.readLine();
reader.close();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
ArrayList<Record> records = new ArrayList<>();
String recordLine = reader.readLine();
while(recordLine != null) {
records.add(new Record(recordLine));
recordLine = reader.readLine();
}
reader.close();
if(args[0].equals("-d")) {
int id = Integer.parseInt(args[1]);
int idToDelete = -1;
for(int i = 0; i < records.size(); i++) {
if(records.get(i).getId() == id){
idToDelete = i;
break;
}
}
records.remove(idToDelete);
} else if (args[0].equals("-u")){
int id = Integer.parseInt(args[1]);
int idToUpdate = -1;
for(int i = 0; i < records.size(); i++) {
if(records.get(i).getId() == id){
idToUpdate = i;
break;
}
}
records.get(idToUpdate).setProductName(args[2]);
records.get(idToUpdate).setPrice(Double.parseDouble(args[3]));
records.get(idToUpdate).setQuantity(Integer.parseInt(args[4]));
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
for (Record r : records) {
writer.write(r.toString() + "\n");
}
writer.close();
}
}
}
class Record implements Comparable<Record>{
private int id;
private String productName;
private Double price;
private int quantity;
public static int ID_WIDTH = 8;
public static int PRODUCT_NAME_WIDTH = 30;
public static int PRICE_WIDTH = 8;
public static int QUANTITY_WIDTH = 4;
public Record (int id, String productName, double price, int quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
public Record(String line) {
this.id = Integer.parseInt(line.substring(0, Record.ID_WIDTH).trim());
this.productName = line.substring(Record.ID_WIDTH, Record.ID_WIDTH + Record.PRODUCT_NAME_WIDTH).trim();
this.price = Double.parseDouble(line.substring(Record.ID_WIDTH + Record.PRODUCT_NAME_WIDTH, Record.ID_WIDTH + Record.PRODUCT_NAME_WIDTH + Record.PRICE_WIDTH).trim());
this.quantity = Integer.parseInt(line.substring(Record.ID_WIDTH + Record.PRODUCT_NAME_WIDTH + Record.PRICE_WIDTH, Record.ID_WIDTH + Record.PRODUCT_NAME_WIDTH + Record.PRICE_WIDTH + Record.QUANTITY_WIDTH).trim());
}
@Override
public int compareTo(Record o) {
return Integer.compare(id, o.id);
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setPrice(Double price) {
this.price = price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
String result = String.format("%1$-" + ID_WIDTH + "s", id) +
String.format("%1$-" + PRODUCT_NAME_WIDTH + "s", productName) +
String.format("%1$-" + PRICE_WIDTH + "s", price) +
String.format("%1$-" + QUANTITY_WIDTH + "s", quantity);
return result;
}
public int getId() {
return id;
}
}
//String.fo