Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ShoppingSpree;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Person> people = new ArrayList<>();
- List<Product> products = new ArrayList<>();
- String[] tokens = scanner.nextLine().split(";");
- try {
- getPerson(people, tokens);
- tokens = scanner.nextLine().split(";");
- getProducts(products, tokens);
- }catch (IllegalArgumentException exception){
- System.out.println(exception.getMessage());
- }
- tokens = scanner.nextLine().split("\\s+");
- buyProducts(scanner, people, products, tokens);
- for (Person person : people) {
- System.out.println(person.toString());
- }
- }
- private static void buyProducts(Scanner scanner, List<Person> people, List<Product> products, String[] tokens) {
- while (!tokens[0].equals("END")) {
- String name = tokens[0];
- String productName = tokens[1];
- try {
- for (Person currentPerson : people) {
- if (currentPerson.getName().equals(name)) {
- for (Product currProduct : products) {
- if (currProduct.getName().equals(productName)) {
- currentPerson.buyProduct(currProduct);
- System.out.println(currentPerson.getName() + " bought " + currProduct.getName());
- }
- }
- }
- }
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- tokens = scanner.nextLine().split("\\s+");
- }
- }
- private static void getProducts(List<Product> products, String[] tokens) {
- for (String token : tokens) {
- String[] element = token.split("=");
- Product product = new Product(element[0], Double.parseDouble(element[1]));
- products.add(product);
- }
- }
- private static void getPerson(List<Person> people, String[] tokens) {
- for (String token : tokens) {
- String[] element = token.split("=");
- Person person = new Person(element[0], Double.parseDouble(element[1]));
- people.add(person);
- }
- }
- private static void buyProduct(Map<String, Person> people, Map<String, Product> products, String person, String product) {
- try {
- people.get(person).buyProduct(products.get(product));
- System.out.println(person + " bought " + product);
- } catch (IllegalArgumentException ex) {
- System.out.println(ex.getMessage());
- }
- }
- }
- package ShoppingSpree;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- 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<>();
- }
- private void setName(String name) {
- Validator.nameIsValid(name);
- this.name = name;
- }
- private void setMoney(double money) {
- Validator.moneyAreValid(money);
- this.money = money;
- }
- public String getName() {
- return this.name;
- }
- public void buyProduct(Product product) {
- if (product.getCost() > this.money) {
- throw new IllegalArgumentException(name
- + " can't afford " + product.getName());
- }
- this.products.add(product);
- this.money -= product.getCost();
- }
- @Override
- public String toString() {
- String output = products.isEmpty() ? "can't afford Kafence"
- : this.products.stream()
- .map(Product::getName)
- .collect(Collectors.joining(", "));
- return this.name + " - " + output;
- }
- }
- package ShoppingSpree;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Product {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- this.setName(name);
- this.setCost(cost);
- }
- private void setName(String name) {
- Validator.nameIsValid(name);
- this.name = name;
- }
- private void setCost(double cost) {
- Validator.moneyAreValid(cost);
- this.cost = cost;
- }
- public String getName() {
- return name;
- }
- public double getCost() {
- return cost;
- }
- }
- package ShoppingSpree;
- public class Validator {
- public static boolean moneyAreValid(double amount){
- if (amount < 0){
- throw new IllegalArgumentException("Money cannot be negative");
- }
- return true;
- }
- public static boolean nameIsValid(String name){
- if (name.trim().isEmpty() || name.trim()==null){
- throw new IllegalArgumentException
- ("Name cannot be empty");
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement