Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main ----->
- package E3_ShoppingSpree;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.Map;
- import java.util.Set;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, Person> collectPeople = new LinkedHashMap<>();
- Map<String, Product> productsCollect = new LinkedHashMap<>();
- try {
- String[] people = reader.readLine().split(";");
- for (String person : people) {
- String[] tokens = person.split("=");
- Person person1 = new Person(tokens[0], Double.parseDouble(tokens[1]));
- collectPeople.putIfAbsent(tokens[0], person1);
- }
- String[] products = reader.readLine().split(";");
- for (String product : products) {
- String[] tokens = product.split("=");
- Product product1 = new Product(tokens[0], Double.parseDouble(tokens[1]));
- productsCollect.putIfAbsent(tokens[0], product1);
- }
- String line;
- while (!"end".equalsIgnoreCase(line = reader.readLine())) {
- String[] tokens = line.split("\\s+");
- String name = tokens[0];
- String productName = tokens[1];
- if (collectPeople.containsKey(name) && productsCollect.containsKey(productName)) {
- Person person = collectPeople.get(name);
- Product product = productsCollect.get(productName);
- person.buyProduct(product);
- }
- }
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- }
- collectPeople.values().forEach(System.out::println);
- }
- }
- Person ----------->
- package E3_ShoppingSpree;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Person implements ValidateData {
- 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 this.name;
- }
- public void setName(String name) {
- validateName(name);
- this.name = name;
- }
- public double getMoney() {
- return this.money;
- }
- public void setMoney(double money) {
- validateNumber(money);
- this.money = money;
- }
- public void buyProduct(Product product) {
- if (product == null) {
- throw new IllegalArgumentException();
- }
- if (this.getMoney() - product.getCost() < 0) {
- throw new IllegalArgumentException(String.format("%s can't afford %s", this.name, product.getName()));
- } else {
- this.products.add(product);
- this.setMoney(this.getMoney() - product.getCost());
- System.out.println(String.format("%s bought %s", this.name, product.getName()));
- }
- }
- @Override
- public String toString() {
- if(this.products.isEmpty()){
- return String.format("%s – Nothing bought",this.getName());
- }
- return this.getName() + " - " +
- this.products
- .stream()
- .map(Product::getName)
- .collect(Collectors.joining(", "));
- }
- @Override
- public void validateNumber(double num) {
- if (num < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- }
- @Override
- public void validateName(String name) {
- if (name == null || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty.");
- }
- }
- }
- Product ------------->
- package E3_ShoppingSpree;
- public class Product implements ValidateData {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- this.setName(name);
- this.setCost(cost);
- }
- public String getName() {
- return this.name;
- }
- private void setName(String name) {
- validateName(name);
- this.name = name;
- }
- public double getCost() {
- return this.cost;
- }
- private void setCost(double cost) {
- validateNumber(cost);
- this.cost = cost;
- }
- @Override
- public void validateNumber(double num) {
- if (num < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- }
- @Override
- public void validateName(String name) {
- if (name == null || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty.");
- }
- }
- }
- Validation ------>
- package E3_ShoppingSpree;
- public interface ValidateData {
- public void validateNumber (double num);
- public void validateName (String name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement