Advertisement
elvirynwa

Main

Jul 10th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package shoppingSpree;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String input = "";
  12.         Map<String, Person> people = new LinkedHashMap<>();
  13.         Map<String, Product> products = new LinkedHashMap<>();
  14.         int count = 0;
  15.  
  16.         try {
  17.             while (!"END".equals(input = scanner.nextLine())) {
  18.                 count++;
  19.                 String name;
  20.                 double money;
  21.                 Person person = null;
  22.                 Product product = null;
  23.                 if (input.contains(";")) {
  24.                     String[] data = input.split(";");
  25.                     for (int i = 0; i < data.length; i++) {
  26.                         String[] tokens = data[i].split("=");
  27.                         name = tokens[0];
  28.                         money = Integer.parseInt(tokens[1]);
  29.                         if (count == 1) {
  30.                             person = new Person(name, money);
  31.                             people.put(name, person);
  32.                         } else {
  33.                             product = new Product(name, money);
  34.                             products.put(name, product);
  35.                         }
  36.                     }
  37.                 } else if (input.contains("=")) {
  38.                     String[] data = input.split("=");
  39.                     name = data[0];
  40.                     money = Integer.parseInt(data[1]);
  41.                     if (count == 1) {
  42.                         person = new Person(name, money);
  43.                         people.put(name, person);
  44.                     } else {
  45.                         product = new Product(name, money);
  46.                         products.put(name, product);
  47.                     }
  48.  
  49.                 } else {
  50.                     String[] tokens = input.split("\\s+");
  51.                     String personName = tokens[0];
  52.                     String productName = tokens[1];
  53.  
  54.                     person = new Person(personName, people.get(personName).getMoney());
  55.                     product = new Product(productName, products.get(productName).getCost());
  56.                     person.buyProduct(product);
  57.  
  58.                 }
  59.             }
  60.         } catch (IllegalArgumentException ex) {
  61.             System.out.println(ex.getMessage());
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement