package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
String key = args[0], id = args[1], productName = args[2], price = args[3], quantity = args[4];
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> idList = new ArrayList<>();
int place;
//считать с файла
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
FileInputStream fileReader = new FileInputStream(file);
byte[] buf = new byte[fileReader.available()];
while (fileReader.available() > 0) fileReader.read(buf);
fileReader.close();
//считать с файла
FileOutputStream bufferedWriter = new FileOutputStream(file);
for (String x : new String(buf).split("\n")) {
idList.add(Integer.parseInt(x.substring(0, 8).replace(" ", ""))); //все id из файла в idList
list.add(x);// все строки из файла без переносов
}
place = idList.indexOf(Integer.parseInt(id.replace(" ", "")));
//входящие параметры выровнять до нужной длины
if (id.length() < 8) {
while (id.length() < 8) id = id + " ";
} else
id = id.substring(0, 8);
if (productName.length() < 30) {
while (productName.length() < 30) productName = productName + " ";
} else
productName = productName.substring(0, 30);
if (price.length() < 8) {
while (price.length() < 8) price = price + " ";
} else
price = price.substring(0, 8);
if (quantity.length() < 4) {
while (quantity.length() < 4) quantity = quantity + " ";
} else
quantity = quantity.substring(0, 4);
//входящие параметры выровнять до нужной длины
switch (key) {
case "-u" :
String inputString = id + productName + price + quantity;
list.set(place, inputString);
for (int i = 0; i < list.size() - 1; i++)
bufferedWriter.write((list.get(i) + "\n").getBytes());
bufferedWriter.write(list.get(list.size() - 1).getBytes());
break;
case "-d" :
list.remove(place);
for (int i = 0; i < list.size() - 1; i++)
bufferedWriter.write((list.get(i) + "\n").getBytes());
bufferedWriter.write(list.get(list.size() - 1).getBytes());
break;
default:
bufferedWriter.write(buf);
break;
}
bufferedWriter.close();
}
}
package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
String key = args[0], id = args[1], productName = args[2],
price = args[3], quantity = args[4];
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> idList = new ArrayList<>();
int place;
//считать с файла
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file = reader.readLine();
FileInputStream fileReader = new FileInputStream(file);
byte[] buf = new byte[fileReader.available()];
while (fileReader.available() > 0) fileReader.read(buf);
fileReader.close();
//считать с файла
FileOutputStream bufferedWriter = new FileOutputStream(file);
for (String x : new String(buf).split("\n")) {
idList.add(Integer.parseInt(x.substring(0, 8).replace(" ", ""))); //все id из файла без пробелов
list.add(x);// все строки из файла
}
place = idList.indexOf(Integer.parseInt(id.replace(" ", "")));
//входящие параметры выровнять до нужной длины
if (id.length() < 8) {
while (id.length() < 8) id = id + " ";
} else
id = id.substring(0, 8);
if (productName.length() < 30) {
while (productName.length() < 30) productName = productName + " ";
} else
productName = productName.substring(0, 30);
if (price.length() < 8) {
while (price.length() < 8) price = price + " ";
} else
price = price.substring(0, 8);
if (quantity.length() < 4) {
while (quantity.length() < 4) quantity = quantity + " ";
} else
quantity = quantity.substring(0, 4);
//входящие параметры выровнять до нужной длины
switch (key) {
case "-u" :
String inputString = id + productName + price + quantity;
list.set(place, inputString);
for (int i = 0; i < list.size() - 1; i++)
bufferedWriter.write((list.get(i) + "\n").getBytes());
bufferedWriter.write(list.get(list.size() - 1).getBytes());
break;
case "-d" :
list.remove(place);
for (int i = 0; i < list.size() - 1; i++)
bufferedWriter.write((list.get(i) + "\n").getBytes());
bufferedWriter.write(list.get(list.size() - 1).getBytes());
break;
default:
bufferedWriter.write(buf);
break;
}
bufferedWriter.close();
}
}