Please guys, I need some help.
У меня всё работает. Я проверял через входные данные, которые давали в комментариях к другим, кто не мог пройти.
Я, возможно, что-то упускаю.
З.Ы. Как только я в ручную указываю размер буфера у BufferedOutputStream перестаёт проходить последний пункт, это я тоже понять не могу, так что запишу в магию.
package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Прайсы 2
*/
public class Solution {
private static boolean isWorks = false;
public static void main(String[] args) throws Exception {
String fileName = readFileName();
File file = new File(fileName);
File tmpFile = new File("tmp.txt");
BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedOutputStream fileOutput = new BufferedOutputStream(new FileOutputStream(tmpFile));
if (args[0].equals("-u")) {
String id = buildId(Integer.parseInt(args[1].trim())).toString();
String productName = String.join(" ", Arrays.copyOfRange(args,2, args.length - 2)).trim();
String newLine = formOutputLine(id, productName, args[args.length - 2].trim(), args[args.length - 1].trim());
while (fileReader.ready()) {
String line = fileReader.readLine();
String idString = line.substring(0, 8);
if (idString.equals(id)) fileOutput.write((newLine).getBytes());
else fileOutput.write((line).getBytes());
if (fileReader.ready()) fileOutput.write("\n".getBytes());
}
isWorks = true;
} else if (args[0].equals("-d")) {
String id = buildId(Integer.parseInt(args[1].trim())).toString();
while (fileReader.ready()) {
String line = fileReader.readLine();
String idString = line.substring(0, 8);
if (!idString.equals(id)) {
fileOutput.write((line).getBytes());
if (fileReader.ready()) fileOutput.write("\n".getBytes());
}
}
isWorks = true;
}
fileReader.close();
fileOutput.close();
boolean success;
if (isWorks) success = tmpFile.renameTo(file);
}
private static String readFileName() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
return fileName;
}
/**
* Forms line for write in a file.
* @param id current id
* @param productName name of product that was sent.
* @param price of product that was sent.
* @param quantity of product that was sent.
* @return String in right format for add to file.
*/
private static String formOutputLine(String id, String productName, String price, String quantity) {
StringBuilder line = new StringBuilder();
line.append(id);
line.append(buildProductName(productName));
line.append(buildPrice(price));
line.append(buildQuantity(quantity));
return line.toString();
}
/**
* Takes the max id number from a file and increment it.
* @param id max id
* @return new id number in format of StringBuilder with length = 8.
*/
private static StringBuilder buildId(int id) {
StringBuilder idString = new StringBuilder();
idString.append(id);
while (idString.length() < 8) {
idString.append(" ");
}
return idString;
}
/**
* Takes name of product and returns StringBuilder with length = 30. If original name was more than 30 symbols,
* that all symbols after 30 would cut off, else symbols were less than 30 - it would add empty symbols.
* @param productName original name of product.
* @return new product name in format of StringBuilder with length = 30.
*/
private static StringBuilder buildProductName(String productName) {
StringBuilder productString = new StringBuilder();
if (productName.length() > 30) {
productString.append(productName.substring(0, 30));
} else {
productString.append(productName);
}
while (productString.length() < 30) {
productString.append(" ");
}
return productString;
}
/**
* Takes price of product and returns right StringBuilder
* @param price of product
* @return new product price in format of StringBuilder with length = 8.
*/
private static StringBuilder buildPrice(String price) {
StringBuilder priceString = new StringBuilder();
priceString.append(price);
while (priceString.length() < 8) {
priceString.append(" ");
}
return priceString;
}
private static StringBuilder buildQuantity(String quantity) {
StringBuilder quantityString = new StringBuilder();
quantityString.append(quantity);
while (quantityString.length() < 4) {
quantityString.append(" ");
}
return quantityString;
}
}