Advertisement
Guest User

Untitled

a guest
Oct 7th, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package DefiningClasses.Exercises.Google;
  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 scan = new Scanner(System.in);
  10.  
  11.         Map<String, Person> map = new LinkedHashMap<>();
  12.         String input = scan.nextLine();
  13.         while (!input.equals("End")) {
  14.             String[] tokens = input.split("\\s+");
  15.             String personName = tokens[0];
  16.  
  17.             if (!map.containsKey(personName)){ //here
  18.                 Person newPerson = new Person();
  19.                 map.put(personName, newPerson);
  20.             }
  21.  
  22.                 String key = tokens[1];
  23.                 switch (key) {
  24.                     case "company":
  25.                         String companyName = tokens[2];
  26.                         String department = tokens[3];
  27.                         String salary = tokens[4];
  28.                         Company newCompany = new Company(companyName, department, salary);
  29.                         map.get(personName).setCompany(newCompany);
  30.                         break;
  31.                     case "pokemon":
  32.                         String pokemonName = tokens[2];
  33.                         String pokemonType = tokens[3];
  34.                         Pokemon newPokemon = new Pokemon(pokemonName, pokemonType);
  35.                         map.get(personName).setPokemon(newPokemon);
  36.                         break;
  37.                     case "parents":
  38.                         String parentName = tokens[2];
  39.                         String parentBirthday = tokens[3];
  40.                         Parents newParents = new Parents(parentName, parentBirthday);
  41.                         map.get(personName).setParents(newParents);
  42.                         break;
  43.                     case "children":
  44.                         String childName = tokens[2];
  45.                         String childBirthday = tokens[3];
  46.                         Children newChildren = new Children(childName, childBirthday);
  47.                         map.get(personName).setChildren(newChildren);
  48.                         break;
  49.                     case "car":
  50.                         String carModel = tokens[2];
  51.                         String carSpeed = tokens[3];
  52.                         Car newCar = new Car(carModel, carSpeed);
  53.                         map.get(personName).setCar(newCar);
  54.                         break;
  55.                 }
  56.             input = scan.nextLine();
  57.         }
  58.  
  59.         String printName = scan.nextLine();
  60.         System.out.println(printName);
  61.         System.out.println(map.get(printName).toString());
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement