Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p04ShoppingSpree;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.LinkedHashSet;
- public class ShoppingSpree {
- public static void main(String[] args) throws IOException {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
- StringBuilder sb = new StringBuilder();
- LinkedHashSet<Person> personsSet = new LinkedHashSet<>();
- ArrayList<Product> productsSet = new ArrayList<>();
- String line1 = reader.readLine();
- String[] people;
- String line2 = reader.readLine();
- String[] products;
- if (line1.contains(";")) {
- people = line1.split(";");
- } else {
- people = line1.split("=");
- }
- if (line2.contains(";")) {
- products = line2.split(";");
- } else {
- products = line2.split("=");
- }
- for (int i = 0; i < people.length; i++) {
- String name;
- int money;
- if (people[i].contains("=")) {
- String[] peopleSplit = people[i].split("=");
- name = peopleSplit[0];
- money = Integer.parseInt(peopleSplit[1]);
- personsSet.add(new Person(name, money));
- } else {
- name = people[i];
- money = Integer.parseInt(people[i + 1]);
- personsSet.add(new Person(name, money));
- break;
- }
- }
- for (int i = 0; i < products.length; i++) {
- String product;
- int price;
- if (products[i].contains("=")) {
- String[] productSplit = products[i].split("=");
- product = productSplit[0];
- price = Integer.parseInt(productSplit[1]);
- productsSet.add(new Product(product, price));
- } else {
- product = products[i];
- price = Integer.parseInt(products[i + 1]);
- productsSet.add(new Product(product, price));
- break;
- }
- }
- String command = reader.readLine();
- while (!command.equals("END")) {
- String[] buyProducts = command.split(" ");
- String name = buyProducts[0];
- String product = buyProducts[1];
- for (Person person : personsSet) {
- if (person.getName().equals(name)) {
- for (Product product1 : productsSet) {
- if (product1.getProduct().equals(product)) {
- person.addToBag(product1.getProduct(), product1.getPrice());
- }
- }
- }
- }
- command = reader.readLine();
- }
- for (Person person : personsSet) {
- person.getPurchases();
- }
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- }
- }
- }
- class Person {
- private String name;
- private int money;
- private ArrayList<String> bag;
- public Person(String name, int money) {
- this.setName(name);
- this.setMoney(money);
- this.bag = new ArrayList<>();
- }
- public String getName() {
- return this.name;
- }
- public int getMoney() {
- return this.money;
- }
- private int bagSize() {
- return this.bag.size();
- }
- public void addToBag(String product, int money) {
- if (this.money >= money) {
- System.out.printf("%s bought %s\n", this.name, product);
- this.payProduct(money);
- this.bag.add(product);
- } else {
- System.out.printf("%s can't afford %s\n", this.name, product);
- }
- }
- private void payProduct(int money) {
- this.money -= money;
- }
- public void getPurchases() {
- if (bagSize() == 0) {
- System.out.printf("%s - Nothing bought\n", this.name);
- } else {
- StringBuilder sb = new StringBuilder();
- sb.append(String.format("%s - ", this.name));
- for (String product : this.bag) {
- sb.append(String.format("%s, ", product));
- }
- sb.deleteCharAt(sb.length() - 2);
- System.out.println(sb.toString());
- }
- }
- private void setName(String name) {
- if (name.length() == 0) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.name = name;
- }
- private void setMoney(int money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.money = money;
- }
- }
- class Product {
- private String product;
- private int price;
- public Product(String product, int price) {
- this.setProduct(product);
- this.setPrice(price);
- }
- public String getProduct() {
- return this.product;
- }
- public int getPrice() {
- return this.price;
- }
- private void setProduct(String product) {
- if (product.isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- this.product = product;
- }
- private void setPrice(int price) {
- if (price < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.price = price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement