Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Prob4ShoppingSpree;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- *
- * @author kalin
- */
- public class Prob4ShoppingSpree {
- //([A-Za-z]+)\s*=\s*([0-9]+)
- public static void main(String[] args) {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {
- String[] input = reader.readLine().trim().split(";");
- Pattern patt = Pattern.compile("([A-Za-z]+)\\s*=\\s*([-]*[0-9]+)");
- List<Person> persons = new ArrayList<>();
- List<Product> products = new ArrayList<>();
- for (int i = 0; i < input.length; i++) {
- Matcher match = patt.matcher(input[i]);
- while (match.find()) {
- persons.add(new Person(match.group(1), Integer.parseInt(match.group(2))));
- }
- }
- input = reader.readLine().trim().split(";");
- for (int i = 0; i < input.length; i++) {
- Matcher match = patt.matcher(input[i]);
- while (match.find()) {
- products.add(new Product(match.group(1), Integer.parseInt(match.group(2))));
- }
- }
- input = reader.readLine().trim().split(" ");
- while (!input[0].equals("END")) {
- String personName = input[0];
- String productName = input[1];
- Person curentPerson = persons.stream().filter(p -> p.getName().equals(personName)).findFirst().orElse(null);
- Product curentProduct = products.stream().filter(p -> p.getName().equals(productName)).findFirst().orElse(null);
- curentPerson.boughtProduct(curentProduct);
- input = reader.readLine().trim().split(" ");
- }
- persons.forEach(p ->
- {System.out.printf("%s - %s%n", p.getName(), p.getBagOfProducts().size() > 0 ? p.getBagOfProducts().toString().replace("[", "").replace("]", "") : "Nothing bought");});
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class Person {
- private String name;
- private int money;
- private ArrayList<String> bagOfProducts;
- public Person(String name, int money) {
- this.setName(name);
- this.setMoney(money);
- this.bagOfProducts = new ArrayList<>();
- }
- private void setName(String name) {
- if (name.isEmpty() || name.trim().length() == 0) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- private void setMoney(int money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.money = money;
- }
- public String getName() {
- return name;
- }
- public void boughtProduct(Product product) {
- if ((this.money - product.getCost()) < 0) {
- System.out.printf("%s can't afford %s%n", this.name, product.getName());
- } else {
- this.money -= product.getCost();
- this.bagOfProducts.add(product.getName());
- System.out.printf("%s bought %s%n", this.name, product.getName());
- }
- }
- public ArrayList<String> getBagOfProducts() {
- return bagOfProducts;
- }
- @Override
- public String toString() {
- return "Person{" + "name=" + name + '}';
- }
- }
- class Product {
- private String name;
- private int cost;
- public Product(String name, int cost) {
- this.setName(name);
- this.setCost(cost);
- }
- private void setName(String name) {
- if (name.isEmpty() || name.trim().length() == 0) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- private void setCost(int cost) {
- if (cost < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.cost = cost;
- }
- public String getName() {
- return name;
- }
- public int getCost() {
- return cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment