Advertisement
Guest User

адаптером

a guest
May 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. package com.javarush.task.task19.task1905;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /*
  7. Адаптировать Customer и Contact к RowItem.
  8. Классом-адаптером является DataAdapter.
  9.  
  10. Инициализируйте countries перед началом выполнения программы. Соответствие кода страны и названия:
  11. UA Ukraine
  12. RU Russia
  13. CA Canada
  14.  
  15.  
  16. Требования:
  17. 1. Класс Solution должен содержать public static поле countries типа Map.
  18. 2. В статическом блоке класса Solution инициализируй поле countries тестовыми данными согласно заданию.
  19. 3. Класс Solution должен содержать интерфейс RowItem.
  20. 4. Класс Solution должен содержать интерфейс Contact.
  21. 5. Класс Solution должен содержать интерфейс Customer.
  22. 6. Класс DataAdapter должен реализовывать интерфейс RowItem.
  23. 7. Класс DataAdapter должен содержать два приватных поля: customer типа Customer и contact Contact.
  24. 8. Класс DataAdapter должен содержать конструктор с параметрами (Customer customer, Contact contact), который инициализирует поля contact и customer.
  25. 9. В классе DataAdapter реализуй методы интерфейса RowItem используя подсказки в виде комментариев в интерфейсах.
  26. */
  27.  
  28. public class Solution {
  29.     public static Map<String,String> countries = new HashMap<String,String>();
  30.         static {
  31.             countries.put("UA", "Ukraine");
  32.             countries.put("RU", "Russia");
  33.             countries.put("CA", "Canada");
  34.  
  35.         }
  36.     public static void main(String[] args) {
  37.  
  38.     }
  39.  
  40.     public static class DataAdapter implements RowItem {
  41.             private Customer customer;
  42.             private  Contact contact;
  43.         public DataAdapter(Customer customer, Contact contact) {
  44.             this.contact=contact;
  45.             this.customer = customer;
  46.         }
  47.  
  48.         @Override
  49.         public String getCountryCode() {
  50.             String desiredKey = null;
  51.             for(Map.Entry<String,String> pair : countries.entrySet())
  52.                 if(pair.getValue().equals(customer.getCountryName())) {
  53.                     desiredKey = pair.getKey();  break;
  54.                 }
  55.             return desiredKey;
  56.         }
  57.  
  58.         @Override
  59.         public String getCompany() {
  60.  
  61.             return customer.getCompanyName();
  62.         }
  63.         //Метод getCompany() должен делегировать полномочие методу getCompanyName() объекта customer.
  64.         @Override
  65.         public String getContactFirstName() {
  66.             String x[]= contact.getName().split(", ",2);
  67.             return x[1];
  68.         }
  69.  
  70.         @Override
  71.         public String getContactLastName() {
  72.            String x2[]= contact.getName().split(", ",2);
  73.             return x2[0];
  74.         }
  75.  
  76.         @Override
  77.         public String getDialString() {
  78.             String number = "callto://" + contact.getPhoneNumber().replaceAll("[()-]", "");
  79.             return number;
  80.         }
  81.     }
  82.  
  83.     public static interface RowItem {
  84.         String getCountryCode();        //example UA
  85.         String getCompany();            //example JavaRush Ltd.
  86.         String getContactFirstName();   //example Ivan
  87.         String getContactLastName();    //example Ivanov
  88.         String getDialString();         //example callto://+380501234567
  89.     }
  90.  
  91.     public static interface Customer {
  92.         String getCompanyName();        //example JavaRush Ltd.
  93.         String getCountryName();        //example Ukraine
  94.     }
  95.  
  96.     public static interface Contact {
  97.         String getName();               //example Ivanov, Ivan
  98.         String getPhoneNumber();        //example 38050123567 or 380501234567 or +380(50)123-4567 or ...
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement