Advertisement
jaVer404

level19.lesson03.task05(done)

Feb 17th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. package com.javarush.test.level19.lesson03.task05;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /* Закрепляем адаптер
  7. Адаптировать Customer и Contact к RowItem.
  8. Классом-адаптером является DataAdapter.
  9. Инициализируйте countries перед началом выполнения программы.
  10.  
  11. Соответствие кода страны и названия:
  12. UA Ukraine
  13. RU Russia
  14. CA Canada
  15. */
  16.  
  17. public class Solution {
  18.     private static Map<String,String> countries = new HashMap<String,String>();
  19.     static
  20.     {
  21.         countries.put("UA", "Ukraine");
  22.         countries.put("RU", "Russia");
  23.         countries.put("CA", "Canada");
  24.     }
  25.  
  26.     public static class DataAdapter implements RowItem{
  27.         private Customer customer;
  28.         private Contact contact;
  29.  
  30.         public DataAdapter(Customer customer, Contact contact)
  31.         {
  32.             this.customer = customer;
  33.             this.contact = contact;
  34.         }
  35.  
  36.         @Override
  37.         public String getCountryCode()
  38.         {
  39.             String counryName = customer.getCountryName();
  40.             String countryCode=null;
  41.             for (Map.Entry<String,String> entry : countries.entrySet())
  42.             {
  43.                 if(counryName.equals(entry.getValue())){
  44.                     countryCode = entry.getKey();
  45.                     break;
  46.                 }
  47.             }
  48.             return countryCode;
  49.         }
  50.  
  51.         @Override
  52.         public String getCompany()
  53.         {
  54.             return customer.getCompanyName();
  55.         }
  56.  
  57.         @Override
  58.         public String getContactFirstName()
  59.         {
  60.             String string = contact.getName();
  61.             String[] parts = string.split(", ");
  62.             return parts[1];
  63.         }
  64.  
  65.         @Override
  66.         public String getContactLastName()
  67.         {
  68.             String string = contact.getName();
  69.             String[] parts = string.split(", ");
  70.             return parts[0];
  71.         }
  72.  
  73.         @Override
  74.         public String getDialString()
  75.         {
  76.             String phone = (contact.getPhoneNumber()).replaceAll("[-()]","");
  77.             return String.format("callto://%s",phone);
  78.         }
  79.     }
  80.  
  81.     public static interface RowItem {
  82.         String getCountryCode();        //example UA
  83.         String getCompany();            //example JavaRush Ltd.
  84.         String getContactFirstName();   //example Ivan
  85.         String getContactLastName();    //example Ivanov
  86.         String getDialString();         //example callto://+380501234567
  87.     }
  88.  
  89.     public static interface Customer {
  90.         String getCompanyName();        //example JavaRush Ltd.
  91.         String getCountryName();        //example Ukraine
  92.     }
  93.  
  94.     public static interface Contact {
  95.         String getName();               //example Ivanov, Ivan
  96.         String getPhoneNumber();        //example +38(050)123-45-67
  97.     }
  98.  
  99.    public static void main(String[] args)
  100.     {
  101. /*        String phone = "+38(050)123-45-67";
  102.         phone = phone.replaceAll("[-()]","");
  103.         System.out.println(phone.format("callto://%s",phone));*/
  104. /*        String string = "Ivanov, Ivan";
  105.         String[] parts = string.split(", ");
  106.         System.out.println("Last name:"+parts[0]);
  107.         System.out.println("First name:"+parts[1]);*/
  108.  
  109. /*        String counryName = "Ukraine";
  110.         String countryCode=null;
  111.         for (Map.Entry<String,String> entry : countries.entrySet())
  112.         {
  113.                 if(counryName.equals(entry.getValue())){
  114.                     countryCode = entry.getKey();
  115.                     break;
  116.         }
  117.         }*/
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement