The program won't verify, I have tested it with a file that has the example given and the argument "-u", "19847 ", "Swim trunks, blue ", "159.00 ", "20 ".
The program runs but the file is empty.
I don't understand why it is empty because I have checked the program and it seems ok.
package com.codegym.task.task18.task1828;
/*
Prices 2
*/
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
public static class Product {
int id;
String name;
String price;
String quantity;
@Override
public String toString(){
return String.format("%-8d%-30s%-8s%-4s", id, name, price, quantity);
}
public Product(int id, String name, String price, String quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public int getId() {
return this.id;
}
public void setId(int id){
this.id = id;
}
}
public static void main(String[] args) throws Exception {
if (args.length == 0) return;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
bufferedReader.close();
BufferedReader bufferedFileReader = new BufferedReader(new FileReader(fileName));
Map<Integer, Product> productMap = new TreeMap<>();
while (bufferedFileReader.ready()) {
String productFromFile = bufferedFileReader.readLine();
int id = Integer.valueOf(productFromFile.substring(0, 8).trim());
String name = productFromFile.substring(8, 38).trim();
String price = productFromFile.substring(38, 46).trim();
String quantity = productFromFile.substring(46, 50).trim();
Product product = new Product(id, name, price, quantity);
productMap.put(id, product);
}
for (Map.Entry<Integer, Product> entry : productMap.entrySet()){
System.out.println(entry.toString());
}
bufferedFileReader.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
switch (args[0]) {
case "-u":
if (args[1] == null || args[2] == null || args[3] == null || args[4] == null) return;
Product productUpdate = productMap.get(Integer.valueOf(args[1]));
productUpdate.name = args[2];
productUpdate.price = args[3];
productUpdate.quantity = args[4];
productMap.replace(productUpdate.id, productUpdate);
for (Map.Entry<Integer, Product> entry : productMap.entrySet()){
bufferedWriter.write(entry.toString());
bufferedWriter.write("/n");
}
case "-d":
if (args[1] == null || args[2] == null || args[3] == null || args[4] == null) return;
productMap.remove(args[1]);
for (Map.Entry<Integer, Product> entry : productMap.entrySet()){
bufferedWriter.write(entry.toString());
}
}
bufferedWriter.close();
}
}