Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Main {
- public static void main(String[] args) throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, Person> people = new LinkedHashMap<>();
- Map<String, Product> products = new LinkedHashMap<>();
- try {
- createAndAddPeople(reader, people);
- createAndAddProducts(reader, products);
- String inLine = reader.readLine();
- while (!"END".equals(inLine)) {
- String[] inTokens = inLine.split("\\s+");
- String personName = inTokens[0];
- String productName = inTokens[1];
- if (people.containsKey(personName)) {
- if (products.containsKey(productName)) {
- Person person = people.get(personName);
- Product product = products.get(productName);
- try {
- person.buyProduct(product);
- } catch (IllegalArgumentException ex) {
- System.out.println(ex);
- }
- }
- }
- inLine = reader.readLine();
- }
- for (Map.Entry<String, Person> map : people.entrySet()) {
- Person person = map.getValue();
- if (person.getProducts().size() > 0) {
- System.out.println(person);
- } else {
- System.out.printf("%s - Nothing bought", person.getName());
- }
- }
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- }
- }
- private static void createAndAddProducts(BufferedReader reader, Map<String, Product> products) throws Exception {
- String[] tokens = reader.readLine().split(";");
- for (int i = 0; i < tokens.length; i++) {
- Product product = null;
- String[] inPersonTokens = tokens[i].split("=");
- String name = inPersonTokens[0];
- double price = Double.parseDouble(inPersonTokens[1]);
- product = new Product(name, price);
- products.put(name, product);
- }
- }
- private static void createAndAddPeople(BufferedReader reader, Map<String, Person> people) throws Exception {
- String[] tokens = reader.readLine().split(";");
- for (int i = 0; i < tokens.length; i++) {
- Person person = null;
- String[] inPersonTokens = tokens[i].split("=");
- String name = inPersonTokens[0];
- double money = Double.parseDouble(inPersonTokens[1]);
- person = new Person(name, money);
- people.put(name, person);
- }
- }
- }
- package test;
- import java.util.ArrayList;
- import java.util.List;
- public class Person {
- private String name;
- private double money; //second change money isnt int
- private List<Product> products;
- public Person(String name, double money) {
- setName(name);
- setMoney(money);
- this.products = new ArrayList<>();
- }
- private void setName(String name) {
- if (name == null || name.trim().isEmpty()) { //3th change
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- private void setMoney(double money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- } else {
- this.money = money;
- }
- }
- public double getMoney() {
- return this.money;
- }
- public List<Product> getProducts() {
- return this.products;
- }
- public void buyProduct(Product product) {
- if (this.getMoney() < product.getPrice()) {
- System.out.printf("%s can't afford %s%n", this.getName(), product.getName());
- } else {
- this.getProducts().add(product);
- this.money -= product.getPrice();
- System.out.println(this.getName() + " bought " + product.getName());
- }
- }
- @Override
- public String toString() {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(this.name);
- stringBuilder.append(" - ");
- stringBuilder.append(String.join(", ", this.getProducts().stream()
- .map(p -> p.getName()).toArray(String[]::new)));
- return stringBuilder.toString();
- }
- }
- package test;
- public class Product {
- private String name;
- private double price;
- public Product(String name, double price) {
- setName(name);
- setPrice(price);
- }
- private void setName(String name) {
- if (name == null || name.trim().isEmpty()) { // first change
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- public String getName() {
- return this.name;
- }
- private void setPrice(double price) {
- if (price < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.price = price;
- }
- public double getPrice() {
- return this.price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment