Created
July 16, 2016 09:34
-
-
Save josinSbazin/34cf049a219d2124a4a88e7423bf6d52 to your computer and use it in GitHub Desktop.
level18.lesson10.bonus03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level18.lesson10.bonus03; | |
/* Прайсы 2 | |
CrUD для таблицы внутри файла | |
Считать с консоли имя файла для операций CrUD | |
Программа запускается с одним из следующих наборов параметров: | |
-u id productName price quantity | |
-d id | |
Значения параметров: | |
где id - 8 символов | |
productName - название товара, 30 chars (60 bytes) | |
price - цена, 8 символов | |
quantity - количество, 4 символа | |
-u - обновляет данные товара с заданным id | |
-d - производит физическое удаление товара с заданным id (все данные, которые относятся к переданному id) | |
В файле данные хранятся в следующей последовательности (без разделяющих пробелов): | |
id productName price quantity | |
Данные дополнены пробелами до их длины | |
Пример: | |
19846 Шорты пляжные синие 159.00 12 | |
198478 Шорты пляжные черные с рисунко173.00 17 | |
19847983Куртка для сноубордистов, разм10173.991234 | |
*/ | |
import java.io.*; | |
import java.util.ArrayList; | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
if (args.length == 0) return; | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
File file = new File(reader.readLine()); | |
reader.close(); | |
System.out.println(updateData(file, args)); | |
} | |
public static boolean updateData(File file, String[] args) throws IOException { | |
if (file.length() == 0) return false; | |
int ok = 0; | |
FileReader fr = new FileReader(file); | |
BufferedReader fileReader = new BufferedReader(fr); | |
ArrayList<String> lines = new ArrayList<String>(); | |
String t; | |
while ((t = fileReader.readLine()) != null) { | |
if (t.length() > 7 && t.substring(0, 8).trim().matches(args[1])) { | |
if (args[0].equals("-u")) lines.add(getData(args)); | |
ok++; | |
} else { | |
lines.add(t); | |
} | |
} | |
fileReader.close(); | |
fr.close(); | |
if (ok > 0) { | |
writer(file, lines); | |
return true; | |
} else return false; | |
} | |
public static String getData(String[] args) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 2; i < args.length - 2; i++) { | |
sb.append(args[i]).append(" "); | |
} | |
String id = cut(args[1], 8); | |
String productName = cut(sb.toString().trim(), 30); | |
String price = cut(args[args.length - 2], 8); | |
String quantity = cut(args[args.length - 1], 4); | |
return id + productName + price + quantity; | |
} | |
public static String cut(String s, int n) { | |
if (s.length() > n) { | |
return s.substring(0, n); | |
} else { | |
String arg = "%-" + n + "s"; | |
return String.format(arg, s); | |
} | |
} | |
public static void writer(File file, ArrayList<String> lines) throws IOException { | |
if (lines.size() == 0) return; | |
FileWriter fw = new FileWriter(file); | |
fw.write(lines.get(0)); | |
if (lines.size() > 1) { | |
for (int i = 1; i < lines.size(); i++) { | |
fw.write("\r\n"); | |
fw.write(lines.get(i)); | |
} | |
} | |
fw.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment