This code passed all my tests, updated product resides on the same line as before update, but requirement 3 somehow not met...
What am I doing wrong, or how to change code to pass requirement 3?
package 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 Exception {
//args = new String[] {"-c", "Beer, very strong and tasty", "4.32", "128"};
//args = new String[] {"-d", "198479"};
//args = new String[] {"-u", "198479", "Beer, very strong and tasty", "4.32", "128"};
if (args.length == 0) return;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
List<ProductInfo> productList = loadProductList(fileName);
switch (args[0]) {
case "-u":
updateProduct(productList, ProductInfo.parseProduct(args, 1));
break;
case "-d":
deleteProduct(productList, Integer.parseInt(args[1]));
break;
}
saveProducts(productList, fileName);
}
private static void deleteProduct(List<ProductInfo> productList, int id) {
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).id == id) {
productList.remove(i);
break;
}
}
}
private static void updateProduct(List<ProductInfo> productList, ProductInfo product) {
for (int i = 0; i < productList.size(); i++) {
if (productList.get(i).id == product.getId()) {
productList.get(i).productName = product.productName;
productList.get(i).price = product.price;
productList.get(i).quantity = product.quantity;
break;
}
}
}
private static void saveProducts(List<ProductInfo> products, String fileName) throws IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
for (ProductInfo product : products) {
writer.write(product.toString());
writer.newLine();
}
writer.close();
}
private static List<ProductInfo> loadProductList(String fileName) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
List<ProductInfo> result = new ArrayList<>();
String entry;
while ((entry = reader.readLine()) != null) {
result.add(ProductInfo.getInstance(entry));
}
reader.close();
return result;
}
private static class ProductInfo {
private int id;
private String productName;
private double price;
private int quantity;
private static int nextId;
public static ProductInfo getInstance(String stringEntry) {
int id = Integer.parseInt(stringEntry.substring(0, 8).trim());
String productName = stringEntry.substring(8, 38).trim();
double price = Double.parseDouble(stringEntry.substring(38, 46).trim());
int quantity = Integer.parseInt(stringEntry.substring(46, 50).trim());
return new ProductInfo(id, productName, price, quantity);
}
public static ProductInfo parseProduct(String[] params, int startingIndex) {
int id = 0;
StringBuilder productName = new StringBuilder();
double price = 0;
int quantity = 0;
for (int i = startingIndex; i < params.length; i++) {
if (i == params.length - 1)
quantity = Integer.parseInt(params[i].trim());
else if (i == params.length - 2)
price = Double.parseDouble(params[i].trim());
else if (i == startingIndex)
id = Integer.parseInt(params[i].trim());
else
productName.append(params[i]).append(" ");
}
return new ProductInfo(id, productName.toString().trim(), price, quantity);
}
public static ProductInfo parseProduct(String[] params) {
if (params[0].equals("-c")) {
StringBuilder productName = new StringBuilder();
double price = 0;
int quantity = 0;
for (int i = 1; i < params.length; i++) {
if (i == params.length - 1)
quantity = Integer.parseInt(params[i].trim());
else if (i == params.length - 2)
price = Double.parseDouble(params[i].trim());
else
productName.append(params[i]).append(" ");
}
return new ProductInfo(productName.toString().trim(), price, quantity);
} else
return null;
}
public ProductInfo(int id, String productName, double price, int quantity) {
this.id = id;
this.productName = productName;
this.price = price;
this.quantity = quantity;
if (nextId <= this.id) nextId = this.id + 1;
}
public ProductInfo(String productName, double price, int quantity) {
this.id = nextId;
this.productName = productName;
this.price = price;
this.quantity = quantity;
nextId++;
}
public int getId() {
return id;
}
public String getProductName() {
return productName;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("%-8s", getId()));
result.append(String.format("%-30s", getProductName()));
result.append(String.format("%-8s", getPrice()));
result.append(String.format("%-4s", getQuantity()));
return result.toString();
}
}
}