ΠΠΎΠ΄ΡΠΊΠ°ΠΆΠΈΡΠ΅, ΠΏΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, Π² ΡΠ΅ΠΌ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ°. Π― Π½Π΅ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΠΏΠΎΠ½ΠΈΠΌΠ°Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΡΠ°Π·Ρ "ΠΡΠΈ Π·Π°ΠΏΡΡΠΊΠ΅ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ Ρ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ "-c productName price quantity" Π² ΠΊΠΎΠ½Π΅Ρ ΡΠ°ΠΉΠ»Π° Π΄ΠΎΠ»ΠΆΠ½Π° Π΄ΠΎΠ±Π°Π²ΠΈΡΡΡ Π½ΠΎΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° Ρ ΡΠΎΠ²Π°ΡΠΎΠΌ."? ΠΠ»ΠΈ ΠΊΠΎΠ΄ ΡΠΎΠ²ΡΠ΅ΠΌ Π½Π΅ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΡΠΉ?
package com.javarush.task.task18.task1827;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
ΠΡΠ°ΠΉΡΡ
*/
public class Solution {
public static void main(String[] args) throws Exception {
if (args.length > 0 && args[0].equals("-c")) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String address = bufferedReader.readLine();
bufferedReader = new BufferedReader(new FileReader(address));
String currentLine;
int maxID = 0;
while ((currentLine = bufferedReader.readLine()) != null) {
int currentID = Integer.parseInt((currentLine.substring(0, 8)).trim());
if (currentID > maxID)
maxID = currentID;
}
maxID++;
String maxIDresult = Integer.toString(maxID);
if (maxIDresult.length() < 8) {
while (maxIDresult.length() < 8)
maxIDresult += " ";
}
else if (maxIDresult.length() > 8)
maxIDresult.substring(0, 8);
bufferedReader.close();
String nameOfProduct = args[1];
if (args.length > 4) {
for (int i = 2; i < args.length-2; i++)
nameOfProduct += " " + args[i];
}
if (nameOfProduct.length() < 30) {
while (nameOfProduct.length() < 30)
nameOfProduct += " ";
}
else if (nameOfProduct.length() > 30)
nameOfProduct.substring(0, 30);
String price = args[args.length - 2];
if (price.length() < 8) {
while (price.length() < 8)
price += " ";
} else if (price.length() > 8)
price.substring(0, 8);
String quantity = args[args.length - 1];
if (quantity.length() < 4) {
while (quantity.length() < 4)
quantity += " ";
} else if (quantity.length() > 4)
quantity.substring(0, 4);
String result = maxIDresult + nameOfProduct + price + quantity;
FileWriter fileWriter = new FileWriter(address);
fileWriter.write('\n');
fileWriter.write(result);
fileWriter.close();
}
else return;
}
}