Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Person -----
- private void setMoney(double money) {
- if (money < 0) {
- throw new IllegalArgumentException("Money cannot be negative");
- }
- this.money = money;
- }
- public void buyProduct(Product product) {
- if (product == null) {
- throw new IllegalArgumentException();
- }
- if (this.money - product.getCost() < 0) {
- throw new IllegalArgumentException(String.format("%s can't afford %s", this.name, product.getName()));
- }
- this.products.add(product);
- this.setMoney(this.money - product.getCost());
- System.out.println(String.format("%s bought %s", this.name, product.getName()));
- }
- Main-----
- try {
- getPeople();
- getProducts();
- String input;
- while (!"end".equalsIgnoreCase(input = reader.readLine())) {
- String[] tokens = input.split("\\s+");
- String name = tokens[0];
- String productName = tokens[1];
- if (people.containsKey(name) && products.containsKey(productName)) {
- Person person = people.get(name);
- Product product = products.get(productName);
- person.buyProduct(product);
- }
- }
- } catch (IllegalArgumentException iae) {
- System.out.println(iae.getMessage());
- } catch (IOException e) {
- e.printStackTrace();
- }
- people.values()
- .forEach(System.out::println);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement