Greetings,
After my fair share of hours already spent on this, I finally have what seems to be the correct output, but the validator isn't accepting anything meaningful. I realize my code could be better organized into tidy little methods. Maybe I'll do that if no one is willing to cross their eyes looking at this code : ) Here's what I believe I have correctly done:
1) Taken in data from some fileName typed into the console, stored it in an ArrayList, then rewrote that into the same file.
2) Acknowledged "-c" as the first argument in the program arguments and initiated the program.
3) Taken into consideration length of each arg according to the specifications and modifying its length appropriately and storing it in new string.
4) Created a new line of data to be added to the end of the file based upon the length and content of each arg.
I figure I'll worry about the stupid .close() not working out once I have the more substantial items figured out.
Thanks in advance to any heroes out there!
Mike
package com.codegym.task.task18.task1827;
/*
Prices
The program is started with the following arguments:
-c productName price quantity
Argument values:
where id is 8 characters.
productName is 30 characters.
price is 8 characters.
quantity is 4 characters.
-c adds the product with the specified parameters to the end of the file, and generates an id by incrementing the maximum id found in the file.
The file data is stored in the following order (without separating spaces):
id productName price quantity
Each data field is padded with spaces up to its length.
Before adding a new line, re-write all of its contents to the file.
Example:
19847 Swim trunks, blue 159.00 12
198479 Swim trunks, black with printe173.00 17
19847983Snowboard jacket with reflecti10173.991234
Requirements:
2. The Solution class should not use static variables.
3. When you run the program without arguments, the product list must remain unchanged.
4. 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.
7. The file streams must be closed.
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//1. The program should read a file name for CrUD operations from the console.
String fileName = reader.readLine();
FileInputStream fileInputStream = new FileInputStream(fileName);
reader.close();
ArrayList<String> fileStrings = new ArrayList<>();
BufferedReader fileReader = new BufferedReader(new InputStreamReader(fileInputStream));
String fileLine = "";
if(args[0].equals("-c")) {
while ((fileLine = fileReader.readLine()) != null) {
System.out.println(fileLine);
fileStrings.add(fileLine);
}
fileInputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
for (String line : fileStrings) {
fileOutputStream.write((line + "\n").getBytes());
}
//So, we should have rewritten the file to itself by now.
//Now, we need to add a new line to the end of the file containing the args[n].
ArrayList<String> programArguments = new ArrayList<>();
for (int i = 1; i < args.length; i++) {
programArguments.add(args[i]);
//System.out.println("This is args[i] " + args[i]);
}
//Now, we have a list of program arguments to work with that need to be added to end of file.
ArrayList<Integer> fileIds = new ArrayList<>();
StringBuilder fileIdStringBuilder = new StringBuilder();
for (String fileLines : fileStrings) {
for (int i = 0; i < 8; i++) {
fileIdStringBuilder.append(fileLines.charAt(i));
}
fileIds.add(Integer.parseInt(fileIdStringBuilder.toString()));
//System.out.println("This is the file Id " + fileIdStringBuilder);
fileIdStringBuilder = new StringBuilder();
}
//Okay, we have our ids now. Find the maximum and iterate it.
int maxId = 0;
for (Integer element : fileIds) {
if (element > maxId)
maxId = element;
}
//System.out.println("This is the maxId " + maxId);
maxId++;
String maxIdString = maxId + "";
StringBuilder maxIdStringBuilder = new StringBuilder(maxIdString);
while (maxIdStringBuilder.length() < 8) {
maxIdStringBuilder.append(' ');
}
maxIdString = maxIdStringBuilder.toString();
//Now, we need to build the other components of the new entry--its productName, price, and quantity.
String productName = args[1];
StringBuilder productNameStringBuilder = new StringBuilder(args[1]);
if(productName.length() < 30) {
for (int i = 0; i < 30; i++) {
if (!Character.isLetterOrDigit(args[1].charAt(i)))
productNameStringBuilder.append(' ');
}
productName = productNameStringBuilder.toString();
}else{
productName = args[1].substring(0,30);
}
//System.out.println("This is the productName " + productName);
//So, now we should have a 30 character productName.
String price = args[2];
StringBuilder priceStringBuilder = new StringBuilder(args[2]);
//System.out.println("This is args[2] priceStringBuilder " + priceStringBuilder);
//If we need to add characters...
if (args[2].length() < 8) {
for (int i = 2; i <= 8; i++) {
if (args[2].length() < i) {
for (int k = 8; k >= i; k--) {
priceStringBuilder.append(' ');
}
price = priceStringBuilder.toString();
break;
} else {
//do nothing
}
}
//If it's the correct length.
} else if (args[2].length() == 8) {
//If we need to remove characters.
} else {
price = args[2].substring(0, 8);
}
//So, now we should have an 8 character price.
//System.out.println("This is the price " + price);
String quantity = args[3];
//System.out.println("This is args[3] " + args[3]);
StringBuilder quantityStringBuilder = new StringBuilder(args[3]);
if (args[3].length() < 4) {
for (int i = 2; i <= 4; i++) {
if (args[3].length() < i) {
for (int k = 4; k >= i; k--) {
quantityStringBuilder.append(' ');
}
quantity = quantityStringBuilder.toString();
break;
} else {
//do nothing
}
}
} else {
quantity = args[3].substring(0, 4);
}
//So, now we should have a 4 character quantity.
//System.out.println("This is the quantity " + quantity);
String stringToAdd = maxIdString + productName + price + quantity;
//System.out.println(stringToAdd);
fileOutputStream.write(stringToAdd.getBytes());
fileOutputStream.close();
}else{
//do nothing?
}
}
}