I've been at it for hours. It's probably something small and I'm crosseyed and can no longer see it. Or of course it's major and I have to redo the whole thing. Again. Help please?
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
TreeMap<String, String> map = new TreeMap<>();
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
while (true) {
String data = fileReader.readLine();
if(data == null) {
break;
} else {
String id = data.substring(0,8);
String remainder = data.substring(9);
map.put(id, remainder);
}
}
fileReader.close();
if (args.length == 0) {
return;
}
if (args[0].equals("-u")){
String id = args[1];
String productName = args[2];
String price = args[3];
String quantity = args[4];
while (id.length() < 8) {
id += " ";
}
while (productName.length() < 30) {
productName += " ";
}
while (price.length() < 8) {
price += " ";
}
while (quantity.length() < 4) {
quantity += " ";
}
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> pair = iterator.next();
String key = pair.getKey().trim();
if(key.equals(args[1])) {
pair.setValue(productName + price + quantity);
}
}
FileOutputStream uFileOutput = new FileOutputStream(fileName);
for (Map.Entry<String, String> update : map.entrySet()) {
uFileOutput.write((update.getKey() + update.getValue()).getBytes());
}
uFileOutput.close();
}
if(args[0].equals("-d")) {
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> pair = iterator.next();
String key = pair.getKey().trim();
if (key.equals(args[1])) {
map.remove(pair);
}
}
FileOutputStream dFileOutput = new FileOutputStream(fileName);
for (Map.Entry<String, String> update : map.entrySet()) {
dFileOutput.write((update.getKey() + update.getValue()).getBytes());
}
dFileOutput.close();
}
}
}