I tested on my files and it is working but not passing third and forth condition. Can someone see mistake ?
if (args.length > 0 && (args[0].equals("-u") || args[0].equals("-d"))) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
reader.close();
FileInputStream inputStream = new FileInputStream(file);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
Map<String, String> map = new TreeMap<>();
while (fileReader.ready()) {
String line = fileReader.readLine();
String tempID = line.substring(0, 8);
String rest = line.substring(8);
map.put(tempID, rest);
}
fileReader.close();
inputStream.close();
int mapSize = map.size();
String id = args[1];
if (id.length() < 8) {
for (int i = id.length(); i < 8; i++) {
id += " ";
}
}
String empty = "";
FileOutputStream outputStreamDelete = new FileOutputStream(file);
outputStreamDelete.write(empty.getBytes());
outputStreamDelete.close();
FileOutputStream outputStream = new FileOutputStream(file, true);
switch (args[0]) {
case "-u":
int countU = 0;
String productName = args[2];
if (productName.length() < 30) {
for (int i = productName.length(); i < 30; i++) {
productName += " ";
}
}
String price = args[3];
if (price.length() < 8) {
for (int i = price.length(); i < 8; i++) {
price += " ";
}
}
String quantity = args[4];
if (quantity.length() < 4) {
for (int i = quantity.length(); i < 4; i++) {
quantity += " ";
}
}
String updatedValue = productName + price + quantity;
for (Map.Entry<String, String> data : map.entrySet()) {
String key = data.getKey();
String oldDAta = data.getValue();
if (key.equals(id)) {
countU++;
String update = key + updatedValue;
outputStream.write(update.getBytes());
if (countU < mapSize) {
outputStream.write(System.lineSeparator().getBytes());
}
}else {
countU++;
String old = key + oldDAta;
outputStream.write(old.getBytes());
if (countU < mapSize) {
outputStream.write(System.lineSeparator().getBytes());
}
}
}
outputStream.close();
break;
case "-d" :
map.remove(id);
int countD = 0;
for (Map.Entry<String, String> data : map.entrySet()) {
String key = data.getKey();
String oldDAta = data.getValue();
countD++;
String allArguments = key + oldDAta;
outputStream.write(allArguments.getBytes());
if (countD < mapSize) {
outputStream.write(System.lineSeparator().getBytes());
}
}
outputStream.close();
break;
}
}
}
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static void main(String[] args) throws IOException {
if (args.length > 0 && (args[0].equals("-u") || args[0].equals("-d"))) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
reader.close();
FileInputStream inputStream = new FileInputStream(file);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
Map<String, String> map = new HashMap<>();
while (fileReader.ready()) {
String line = fileReader.readLine();
String tempID = line.substring(0, 8);
String rest = line.substring(8);
map.put(tempID, rest);
}
fileReader.close();
inputStream.close();
String id = args[1];
String productName = args[2];
String price = args[3];
String quantity = args[4];
String updatedValue = productName + price + quantity;
String empty = "";
FileOutputStream outputStreamDelete = new FileOutputStream(file);
outputStreamDelete.write(empty.getBytes());
outputStreamDelete.close();
FileOutputStream outputStream = new FileOutputStream(file, true);
switch (args[0]) {
case "-u":
ArrayList<String> lines = new ArrayList<>();
for (Map.Entry<String, String> data : map.entrySet()) {
String key = data.getKey();
String oldDAta = data.getValue();
if (key.equals(id)) {
String update = key + updatedValue;
lines.add(update);
}else {
String old = key + oldDAta;
lines.add(old);
}
}
for (int i=0; i < lines.size(); i++) {
if (i == lines.size()-1) {
outputStream.write(lines.get(i).getBytes());
} else {
outputStream.write(lines.get(i).getBytes());
outputStream.write(System.lineSeparator().getBytes());
}
}
outputStream.close();
break;
case "-d" :
map.remove(id);
ArrayList<String> lines2 = new ArrayList<>();
for (Map.Entry<String, String> data : map.entrySet()) {
String key = data.getKey();
String oldDAta = data.getValue();
if (id.equals(key)) {
} else {
String allArguments = key + oldDAta;
lines2.add(allArguments);
}
}
for (int i=0; i < lines2.size(); i++) {
if (i == lines2.size()-1) {
outputStream.write(lines2.get(i).getBytes());
} else {
outputStream.write(lines2.get(i).getBytes());
outputStream.write(System.lineSeparator().getBytes());
}
}
outputStream.close();
break;
}
}
}
}