Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Test.Person;
- import Test.Product;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- var persons = new LinkedHashMap<String, Person>();
- var products = new LinkedHashMap<String, Product>();
- String[] peopleInfo = reader.readLine().split(";");
- String[] productInfo = reader.readLine().split(";");
- for (String p : peopleInfo) {
- try {
- Person person = new Person(p.split("=")[0],
- Double.parseDouble(p.split("=")[1]));
- persons.putIfAbsent(person.getName(), person);
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- return;
- }
- }
- for (String p : productInfo) {
- try {
- Product product = new Product(p.split("=")[0],
- Double.parseDouble(p.split("=")[1]));
- products.putIfAbsent(product.getName(), product);
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- return;
- }
- }
- String[] inputArgs = reader.readLine().split(" ");
- while (!"end".equalsIgnoreCase(inputArgs[0])) {
- String person = inputArgs[0];
- String product = inputArgs[1];
- try {
- if (persons.containsKey(person)) {
- persons.get(person).buyProduct(products.get(product));
- }
- } catch (IllegalStateException ise) {
- System.out.println(ise.getMessage());
- }
- inputArgs = reader.readLine().split(" ");
- }
- for (Person person : persons.values()) {
- StringBuilder sb = new StringBuilder();
- sb.append(person.getName()).append(" - ");
- if (person.getProducts().isEmpty()) {
- sb.append("Nothing bought");
- } else {
- String prefix = "";
- for (Product product : person.getProducts()) {
- sb.append(prefix);
- prefix = ", ";
- sb.append(product.getName());
- }
- }
- System.out.println(sb.toString());
- }
- }
- }
- // Person Class
- package Test;
- import java.util.ArrayList;
- import java.util.List;
- public class Person {
- private String name;
- private double money;
- private List<Product> products;
- public Person(String name, double money) {
- this.setName(name);
- this.setMoney(money);
- this.products = new ArrayList<>();
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- //IAM: VALIDATION
- if (!(name == null || name.trim().isEmpty())) {
- this.name = name;
- } else {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- }
- public double getMoney() {
- return money;
- }
- public void setMoney(double money) {
- //IAM: VALIDATION
- if (money >= 0) {
- this.money = money;
- } else {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- }
- public List<Product> getProducts() {
- return products;
- }
- public void setProducts(List<Product> products) {
- this.products = products;
- }
- public void buyProduct(Product product) {
- if (this.money >= product.getCost()) {
- products.add(product);
- System.out.println(this.getName() + " bought " + product.getName());
- this.money -= product.getCost();
- } else {
- String MESSAGE = this.getName() + " can't afford " + product.getName();
- throw new IllegalStateException(MESSAGE);
- }
- }
- }
- // Product class
- package Test;
- // 3 intervala ne sa 0 len
- // Money text is not cost
- // Linked Hash map - ordera
- // return sled catch
- public class Product {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- this.setName(name);
- this.setCost(cost);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- //IAM: VALIDATION
- if (!(name == null || name.trim().isEmpty())) {
- this.name = name;
- } else {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- }
- public double getCost() {
- return cost;
- }
- public void setCost(double money) {
- //IAM: VALIDATION
- if (money >= 0) {
- this.cost = money;
- } else {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement