I can't seem to figure out what's causing my code to not verify the 3rd and 4th requirements. Please take a look at my code and recommend what I could try.
Thanks in advance.
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
String fileName, prodName, Entry, price, quantity, lineID;
List<String> FileContent;
int lineIndex = 0;
fileName = getFilename(); //read file from console for CRUD operation
if ((args.length == 5) && (args[0].length() > 0) && args[0].equals("-u")) {
lineID = String.valueOf(Integer.parseInt(args[1]));
while (lineID.length() < 8) lineID+= " ";
prodName = args[2];
while (prodName.length() < 30) prodName += " ";
price = String.format("%.2f", Double.parseDouble(args[3]));
while (price.length() < 8) price += " ";
quantity = String.valueOf(Integer.parseInt(args[4]));
while (quantity.length() < 4) quantity += " ";
Entry = lineID + prodName + price + quantity;
FileContent = getContent(fileName);
for (int i=0; i < FileContent.size(); i++) {
if (FileContent.get(i).substring(0,8).equals(lineID)) {
lineIndex = i;
}
}
FileContent.set(lineIndex, Entry);
createNewFile2(fileName, FileContent);
/*/content for the new file in one string
String NEW_CONTENT = "";
for (int i=0; i < FileContent.size(); i++) {
NEW_CONTENT = NEW_CONTENT + (FileContent.get(i) + "\n");
}
createNewFile(fileName, NEW_CONTENT.trim());
*/
}
else if ((args.length == 2) && (args[0].length() > 0) && args[0].equals("-d")) {
lineID = String.valueOf(Integer.parseInt(args[1]));
FileContent = getContent(fileName);
for (int i = 0; i < FileContent.size(); i++) {
if (FileContent.get(i).substring(0,8).equals(lineID)) {
lineIndex = i;
}
}
FileContent.remove(FileContent.get(lineIndex));
createNewFile2(fileName, FileContent);
/*
String NEW = "";
for (int i = 0; i < FileContent.size(); i++) {
NEW = NEW + (FileContent.get(i) + "\n");
}
createNewFile(fileName, NEW.trim());
*/
}
}
//read a file name from console
public static String getFilename() {
BufferedReader reader = null;
String file = null;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
file = reader.readLine();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
//read contents of the file, add each line to a list
//returns content of the file in a list form
public static List<String> getContent(String FileToRead) {
List<String> list = new ArrayList<>();
try {
BufferedReader lineReader = new BufferedReader(new FileReader(FileToRead));
while(lineReader.ready()) {
list.add(lineReader.readLine());
}
lineReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//Overwrite the file with the updated content
public static void createNewFile(String FileToOverwrite, String line) {
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FileToOverwrite));
bufferedWriter.write(line);
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createNewFile2(String overwrite ,List<String> list) {
try {
BufferedWriter bufferedWriter2 = new BufferedWriter(new FileWriter(overwrite));
for (int i = 0; i < list.size(); i++) {
bufferedWriter2.write(list.get(i));
if (i < list.size() - 1)
bufferedWriter2.newLine();
}
bufferedWriter2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}