Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.task.task18.task1828;
- import java.io.*;import java.util.ArrayList;
- public class Solution { public static void main(String[] args) throws Exception { String param = args[0]; String id = args[1]; BufferedReader fileNamereader = new BufferedReader(new InputStreamReader(System.in)); String fileName = fileNamereader.readLine();
- ArrayList<String> arr = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader(fileName)); String s; while ((s = reader.readLine()) != null) { arr.add(s); } reader.close();
- String rowForUpdateOrDelete = ""; for (String obj : arr) { if (obj.substring(0, 8).trim().equals(id)) rowForUpdateOrDelete = obj; } if (!rowForUpdateOrDelete.equals("")) { if (param.equals("-u")) { String productName = args[2]; String price = args[3]; String quantity = args[4];
- int indexOfForManipulating = arr.indexOf(rowForUpdateOrDelete);
- int m = 30 - productName.length(); for (int i = 0; i < m; i++) { productName += " "; }
- m = 8 - price.length(); for (int i = 0; i < m; i++) { price += " "; }
- m = 4 - quantity.length(); for (int i = 0; i < m; i++) { quantity += " "; }
- m = 8 - id.length(); for (int i = 0; i < m; i++) { id += " "; }
- String newRow = id + productName + price + quantity; arr.set(indexOfForManipulating, newRow); }
- if (param.equals("-d")) { int indexOfForManipulating = arr.indexOf(rowForUpdateOrDelete); arr.remove(indexOfForManipulating); } }
- BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); for (int i = 0; i < arr.size(); i++) { writer.write(arr.get(i)); if(i != (arr.size()-1)) {writer.newLine();} } writer.close(); } }
Add Comment
Please, Sign In to add comment