I am studying Java. Can you criticize my code and tell me what I need to do to make it better?
I have code that reads a txt file(product base). It contains tabular data where lines are formatted as:
id productName price quantity
We launch the code with:
-u id productName price quantity
- update line;
or
-d id
- delete line.
So the logic is to find this line, create new File with updated line or without it, delete base file and rename new file.
Last important thing: every element on textbase has own weight. It's int []argsLength
. If the element size less that its weight, we fill empty space with whitespaces.
simple for test(indents is correct):
1 Recorder 100.00 12
212 Rocket 182.00 400
99333 Hat 4500.00 5
1984711 Crocodile 2.5 4339
13247983Pistol 53500.903
https://ru.files.fm/u/5q3zb94a
main:
public class CRUD {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = reader.readLine();
BufferedReader fileReader = new BufferedReader(new FileReader(filename));
int[] argsLength = new int[]{0, 8, 30, 8, 4};
FileCreator creator = new FileCreatorFactory().getFileCreator(args[0], args, argsLength, filename);
String line;
if (creator == null) {
System.out.println("Unknow command");
return;
}
if ((line = creator.isLineIsset()) == null) {
System.out.println("Unknow ID");
return;
}
File resultFile = creator.createNewFile(line);
if (resultFile.length() == 0) {
System.out.println("Error");
return;
}
reader.close();
fileReader.close();
Files.delete(new File(filename).toPath());
System.out.println("Result:" + (resultFile.renameTo(new File(filename))));
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileCreatorFactory:
package ru.kirstentasks.filecreator;
public class FileCreatorFactory {
public FileCreator getFileCreator(String arg,String[] args, int[] argsMaxLength,String filename) {
switch (arg) {
case "-u":
return new idUpdater(filename,args,argsMaxLength);
case "-d":
return new idDeleter(filename,args,argsMaxLength);
default:
return null;
}
}
}
FileCreator:
public abstract class FileCreator {
protected String[] args;
protected int[] argsMaxLength;
protected String fileName;
FileCreator(String fileName, String[] args, int[] argsMaxLength) {
this.args = args;
this.argsMaxLength = argsMaxLength;
this.fileName = fileName;
}
public String isLineIsset() throws IOException {
String result;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (!((result = reader.readLine()) == null)) {
if (args[1].trim().equals(result.substring(0, argsMaxLength[1]).trim())) {
reader.close();
return result;
}
}
reader.close();
return null;
}
public abstract File createNewFile(String line);
}
idDeleter:
package ru.kirstentasks.filecreator;
import java.io.*;
public class idDeleter extends FileCreator {
private String fileName;
idDeleter(String fileName, String[] args, int[] argsMaxLength) {
super(fileName, args, argsMaxLength);
this.fileName = fileName;
}
@Override
public File createNewFile(String line) {
BufferedReader fileReader;
BufferedWriter fileWriter;
File tempFile = new File(fileName + ".temp");
try {
fileReader = new BufferedReader(new FileReader(fileName));
fileWriter = new BufferedWriter(new FileWriter(tempFile));
String temp;
while (!((temp = fileReader.readLine()) == null)) {
if (temp.equals(line)) {
continue;
}
fileWriter.write(temp);
fileWriter.newLine();
}
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return tempFile;
}
}
idUpdater:
package ru.kirstentasks.filecreator;
import java.io.*;
public class idUpdater extends FileCreator {
private String filename;
idUpdater(String filename, String[] args, int[] argsMaxLength) {
super(filename, args, argsMaxLength);
this.filename = filename;
}
@Override
public File createNewFile(String line) {
BufferedReader fileReader;
BufferedWriter fileWriter;
File tempFile = new File(filename + ".temp");
try {
fileReader = new BufferedReader(new FileReader(filename));
fileWriter = new BufferedWriter(new FileWriter(tempFile));
String temp;
while (!((temp = fileReader.readLine()) == null)) {
if (temp.equals(line)) {
temp = createLine(args, argsMaxLength);
}
fileWriter.write(temp);
fileWriter.newLine();
}
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return tempFile;
}
private String createLine(String[] args, int[] argsLength) {
if (args.length != argsLength.length) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i < args.length; i++) {
sb.append(String.format("%-" + argsLength[i] + "s", args[i]));
}
return sb.toString();
}
}