I've written this code, but I can't pass the 3d and 4th requirements, i.e. I need to make sure the product list remains unchanged when you run the program without arguments, and 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.
I thought the line if (!args[0].equals("-c")) return; was enough to make sure nothing happens when there are no arguments or the arguments are different from what is expected.
I probably misunderstand the task.
For instance, I'm not really getting how I am supposed to follow this instruction:
"Before adding a new line, re-write all of its contents to the file".
To me, adding a line (to the file) equals to rewriting the contents of this line to the file. How can one be done before the other? Is that where my mistake is?
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
import java.nio.Buffer;
public class Solution {
public static void main(String[] args) throws Exception {
//• The program should read a file name for CrUD operations from the console.
BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
String fName = breader.readLine();
FileReader freader = new FileReader(fName);
BufferedReader breader2 = new BufferedReader(freader);
int idMax = 0;
while (breader2.ready()) {
String line = breader2.readLine();
String regex1 = line.substring(0,8);
String digits = regex1.trim();
int id = Integer.parseInt(digits);
idMax = idMax > id? idMax : id;
}
//• The Solution class should not use static variables. - OK
//• When you run the program without arguments, the product list must remain unchanged.
//• 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.
BufferedWriter bwriter = new BufferedWriter(new FileWriter(fName));
if (!args[0].equals("-c")) return;
//The width is the minimum number of characters to be written to the output.
// If the length of the converted value is less than the width
// then the output will be padded by ' ' ('\u0020')
// until the total number of characters equals the width.
// The padding is on the left by default.
// If the '-' flag is given, then the padding will be on the right. If the width is not specified then there is no minimum.
String s1 = String.valueOf(idMax + 1);
//String.format("%1$" + length + "s", inputString);
String ID = String.format("%1$-" + 8 + "s", s1);
/* if (s1.length() < 8) {
for (int i = 0; i < (8 - s1.length()); i++) {
s1 = s1 + " ";
}
} else s1 = s1.substring(0, 8);*/
String s2 = args[1];
/*if (s2.length() < 30) {
for (int i = 0; i < (30 - s2.length()); i++) {
s2 = s2 + " ";
}
} else s2 = s2.substring(0, 30);*/
String productName = String.format("%1$-" + 30 + "s", s2);
String s3 = args[2];
/*if (s3.length() < 8) {
for (int i = 0; i < (8 - s3.length()); i++) {
s3 = s3 + " ";
}
} else s3 = s3.substring(0, 8);*/
String price = String.format("%1$-" + 8 + "s", s3);
String s4 = args[3];
/*if (s4.length() < 4) {
for (int i = 0; i < (4 - s4.length()); i++) {
s4 = s4 + " ";
}
} else s4 = s4.substring(0, 4);*/
String quantity = String.format("%1$-" + 4 + "s", s4);
String result = ID + productName + price + quantity;
bwriter.write(result);
bwriter.write(System.lineSeparator());
//• The product must have the next id after the maximum id found in the file.
//• The format of the new product line must precisely match that format specified in the task.
//• The file streams must be closed.
breader.close();
freader.close();
breader2.close();
bwriter.close();
}
}