package Prob04; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Person { private String name; private int money; private List bag; public Person(String name, int money) { this.setName(name); this.setMoney(money); this.bag = new ArrayList<>(); } public String getName() { return name; } public void setName(String name) { if(name.isEmpty()){ throw new IllegalArgumentException("Name cannot be empty"); } this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { if(money < 0){ throw new IllegalArgumentException("Money cannot be negative"); } this.money = money; } public List getBag() { return bag; } public void setBag(Product product) { this.bag.add(product); } @Override public String toString() { String str = String.format("%s - ", this.name); if(this.bag.size() > 0){ for(Product pr : this.bag){ str += pr.getName() + ", "; } str = str.substring(0, str.length() - 2); } else{ str = str + "Nothing bought"; } return str; } } class Product { private String name; private int cost; public Product(String name, int cost) { this.setName(name); this.setCost(cost); } public String getName() { return name; } public void setName(String name) { if(name.isEmpty()){ throw new IllegalArgumentException("Name cannot be empty"); } this.name = name; } public int getCost() { return cost; } public void setCost(int cost) { if(cost < 0){ throw new IllegalArgumentException("Money cannot be negative"); } this.cost = cost; } } public class Prob04Main { public static void main(String[] args) throws IOException { BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in)); String clientsLine = scanner.readLine(); String productsLine = scanner.readLine(); String productAndClientLine = scanner.readLine(); String[] splitClients = clientsLine.split(";"); String[] splitProducts = productsLine.split(";"); List clientsList = new ArrayList<>(); List productsList = new ArrayList<>(); try { for (int i = 0; i < splitClients.length; i++) { String[] splitClient = splitClients[i].split("="); String name = splitClient[0]; int money = Integer.parseInt(splitClient[1]); Person person = new Person(name, money); clientsList.add(person); } for (int i = 0; i < splitProducts.length; i++) { if (!(splitClients[i].isEmpty() || splitClients[i] == null)) { String[] splitProduct = splitProducts[i].split("="); String name = splitProduct[0]; int cost = Integer.parseInt(splitProduct[1]); Product product = new Product(name, cost); productsList.add(product); } } } catch(IllegalArgumentException ex){ System.out.println(ex.getMessage()); } while(!productAndClientLine.equals("END")){ String[] splitTheLine = productAndClientLine.split(" "); String client = splitTheLine[0]; String product = splitTheLine[1]; for (Person p : clientsList){ if(p.getName().equals(client)){ for(Product pr : productsList){ if(pr.getName().equals(product)){ if(p.getMoney() >= pr.getCost()){ try { p.setMoney(p.getMoney() - pr.getCost()); } catch(IllegalArgumentException ex){ System.out.println(ex.getMessage()); } p.setBag(pr); System.out.println(p.getName() + " bought " + pr.getName()); } else{ System.out.println(p.getName() + " can't afford " + pr.getName()); } } } } } productAndClientLine = scanner.readLine(); } for(Person p : clientsList){ System.out.println(p.toString()); } } }