Advertisement
jaVer404

level18.lesson10.bonus02(sad pinguin)

Feb 6th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.bonus02;
  2.  
  3. /* Прайсы
  4. CrUD для таблицы внутри файла
  5. Считать с консоли имя файла для операций CrUD
  6. Программа запускается со следующим набором параметров:
  7. -c productName price quantity
  8.  
  9. Значения параметров:
  10. где id - 8 символов
  11. productName - название товара, 30 chars (60 bytes)
  12. price - цена, 8 символов
  13. quantity - количество, 4 символа
  14. -c  - добавляет товар с заданными параметрами в конец файла, генерирует id самостоятельно, инкрементируя максимальный id, найденный в файле
  15.  
  16. В файле данные хранятся в следующей последовательности (без разделяющих пробелов):
  17. id productName price quantity
  18. Данные дополнены пробелами до их длины
  19.  
  20. Пример:
  21. 19846   Шорты пляжные синие           159.00  12
  22. 198478  Шорты пляжные черные с рисунко173.00  17
  23. 19847983Куртка для сноубордистов, разм10173.991234
  24. */
  25.  
  26. import java.io.BufferedReader;
  27. import java.io.FileReader;
  28. import java.io.InputStreamReader;
  29.  
  30. public class Solution
  31. {
  32.     public static void main(String[] args) throws Exception
  33.     {
  34.         if (args[0].equals("-c") && args.length >= 4)
  35.         {
  36.             BufferedReader userInput = null;
  37.             BufferedReader fileReader = null;
  38.             String fileName;
  39.             int max = 0;
  40.             try
  41.             {
  42.                 userInput = new BufferedReader(new InputStreamReader(System.in));
  43.                 fileName = userInput.readLine();
  44.                 userInput.close();
  45.                 fileReader = new BufferedReader(new FileReader(fileName));
  46.                 String fromFile;
  47.                 String cutFromFile;
  48.                 int temp = 0;
  49.                 while ((fromFile = fileReader.readLine()) != null)
  50.                 {
  51.                     cutFromFile = (fromFile.substring(0, 8)).trim();
  52.                     try
  53.                     {
  54.                         temp = Integer.parseInt(cutFromFile);
  55.                         if (temp > max)
  56.                         {
  57.                             max = temp;
  58.                         }
  59.                     }
  60.                     catch (Exception e)
  61.                     {
  62.                     }
  63.                 }
  64.             }
  65.             catch (Exception e)
  66.             {
  67.             }
  68.             finally/*тут закрываем потоки, если они существуют*/
  69.             {
  70.                 if (userInput != null)
  71.                 {
  72.                     userInput.close();
  73.                 }
  74.                 if (fileReader != null)
  75.                 {
  76.                     fileReader.close();
  77.                 }
  78.             }
  79.             max++;
  80.             /*Now we have ID number*/
  81.             String writeToFile =
  82.                     formNewPrice(max + "")
  83.                             + formNewProdName(args[1])
  84.                             + formNewPrice(args[2])
  85.                             + formNewQuan(args[3]);
  86.         }
  87.     }
  88.  
  89.     public static String formNewProdName(String input)
  90.     {
  91.         if (input.length() > 30)
  92.         {
  93.             return input.substring(0, 30);
  94.         }
  95.  
  96.         return String.format("%-30s", input);
  97.     }
  98.  
  99.     public static String formNewPrice(String input)
  100.     {
  101.         if (input.length() > 8)
  102.         {
  103.             return input.substring(0, 8);
  104.         }
  105.         return String.format("%-8s", input);
  106.     }
  107.  
  108.     public static String formNewQuan(String input)
  109.     {
  110.         if (input.length() > 4)
  111.         {
  112.             return input.substring(0, 4);
  113.         }
  114.         return String.format("%-4s",input);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement