Hi,
Could you please help me to figure out what I am doing wrong here?
Test for creation fails, but it works fine when I test it manually.
It could be related to String->Double conversion or something else.
Please, give some hints what I am missing here.
Thank you!
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
// String fileName = "/Users/baurzhan/Downloads/productTable.txt";
//
switch (args[0]) {
case "-c":
Product product = new Product(args[1], args[2], args[3]);
CRUD crud = new CRUD(fileName);
crud.create(product);
}
}
public static class Product {
private int id;
private String name;
private double price;
private int quantity;
private final int MAX_LENGTH_ID = 8;
private final int MAX_LENGTH_NAME = 30;
private final int MAX_LENGTH_PRICE = 8;
private final int MAX_LENGTH_QUANTITY = 4;
private final int INDEX_ID_START = 0;
private final int INDEX_ID_END = INDEX_ID_START + MAX_LENGTH_ID;
private final int INDEX_NAME_START = INDEX_ID_END;
private final int INDEX_NAME_END = INDEX_NAME_START + MAX_LENGTH_NAME;
private final int INDEX_PRICE_START = INDEX_NAME_END;
private final int INDEX_PRICE_END = INDEX_PRICE_START + MAX_LENGTH_PRICE;
private final int INDEX_QUANTITY_START = INDEX_PRICE_END;
private final int INDEX_QUANTITY_END = INDEX_QUANTITY_START + MAX_LENGTH_QUANTITY;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public Product(String line) {
//System.out.println("Constructor line, " + "line is " + line + ", substr is " + line.substring(INDEX_ID_START, INDEX_ID_END));
this.id = Integer.parseInt(line.substring(INDEX_ID_START, INDEX_ID_END).trim());
this.name = line.substring(INDEX_NAME_START, INDEX_NAME_END).trim();
this.price = Double.parseDouble(line.substring(INDEX_PRICE_START, INDEX_PRICE_END).trim());
this.quantity = Integer.parseInt(line.substring(INDEX_QUANTITY_START, INDEX_QUANTITY_END).trim());
}
public Product(String id, String name, String price, String quantity) {
this.id = Integer.parseInt(id.trim());
if (name.length()> MAX_LENGTH_NAME ) {
this.name = name.trim().substring(0, MAX_LENGTH_NAME);
} else {
this.name = name.trim();
}
this.price = Double.parseDouble(price.trim());
this.quantity = Integer.parseInt(quantity.trim());
}
public Product(String name, double price, int quantity) {
this.name = name.trim();
this.price = price;
this.quantity = quantity;
}
public Product(String name, String price, String quantity) {
this.name = name.trim();
this.price = Double.parseDouble(price.trim());
this.quantity = Integer.parseInt(quantity.trim());
}
private static String padRight(String s, int n) {
return String.format("%-" + n + "s", s);
}
@Override
public String toString() {
return padRight(String.valueOf(id), 8) + padRight(name, 30) + padRight(String.format("%.2f", price), 8) + padRight(String.valueOf(quantity), 4);
}
}
public static class CRUD {
private String fileName;
public CRUD(String fileName) {
this.fileName = fileName;
}
public void create(Product product) throws IOException {
int nextID = getMaxID() + 1;
if (product.getId() == 0) {
product.setId(nextID);
}
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName, true));
bufferedWriter.write(product.toString());
bufferedWriter.newLine();
bufferedWriter.close();
}
private int getMaxID() throws IOException {
int result = 1;
String data, lastLine = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
while ((data = bufferedReader.readLine()) != null) {
lastLine = data;
}
bufferedReader.close();
Product product = new Product(lastLine);
return product.getId();
}
}
}