Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pr04ShoppingSpree;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Pr04ShoppingSpree {
- public static void main(String[] args) throws IOException {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));) {
- Map<String, Product> priceList = new HashMap<>();
- Map<String, Person> personList = new LinkedHashMap<>();
- String[] firstLine = reader.readLine().trim().split(";");
- for (String tokens : firstLine) {
- String[] people = tokens.split("=");
- String name = people[0];
- double money = Double.valueOf(people[1]);
- Person person = new Person(name, money);
- personList.put(name, person);
- }
- String[] secondLine = reader.readLine().trim().split(";");
- for (String tokens : secondLine) {
- String[] products = tokens.split("=");
- String name = products[0];
- double cost = Double.valueOf(products[1]);
- Product product = new Product(name, cost);
- priceList.put(name, product);
- }
- String input = reader.readLine();
- while (!input.equals("END")) {
- String[] params = input.split("\\s+");
- String personName = params[0];
- String productName = params[1];
- personList.get(personName).broughtProduct(
- priceList.get(productName));
- input = reader.readLine();
- }
- for (String person : personList.keySet()) {
- System.out.println(personList.get(person));
- }
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- class Person {
- private String name;
- private double money;
- private List<Product> shoppingBag;
- public Person(String name, double money) {
- setName(name);
- setMoney(money);
- this.shoppingBag = new ArrayList<>();
- }
- public String getName() {
- return name;
- }
- public String setName(String name){
- if (name.isEmpty() || name.contentEquals(" ")) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- return this.name = name;
- }
- public double getMoney() {
- return money;
- }
- public double setMoney(double money){
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- return this.money = money;
- }
- public List<Product> getShoppingBag() {
- return shoppingBag;
- }
- public void broughtProduct(Product product){
- if (product.getCoast() > this.money) {
- System.out.printf("%s can't afford %s%n",this.name,product.getName());
- }else{
- shoppingBag.add(product);
- this.money -= product.getCoast();
- System.out.printf("%s bought %s%n",this.name,product.getName());
- }
- }
- @Override
- public String toString() {
- if (shoppingBag.size() > 0) {
- StringBuilder sb = new StringBuilder(shoppingBag.toString());
- return String.format("%s - %s", this.name,sb.toString().substring(1, sb.length()-1));
- }else{
- return String.format("%s - Nothing bought",this.name);
- }
- }
- }
- class Product {
- private String name;
- private double cost;
- public Product(String name, double cost) {
- setName(name);
- setCost(cost);
- }
- public String getName() {
- return name;
- }
- public String setName(String name){
- if (name.isEmpty() || name.contentEquals(" ")) {
- throw new IllegalArgumentException("Name cannot be empty");
- }
- return this.name = name;
- }
- public double getCoast() {
- return cost;
- }
- public double setCost(double cost){
- if (cost < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- return this.cost = cost;
- }
- @Override
- public String toString() {
- return this.name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment