not able to verify the condition but when run in intellij , I am getting the expected output. I believe it would be worth highlighting the errors with respect to test cases for this program. Thanks.
package com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
//poc for padding: System.out.println(String.format("%1$-" + 8 + "s", "what").replace(' ', '0'));
//1. read fileName from the console.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
// String fileName="C:\\Users\\609631742\\OneDrive - BT Plc\\Notebooks\\or\\devops\\java-30-jan\\daily notes\\new.txt";
ArrayList<String> idStoreAsString = new ArrayList<String>();
FileInputStream readFile = new FileInputStream(fileName);
Scanner sc = new Scanner(readFile);
while (sc.hasNextLine()) {
String id = sc.nextLine().substring(0, 8);
//storing ids in arrayList
idStoreAsString.add(id);
}
readFile.close();
//find the maximum id in arrayList
Collections.sort(idStoreAsString, Collections.reverseOrder());
String max = idStoreAsString.get(0);
max = Integer.toString(Integer.parseInt(max) + 1);
//to store the passed argument into related fields with padding to fulfill requirement.
//ex: -c productName price quantity
max = String.format("%1$-" + 8 + "s", max);
//creating FileOutputStream
FileOutputStream writeFile = new FileOutputStream(fileName, true);
try {
String productName = String.format("%1$-" + 30 + "s", args[1]);
String price = String.format("%1$-" + 8 + "s", args[2]);
String quantity = String.format("%1$-" + 4 + "s", args[3]);
//line to be added at end of the file.
String lineToBeAddedToFile = max + productName + price + quantity;
//2.not using the static variables.
//3.program without arguments,keeps file unchanged.
//4.run with arguments -c
//3.program without arguments.
if (args[0].equals("-c") && !args[1].equals(null) && !args[2].equals((null)) && !args[3].equals(null)) {
String returnString = System.lineSeparator();
writeFile.write(returnString.getBytes());
writeFile.write(lineToBeAddedToFile.getBytes());
} //end of if
} catch (Exception e) {
}
reader.close();
readFile.close();
writeFile.close();
} //end of main
} //end of Solution