My code works flawlessly.
I checked it on my computer 100500+ times.
But the test does not want to pass ... strange. Who will say why?
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
return;
}
String id;
String productName = "";
String price = "";
String quantity = "";
String tmpStr;
String tmpString;
StringBuilder str = new StringBuilder();
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
File tmp_fileName = File.createTempFile("TMP", ".txt");
BufferedWriter tmp_file = new BufferedWriter(new FileWriter(tmp_fileName));
BufferedReader fileRead = new BufferedReader(new FileReader(fileName));
one: {
if (args.length == 2 && args[0].equals("-d")) {
id = args[1];
} else if (args.length > 4 && args[0].equals("-u")) {
id = args[1];
price = args[args.length - 2];
quantity = args[args.length - 1];
if (args.length == 5) {
productName = args[2];
break one;
}
int difference = args.length - 3;
for (int i = 2; i <= difference; i++) {
productName += args[i];
productName += " ";
}
} else {
return;
}
}
id = addSpaces(id, 8);
productName = addSpaces(productName, 30);
price = addSpaces(price, 8);
quantity = addSpaces(quantity, 4);
str.append(id).append(productName).append(price).append(quantity);
while (fileRead.ready())
{
tmpStr = fileRead.readLine();
tmpString = tmpStr.substring(0, 8);
if (tmpString.equals(id) && args[0].equals("-d")) {
continue;
} else if (tmpString.equals(id) && args[0].equals("-u")) {
tmpStr = str.toString();
}
tmp_file.write(tmpStr);
tmp_file.newLine();
}
scanner.close();
fileRead.close();
tmp_file.close();
BufferedWriter fileWrite = new BufferedWriter(new FileWriter(fileName));
BufferedReader tmp_fileRead = new BufferedReader(new FileReader(tmp_fileName));
while (tmp_fileRead.ready())
{
tmpStr = tmp_fileRead.readLine();
fileWrite.write(tmpStr);
fileWrite.newLine();
}
fileWrite.close();
tmp_fileRead.close();
tmp_fileName.deleteOnExit();
}
public static String addSpaces(String str, int count) {
while (str.length() < count)
{
str += " ";
}
if (str.length() > count)
str = str.substring(0, count);
return str;
}
}