Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
- LinkedHashMap<String, Person> people = new LinkedHashMap<>();
- LinkedHashMap<String, Product> products = new LinkedHashMap<>();
- try {
- String[] clients = console.readLine().split(";");
- for (int i = 0; i < clients.length; i++) {
- String[] nameMoney = clients[i].split("=");
- String name = nameMoney[0];
- double money = Double.parseDouble(nameMoney[1]);
- Person person = new Person(name, money);
- people.put(name, person);
- }
- String[] consumables = console.readLine().split(";");
- for (int i = 0; i < consumables.length; i++) {
- String[] productCost = consumables[i].split("=");
- String product = productCost[0];
- double cost = Double.parseDouble(productCost[1]);
- Product pro = new Product(product, cost);
- products.put(product, pro);
- }
- while (true) {
- String[] input = console.readLine().split("\\s+");
- if (input[0].equals("END")) {
- break;
- }
- String client = input[0];
- String product = input[1];
- Person person = people.get(client);
- Product pro = products.get(product);
- person.buyProduct(pro);
- }
- for (Person person : people.values()) {
- if (person.getProducts().isEmpty()){
- System.out.printf("%s - Nothing bought%n", person.getName());
- } else {
- System.out.printf("%s - %s%n",
- person.getName(),
- String.join(", ", person.getProducts()));
- }
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- class Person {
- private String name;
- private double money;
- private List<String> products;
- public Person(String name, double money) {
- this.setName(name);
- this.setMoney(money);
- this.products = new ArrayList<>();
- }
- public List<String> getProducts() {
- return products;
- }
- protected String getName() {
- return name;
- }
- private void setName(String name) {
- if (name.isEmpty() || name.contentEquals(" ")) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- private double getMoney() {
- return money;
- }
- private void setMoney(double money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.money = money;
- }
- public void buyProduct(Product product) {
- if (this.getMoney() >= product.getCost()) {
- this.products.add(product.getName());
- this.money -= product.getCost();
- boughtProduct(product);
- }else {
- noMoney(product);
- }
- }
- private void boughtProduct(Product product) {
- System.out.printf("%s bought %s%n", this.name, product.getName());
- }
- private void noMoney(Product product){
- System.out.printf("%s can't afford %s%n", this.getName(), product.getName());
- }
- }
- 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) {
- if (name.isEmpty() || name.contentEquals(" ")){
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- public double getCost() {
- return cost;
- }
- public void setCost(double cost) {
- if (cost <= 0){
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.cost = cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement