Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. package com.javarush.task.task18.task1828;
  2.  
  3. /*
  4. Прайсы 2
  5. */
  6.  
  7. import java.io.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class Solution {
  14. public static void main(String[] args) throws Exception {
  15.  
  16. // VALIDATOR AREA
  17. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18. String fileName = reader.readLine();
  19. reader.close();
  20.  
  21. // TEST AREA
  22. // args = new String[]{"-c", "Linen Shirt", "99.80", "15"};
  23. // args = new String[]{"-d", "198479"};
  24. // args = new String[]{"-r", "19847983"};
  25. // args = new String[]{"-u", "19847", "Bagel", "9.99", "1"};
  26. // String fileName = "res/002/base.txt";
  27.  
  28. if (args.length != 0) {
  29. switch (args[0]) {
  30. case "-c": {
  31. createEntry(args[1], args[2], args[3], fileName);
  32. break;
  33. }
  34. case "-r": {
  35. readEntry(args[1], fileName);
  36. break;
  37. }
  38. case "-u": {
  39. updateEntry(args[1], args[2], args[3], args[4], fileName);
  40. break;
  41. }
  42. case "-d": {
  43. deleteEntry(args[1], fileName);
  44. break;
  45. }
  46. default: {
  47. break;
  48. }
  49.  
  50. }
  51. }
  52. }
  53.  
  54. private static void createEntry(String productName,
  55. String price,
  56. String quantity,
  57. String fileName) throws IOException {
  58. ArrayList<Product> products = loadProducts(fileName);
  59. products.add(new Product(Product.getNextId(products), productName, price, quantity));
  60. saveProducts(fileName, products);
  61. }
  62.  
  63. private static void readEntry(String id,
  64. String fileName) throws IOException {
  65. ArrayList<Product> products = loadProducts(fileName);
  66. for (Product p : products) {
  67. if (Product.applyLength(p.id, 8).equals(id)) {
  68. System.out.println(String.format("#%s | %s | $%s | Left: %s ",
  69. p.id, p.productName, p.price, p.quantity));
  70. }
  71. }
  72.  
  73. }
  74.  
  75. private static void updateEntry(String id,
  76. String productName,
  77. String price,
  78. String quantity,
  79. String fileName) throws IOException {
  80. ArrayList<Product> products = loadProducts(fileName);
  81. for (Product p : products) {
  82. if ((p.id).equals(Product.applyLength(id, 8))) {
  83. p.productName = Product.applyLength(productName, 30);
  84. p.price = Product.applyLength(price, 8);
  85. p.quantity = Product.applyLength(quantity, 4);
  86. }
  87. }
  88. saveProducts(fileName, products);
  89. }
  90.  
  91. private static void deleteEntry(String id,
  92. String fileName) throws IOException {
  93. ArrayList<Product> products = loadProducts(fileName);
  94. for (Product p : products) {
  95. if ((p.id).equals(Product.applyLength(id, 8))) {
  96. products.remove(p);
  97. }
  98. }
  99. saveProducts(fileName, products);
  100. }
  101.  
  102. private static void saveProducts(String fileName, ArrayList<Product> products) throws IOException { // writes file
  103. BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
  104. for (int i = 0; i < products.size(); i++) {
  105. writer.write(products.get(i).id);
  106. writer.write(products.get(i).productName);
  107. writer.write(products.get(i).price);
  108. writer.write(products.get(i).quantity);
  109. if (i != products.size() - 1) writer.write("\n");
  110. }
  111. writer.close();
  112. }
  113.  
  114. private static ArrayList<Product> loadProducts(String fileName) throws IOException { // loads products from file
  115. BufferedReader reader = new BufferedReader(new FileReader(fileName));
  116. ArrayList<Product> products = new ArrayList<>();
  117. while (true) {
  118. String line = reader.readLine();
  119. if (line == null || line.length() < 46) break;
  120. products.add(new Product(
  121. line.substring(0, 8),
  122. line.substring(8, 38),
  123. line.substring(38, 46),
  124. line.substring(46)));
  125. }
  126. reader.close();
  127. return products;
  128. }
  129.  
  130. public static class Product {
  131. private String id;
  132. private String productName;
  133. private String price;
  134. private String quantity;
  135.  
  136. Product(String id, String productName, String price, String quantity) {
  137. this.id = applyLength(id, 8);
  138. this.productName = applyLength(productName, 30);
  139. this.price = applyLength(price, 8);
  140. this.quantity = applyLength(quantity, 4);
  141. }
  142.  
  143. static String getNextId(List<Product> products) { // finds max ID and returns next
  144. int maxId = 0;
  145. for (Product p : products) {
  146. Pattern pattern = Pattern.compile("(\\d+)");
  147. Matcher matcher = pattern.matcher(p.id);
  148. if (matcher.find()) {
  149. int id = Integer.parseInt(matcher.group(1));
  150. maxId = Math.max(id, maxId);
  151. }
  152. }
  153. return String.valueOf(++maxId);
  154. }
  155.  
  156. static String applyLength(String original, int length) { // applies target length to any string
  157. if (original.length() <= length) {
  158. StringBuffer sb = new StringBuffer(60);
  159. return new String(sb.append(original).append(" ".repeat(Math.max(0, length - original.length()))));
  160. } else {
  161. return original.substring(0, 30);
  162. }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement