Advertisement
Adeptius

Untitled

Feb 11th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.javarush.test.level19.lesson03.task03;
  2.  
  3. /* Адаптация нескольких интерфейсов
  4. Адаптировать IncomeData к Customer и Contact.
  5. Классом-адаптером является IncomeDataAdapter.
  6. Инициализируйте countries перед началом выполнения программы. Соответствие кода страны и названия:
  7. UA Ukraine
  8. RU Russia
  9. CA Canada
  10. Дополнить телефонный номер нулями до 10 цифр при необходимости (смотри примеры)
  11. Обратите внимание на формат вывода фамилии и имени человека
  12. */
  13.  
  14. import java.util.HashMap;
  15. import java.util.Map;
  16.  
  17. public class Solution {
  18.    
  19.     public static Map<String, String> countries = new HashMap<String, String>();
  20.     static {
  21.         countries.put("UA", "Ukraine");
  22.         countries.put("RU", "Russia");
  23.         countries.put("CA", "Canada");
  24.     }
  25.  
  26.     public static class IncomeDataAdapter implements Customer, Contact {
  27.         private IncomeData data;
  28.         IncomeDataAdapter(IncomeData data){
  29.             this.data = data;
  30.         }
  31.  
  32.         @Override
  33.         public String getName() {
  34.             return this.data.getContactLastName() + ", " + this.data.getContactFirstName();
  35.         }
  36.  
  37.         @Override
  38.         public String getPhoneNumber() {
  39.             String a = String.valueOf(this.data.getPhoneNumber());
  40.             while (a.length() < 10) a += "0";
  41.  
  42.             String s = "+" + this.data.getCountryPhoneCode()
  43.                     + "(0"
  44.                     + a.substring(0,2)
  45.                     + ")"
  46.                     + a.substring(2,5)
  47.                     + "-"
  48.                     + a.substring(6,8)
  49.                     + "-"
  50.                     + a.substring(8,10);
  51.             return s;
  52.         }
  53.  
  54.         @Override
  55.         public String getCompanyName() {
  56.             return this.data.getCompany();
  57.         }
  58.  
  59.         @Override
  60.         public String getCountryName() {
  61.             return countries.get(this.data.getCountryCode());
  62.         }
  63.     }
  64.  
  65.     public static interface IncomeData {
  66.         String getCountryCode();        //example UA
  67.  
  68.         String getCompany();            //example JavaRush Ltd.
  69.  
  70.         String getContactFirstName();   //example Ivan
  71.  
  72.         String getContactLastName();    //example Ivanov
  73.  
  74.         int getCountryPhoneCode();      //example 38
  75.  
  76.         int getPhoneNumber();           //example 501234567
  77.     }
  78.  
  79.     public static interface Customer {
  80.         String getCompanyName();        //example JavaRush Ltd.
  81.  
  82.         String getCountryName();        //example Ukraine
  83.     }
  84.  
  85.     public static interface Contact {
  86.         String getName();               //example Ivanov, Ivan
  87.  
  88.         String getPhoneNumber();        //example +38(050)123-45-67
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement