Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.task.task19.task1905;
- import java.util.HashMap;
- import java.util.Map;
- /*
- Адаптировать Customer и Contact к RowItem.
- Классом-адаптером является DataAdapter.
- Инициализируйте countries перед началом выполнения программы. Соответствие кода страны и названия:
- UA Ukraine
- RU Russia
- CA Canada
- Требования:
- 1. Класс Solution должен содержать public static поле countries типа Map.
- 2. В статическом блоке класса Solution инициализируй поле countries тестовыми данными согласно заданию.
- 3. Класс Solution должен содержать интерфейс RowItem.
- 4. Класс Solution должен содержать интерфейс Contact.
- 5. Класс Solution должен содержать интерфейс Customer.
- 6. Класс DataAdapter должен реализовывать интерфейс RowItem.
- 7. Класс DataAdapter должен содержать два приватных поля: customer типа Customer и contact Contact.
- 8. Класс DataAdapter должен содержать конструктор с параметрами (Customer customer, Contact contact), который инициализирует поля contact и customer.
- 9. В классе DataAdapter реализуй методы интерфейса RowItem используя подсказки в виде комментариев в интерфейсах.
- */
- 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 DataAdapter implements RowItem {
- private Customer customer;
- private Contact contact;
- public DataAdapter(Customer customer, Contact contact) {
- this.contact=contact;
- this.customer = customer;
- }
- @Override
- public String getCountryCode() {
- String desiredKey = null;
- for(Map.Entry<String,String> pair : countries.entrySet())
- if(pair.getValue().equals(customer.getCountryName())) {
- desiredKey = pair.getKey(); break;
- }
- return desiredKey;
- }
- @Override
- public String getCompany() {
- return customer.getCompanyName();
- }
- //Метод getCompany() должен делегировать полномочие методу getCompanyName() объекта customer.
- @Override
- public String getContactFirstName() {
- String x[]= contact.getName().split(", ",2);
- return x[1];
- }
- @Override
- public String getContactLastName() {
- String x2[]= contact.getName().split(", ",2);
- return x2[0];
- }
- @Override
- public String getDialString() {
- String number = "callto://" + contact.getPhoneNumber().replaceAll("[()-]", "");
- return number;
- }
- }
- public static interface RowItem {
- String getCountryCode(); //example UA
- String getCompany(); //example JavaRush Ltd.
- String getContactFirstName(); //example Ivan
- String getContactLastName(); //example Ivanov
- String getDialString(); //example callto://+380501234567
- }
- 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 38050123567 or 380501234567 or +380(50)123-4567 or ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement