package com.javarush.task.task18.task1828;
import java.io.*;
import java.util.*;
public class Solution {
static {
}
public static void main(String[] args) throws Exception {
switch (args[0]) {
case "-u":
update(args);
break;
case "-d":
delete(args);
break;
}
}
public static void update(String[] args) throws IOException {
if (args.length != 0) {
ArrayList<String> list = new ArrayList<>();
int searchedId = Integer.parseInt(args[1]);
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
scanner.close();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.ready()) {
String str = reader.readLine();
if (!str.equals(""))
list.add(str);
}
reader.close();
for (int i=0; i < list.size(); i++){
String str = list.get(i);
int id = Integer.parseInt(str.substring(0, 8).trim());
if (id == searchedId){
for (int j = args[2].length(); j < 30; j++) {
args[2] = args[2] + " ";
}
for (int j = args[3].length(); j < 8; j++) {
args[3] = args[3] + " ";
}
for (int j = args[4].length(); j < 4; j++) {
args[4] = args[4] + " ";
}
for (int j = args[1].length(); j < 8; j++) {
args[1] = args[1] + " ";
}
str = args[1] + args[2] + args[3] + args[4];
list.set(i, str);
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (String str : list) {
writer.write(str);
writer.newLine();
}
writer.close();
}
}
public static void delete(String[] args) throws IOException{
ArrayList<String> list = new ArrayList<>();
int searchedId = Integer.parseInt(args[1]);
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
scanner.close();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while (reader.ready()) {
String str = reader.readLine();
if (!str.equals(""))
list.add(str);
}
reader.close();
for (int i=0; i < list.size(); i++) {
String str = list.get(i);
int id = Integer.parseInt(str.substring(0, 8).trim());
if (id == searchedId) {
list.remove(i);
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (String str : list) {
writer.write(str);
writer.newLine();
}
writer.close();
}
}