Is there something I'm not taking into consideration? As far as I can tell the code should work ok.
package com.codegym.task.task18.task1827;
/*
Prices
CrUD for a table inside a file.
Read a file name for CrUD operations from the console.
The program is started with the following arguments:
-c productName price quantity
Argument values:
where id is 8 characters.
productName is 30 characters.
price is 8 characters.
quantity is 4 characters.
-c adds the product with the specified parameters to the end of the file, and generates an id by incrementing the maximum id found in the file.
The file data is stored in the following order (without separating spaces):
id productName price quantity
Each data field is padded with spaces up to its length.
Before adding a new line, re-write all of its contents to the file.
Example:
19846 Swim trunks, blue 159.00 12
198478 Swim trunks, black with print 173.00 17
19847983Snowboard jacket 10173.991234
Requirements:
1. The program should read a file name for CrUD operations from the console.
2. The Solution class should not use static variables.
3. When you run the program without arguments, the product list must remain unchanged.
4. When the program is run with the arguments "-c productName price quantity", a new line with the corresponding product should be added to the end of the file.
5. The product must have the next id after the maximum id found in the file.
6. The format of the new product line must precisely match that format specified in the task.
7. The file streams must be closed.
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String filename = br.readLine();
FileInputStream inputStream = new FileInputStream(filename);
br.close();
br = new BufferedReader(new InputStreamReader(inputStream));
if (args[0].equals("-c")) {
int start = 1;
String s = "";
while (true) {
s = br.readLine();
if (s == null) break;
start++;
}
String idString = buildIdString(start);
String productNameString = buildProductNameString(args[1]);
String priceString = buildPriceString(args[2]);
String quantityString = buildQuanityString(args[3]);
StringBuilder sb = new StringBuilder();
sb.append(idString);
sb.append(productNameString);
sb.append(priceString);
sb.append(quantityString);
sb.append("\n");
FileOutputStream outputStream = new FileOutputStream(filename, true);
outputStream.write(sb.toString().getBytes());
outputStream.close();
}
br.close();
inputStream.close();
}
public static String buildIdString(int indexNo) {
String ind = Integer.toString(indexNo);
char[] idBuffer = new char[8];
for (int i = 0; i < idBuffer.length; i++) {
if (i < ind.length()) idBuffer[i] = ind.charAt(i);
else idBuffer[i] = ' ';
}
return new String(idBuffer);
}
public static String buildProductNameString(String productName) {
char[] productNameBuffer = new char[30];
for (int i = 0; i < productNameBuffer.length; i++) {
if (i < productName.length()) productNameBuffer[i] = productName.charAt(i);
else productNameBuffer[i] = ' ';
}
return new String(productNameBuffer);
}
public static String buildPriceString(String price) {
char[] priceBuffer = new char[8];
for (int i = 0; i < priceBuffer.length; i++) {
if (i < price.length()) priceBuffer[i] = price.charAt(i);
else priceBuffer[i] = ' ';
}
return new String(priceBuffer);
}
public static String buildQuanityString(String quantity) {
char[] quantityBuffer = new char[4];
for (int i = 0; i < quantityBuffer.length; i++) {
if (i < quantity.length()) quantityBuffer[i] = quantity.charAt(i);
else quantityBuffer[i] = ' ';
}
return new String(quantityBuffer);
}
}