Created
June 12, 2018 14:00
-
-
Save Maxomax/ffe36bdc06c57c22fde32eba02b15921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task19.task1903; | |
/* | |
Адаптация нескольких интерфейсов | |
Адаптируй IncomeData к Customer и Contact. | |
Классом-адаптером является IncomeDataAdapter. | |
Инициализируйте countries перед началом выполнения программы. Соответствие кода страны и названия: | |
UA Ukraine | |
RU Russia | |
CA Canada | |
Дополнить телефонный номер нулями до 10 цифр при необходимости (смотри примеры). | |
Обратите внимание на формат вывода фамилии и имени человека. | |
*/ | |
import javax.swing.text.MaskFormatter; | |
import java.text.DecimalFormat; | |
import java.text.MessageFormat; | |
import java.text.ParseException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Solution { | |
public static Map<String, String> countries = new HashMap<String, String>(); | |
static { | |
countries.put("UA", "Ukraine"); | |
countries.put("RU", "Russia"); | |
countries.put("CA", "Canada"); | |
} | |
public static void main(String[] args) { | |
} | |
public static class IncomeDataAdapter implements Customer, Contact { | |
private IncomeData data; | |
public IncomeDataAdapter(IncomeData data) { | |
this.data = data; | |
} | |
@Override | |
public String getCompanyName() { | |
return data.getCompany(); | |
} | |
@Override | |
public String getCountryName() { | |
return countries.get(data.getCountryCode()); | |
} | |
@Override | |
public String getName() { | |
return data.getContactLastName() + ", " + data.getContactFirstName(); | |
} | |
@Override | |
public String getPhoneNumber() { | |
StringBuilder phone =new StringBuilder(); | |
String temp = String.valueOf(data.getPhoneNumber()); | |
if (temp.length()<10) | |
for (int i = temp.length(); i < 10; i++) { | |
phone.append("0"); | |
} | |
phone.append(temp); | |
try { | |
MaskFormatter format = new MaskFormatter("(###)###-##-##"); | |
format.setValueContainsLiteralCharacters(false); | |
temp = format.valueToString(phone); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return "+"+data.getCountryPhoneCode()+temp; | |
} | |
} | |
public static interface IncomeData { | |
String getCountryCode(); //example UA | |
String getCompany(); //example JavaRush Ltd. | |
String getContactFirstName(); //example Ivan | |
String getContactLastName(); //example Ivanov | |
int getCountryPhoneCode(); //example 38 | |
int getPhoneNumber(); //example 501234567 | |
} | |
public static interface Customer { | |
String getCompanyName(); //example JavaRush Ltd. | |
String getCountryName(); //example Ukraine | |
} | |
public static interface Contact { | |
String getName(); //example Ivanov, Ivan | |
String getPhoneNumber(); //example +38(050)123-45-67 | |
} | |
} | |
/*Требования: | |
1. Класс Solution должен содержать public static поле countries типа Map. | |
2. В статическом блоке класса Solution инициализируй поле countries тестовыми данными согласно заданию. | |
3. Класс IncomeDataAdapter должен реализовывать интерфейсы Customer и Contact. | |
4. Класс IncomeDataAdapter должен содержать приватное поле data типа IncomeData. | |
5. Класс IncomeDataAdapter должен содержать конструктор с параметром IncomeData. | |
6. В классе IncomeDataAdapter реализуй методы интерфейсов Customer и Contact используя подсказки в виде комментариев в интерфейсах.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment