package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Solution {
final static int ID_LENGTH = 8;
final static int PRODUCT_NAME_LENGTH = 30;
final static int PRICE_LENGTH = 8;
final static int QUANTITY_LENGTH = 4;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
if (args.length == 5 && args[0].equals("-u")) update(fileName, args[1], args[2], args[3], args[4]);
if (args.length == 2 && args[0].equals("-d")) delete(fileName, args[1]);
reader.close();
}
private static void delete(String file, String id) throws IOException {
List<String> list = new ArrayList<>();
int idLeng = ID_LENGTH - id.length();
if (id.length() < ID_LENGTH){
for (int i = 0; i < idLeng; i++) {
id += " ";
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file))));
String line;
while ((line = reader.readLine()) != null){
String idInFile = line.substring(0, ID_LENGTH);
if (!id.equals(idInFile)){
list.add(line);
}
}
reader.close();
PrintWriter pw = new PrintWriter(new FileWriter(file));
for (String s: list) {
pw.println(s);
}
pw.close();
}
public static void update(String file, String id, String productName, String price, String quantity) throws IOException {
List<String> list = new ArrayList<>();
int idLeng = ID_LENGTH - id.length();
if (id.length() < ID_LENGTH){
for (int i = 0; i < idLeng; i++) {
id += " ";
}
}
int prLength = PRODUCT_NAME_LENGTH - productName.length();
if (productName.length() < PRODUCT_NAME_LENGTH){
for (int i = 0; i < prLength; i++) {
productName += " ";
}
}
int priLength = PRICE_LENGTH - price.length();
if (price.length() < PRICE_LENGTH){
for (int i = 0; i < priLength; i++) {
price += " ";
}
}
int quaLength = QUANTITY_LENGTH - quantity.length();
if (quantity.length() < QUANTITY_LENGTH){
for (int i = 0; i < quaLength; i++) {
quantity += " ";
}
}
String toUpdate = id + productName + price + quantity;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file))));
String line;
while ((line = reader.readLine()) != null){
String idInFile = line.substring(0, ID_LENGTH);
if (id.equals(idInFile)){
list.add(toUpdate);
}else list.add(line);
}
reader.close();
PrintWriter pw = new PrintWriter(new FileWriter(file));
for (String s: list) {
pw.println(s);
}
pw.close();
}
}package com.javarush.task.task18.task1828;
/*
Прайсы 2
*/
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Solution {
final static int ID_LENGTH = 8;
final static int PRODUCT_NAME_LENGTH = 30;
final static int PRICE_LENGTH = 8;
final static int QUANTITY_LENGTH = 4;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
if (args.length == 5 && args[0].equals("-u")) update(fileName, args[1], args[2], args[3], args[4]);
if (args.length == 2 && args[0].equals("-d")) delete(fileName, args[1]);
reader.close();
}
private static void delete(String file, String id) throws IOException {
List<String> list = new ArrayList<>();
int idLeng = ID_LENGTH - id.length();
if (id.length() < ID_LENGTH){
for (int i = 0; i < idLeng; i++) {
id += " ";
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file))));
String line;
while ((line = reader.readLine()) != null){
String idInFile = line.substring(0, ID_LENGTH);
if (!id.equals(idInFile)){
list.add(line);
}
}
reader.close();
PrintWriter pw = new PrintWriter(new FileWriter(file));
for (String s: list) {
pw.println(s);
}
pw.close();
}
public static void update(String file, String id, String productName, String price, String quantity) throws IOException {
List<String> list = new ArrayList<>();
int idLeng = ID_LENGTH - id.length();
if (id.length() < ID_LENGTH){
for (int i = 0; i < idLeng; i++) {
id += " ";
}
}
int prLength = PRODUCT_NAME_LENGTH - productName.length();
if (productName.length() < PRODUCT_NAME_LENGTH){
for (int i = 0; i < prLength; i++) {
productName += " ";
}
}
int priLength = PRICE_LENGTH - price.length();
if (price.length() < PRICE_LENGTH){
for (int i = 0; i < priLength; i++) {
price += " ";
}
}
int quaLength = QUANTITY_LENGTH - quantity.length();
if (quantity.length() < QUANTITY_LENGTH){
for (int i = 0; i < quaLength; i++) {
quantity += " ";
}
}
String toUpdate = id + productName + price + quantity;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file))));
String line;
while ((line = reader.readLine()) != null){
String idInFile = line.substring(0, ID_LENGTH);
if (id.equals(idInFile)){
list.add(toUpdate);
}else list.add(line);
}
reader.close();
PrintWriter pw = new PrintWriter(new FileWriter(file));
for (String s: list) {
pw.println(s);
}
pw.close();
}
}