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.Scanner;
- import java.util.concurrent.atomic.AtomicInteger;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Person> personList = new ArrayList<>();
- List<Product> productList = new ArrayList<>();
- String[] inputArray = scanner.nextLine().split(";");
- for (int i = 0; i < inputArray.length; i++) {
- try {
- String[] temp = inputArray[i].split("=");
- Person person = new Person(temp[0], Double.parseDouble(temp[1]));
- personList.add(person);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- System.exit(0);
- }
- }
- inputArray = scanner.nextLine().split(";");
- for (int i = 0; i < inputArray.length; i++) {
- try {
- String[] temp = inputArray[i].split("=");
- Product product = new Product(temp[0], Double.parseDouble(temp[1]));
- productList.add(product);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- System.exit(0);
- }
- }
- String input = scanner.nextLine();
- while(!input.equals("END")) {
- String[] inputArray2 = input.split(" ");
- for (int i = 0; i < personList.size(); i++) {
- if (personList.get(i).getName().equals(inputArray2[0])) {
- for (int j = 0; j < productList.size(); j++) {
- if (productList.get(j).getName().equals(inputArray2[1])) {
- Person temp = personList.get(i);
- temp.buyProduct(productList.get(j));
- personList.set(i, temp);
- }
- }
- }
- }
- input = scanner.nextLine();
- }
- personList.stream().forEach(e -> {
- System.out.print(e.getName() + " - ");
- if (e.getProducts().size() == 0) {
- System.out.print("Nothing bought");
- } else {
- AtomicInteger counter = new AtomicInteger();
- e.getProducts().stream().forEach(t -> {
- System.out.print(t.getName());
- if (counter.get() < e.getProducts().size() - 1) {
- System.out.print(", ");
- }
- counter.getAndIncrement();
- });
- }
- System.out.println("");
- });
- }
- }
- //=======================================================================================
- package ShoppingSpree;
- 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<>();
- }
- private void setName(String name) {
- if (name == null || name.length() < 1 || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty");
- } else {
- this.name = name;
- }
- }
- private void setMoney(double money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- } else {
- this.money = money;
- }
- }
- public void buyProduct (Product product) {
- if (this.money >= product.getCost()) {
- this.products.add(product);
- this.money = this.money - product.getCost();
- System.out.printf("%s bought %s\n", this.name, product.getName());
- } else {
- System.out.printf("%s can't afford %s", this.name, product.getName());
- }
- }
- public String getName() {
- return name;
- }
- public List<Product> getProducts() {
- return products;
- }
- }
- //=============================================================================================
- package ShoppingSpree;
- public class Product {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- this.name = name;
- this.cost = cost;
- }
- private void setName(String name) {
- if (name == null || name.length() < 1 || name.trim().isEmpty()) {
- throw new IllegalArgumentException("Name cannot be empty");
- } else {
- this.name = name;
- }
- }
- private void setCost(double cost) {
- if (cost < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- } else {
- this.cost = cost;
- }
- }
- public String getName() {
- return name;
- }
- public double getCost() {
- return cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment