Input data :

Иванов газета 10 Петров ручка 5 Николаев тетрадь 3 Иванов ручка 2 Николаев ручка 1 Петров тетрадь 2 Николаев газета 1 

Output :

 Иванов : газета - 10 шт ; ручка - 5 шт ; Николаев : газета - 1 шт ; ручка - 1 шт ; тетрадь - 3 шт; Петров : ручка - 5 шт ; тетрадь - 2 шт; 

Code :

 import java.io.*; import java.util.ArrayList; /** * * @author Марат */ public class Semnadcat2 { public static void main(String[] args) throws IOException { ArrayList<Customer> customers = new ArrayList<>(); BufferedReader bufferedReader = new BufferedReader(new FileReader("C://SomeDir//notes3.txt")); String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { customers.add(new Customer(currentLine)); } bufferedReader.close(); String [] a = new String[customers.size()]; for (int i = 0; i < customers.size(); i++) { Customer customer = customers.get(i); System.out.println(customer.getSurname() + " | " + customer.getProduct() + " | " + customer.getAmount()); a[i] = customer.getSurname(); } for (int i = 0; i < customers.size(); i++) { String k = a[i]; for (int j = 1; j < customers.size(); j++) { if (k.equals(a[j])) { a[j] = ""; } } System.out.println(a[i]); } } } class Customer { private String mSurname; private String mProduct; private int mAmount; public Customer(String line) { String[] customer = line.split(" "); mSurname = customer[0]; mProduct = customer[1]; mAmount = Integer.parseInt(customer[2]); } public String getSurname() { return mSurname; } public String getProduct() { return mProduct; } public int getAmount() { return mAmount; } } 

Errors:

It is not possible to get the necessary type of output data to the console.

    2 answers 2

    • To output by name, you need to group the Customer sets by that name. This can be done using HashMap , the key in which will be the name, and the value - the list of Customer .
    • To sum up the quantity of one type of goods, you need to check whether there is already such a product in the list.

    In the Customer class, add the addAmount method:

     public void addAmount(int amount) { mAmount += amount; } 

    Main class:

     public static void main(String[] args) { SortedMap<String, ArrayList<Customer>> nameToCustomers = new TreeMap<>(); try (BufferedReader bufferedReader = new BufferedReader(new FileReader("test.txt"))) { String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { Customer newCustomer = new Customer(currentLine); String name = newCustomer.mSurname; if (!nameToCustomers.containsKey(name)) { nameToCustomers.put(name, new ArrayList<>()); } ArrayList<Customer> customers = nameToCustomers.get(name); boolean found = false; for (Customer customer : customers) { if (customer.mProduct.equals(newCustomer.mProduct)) { customer.addAmount(newCustomer.getAmount()); found = true; break; } } if (!found) { customers.add(newCustomer); } } } catch (IOException e) { } for (String name : nameToCustomers.keySet()) { System.out.print(name + ": "); for (Customer customer : nameToCustomers.get(name)) { System.out.print(customer.getProduct() + " - " + customer.getAmount() + "; "); } System.out.println(); } } 
    • for example, this data entry: Ivanov paper 10 Petrov pens 5 Ivanov marker 3 Ivanov paper 7 Petrov envelope 20 Ivanov envelope 5 - Marat Zimnurov
    • he will deduce that Ivanov: paper - 10; marker - 3; paper - 7; envelope - 5; - Marat Zimnurov
    • and how to summarize the paper in this case? he writes out as separate fragments, but not as whole - Marat Zimnurov
    • @ MaratZimnurov about this was worth mentioning immediately in the question. I will finish the code. - Regent
    • one
      @Regent if you use SortedMap, you can do without sorting and reduce the number of containers to 1. - Mikhail Vaysman
     import java.util.*; import java.util.stream.Collectors; public class ListOfItems { public static void main(String[] args) { // вместо файла использовал строку - суть не меняется String input = "Иванов газета 10\n" + "Петров ручка 5\n" + "Николаев тетрадь 3\n" + "Николаев тетрадь 3\n" + "Иванов ручка 2\n" + "Иванов ручка 2\n" + "Николаев ручка 1\n" + "Петров тетрадь 2\n" + "Николаев газета 1\n"; SortedMap<String, Customer> people = new TreeMap<>(); StringTokenizer lines = new StringTokenizer(input, "\n"); while (lines.hasMoreElements()) { String line = lines.nextToken(); String[] columns = line.split(" "); // нашли или создали клиента Customer customer = people.computeIfAbsent(columns[0], Customer::new); // добавили клиенту элемент в список customer.addItem(columns[1], Integer.parseInt(columns[2])); } people.forEach((s, customer) -> System.out.println(customer)); } static class Customer { private String name; private Map<String, Item> items; public Customer(String name) { this.name = name; items = new HashMap<>(); } public void addItem(String name, int quantity) { Item item = items.computeIfAbsent(name, Item::new); item.changeQuantity(quantity); } @Override public String toString() { return name + " " + items.values().stream().map(Item::toString).collect(Collectors.joining(" ; ")); } } static class Item { private String name; private int quantity; public Item(String name) { this(name, 0); } public Item(String name, int quantity) { this.name = name; this.quantity = quantity; } public void changeQuantity(int delta) { quantity += delta; } @Override public String toString() { return name + " - " + quantity + "шт"; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Item)) return false; Item item = (Item) o; return name.equals(item.name); } @Override public int hashCode() { return name.hashCode(); } } } 
    • @Regent now has a summation. - Mikhail Vaysman