public static class IncomeDataAdapter implements Customer, Contact{
private IncomeData data;
public IncomeDataAdapter(IncomeData data){
this.data = data;
}
public String getCompanyName(){
return data.getCompany();
}
public String getCountryName(){
String countryName = null;
for (Map.Entry<String, String> pair : countries.entrySet()){
if (pair.getKey() == data.getCountryCode()){
countryName = pair.getValue();
}
}
return countryName;
}
public String getName(){
return data.getContactLastName() +
", " +
data.getContactFirstName();
}
public String getPhoneNumber(){
String number = data.getPhoneNumber() + "";
for (int i = 0; i < 10-number.length(); i++){
number = "0" + number;
}
return "+" + data.getCountryPhoneCode() +
"(" +
number.substring(0, 3) +
")" +
number.substring(3, 6) +
"-" +
number.substring(6, 8) +
"-" +
number.substring(8, 10);
}
}
错误提示:
如果有必要,使用零填充电话号码,使其长度达到 10 位数(请参见示例)。
请注意人员的名字和姓氏的格式。package zh.codegym.task.task19.task1903;
/*
适配多个接口
*/
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static Map<String, String> countries = new HashMap<>();
static {
countries.put("UA", "乌克兰");
countries.put("US", "美国");
countries.put("FR", "法国");
}
public static void main(String[] args) {
}
public static class IncomeDataAdapter implements Customer, Contact{
private IncomeData data;
public IncomeDataAdapter(IncomeData data){
this.data = data;
}
public String getCompanyName(){
return data.getCompany();
}
public String getCountryName(){
String countryName = null;
for (Map.Entry<String, String> pair : countries.entrySet()){
if (pair.getKey() == data.getCountryCode()){
countryName = pair.getValue();
}
}
return countryName;
}
public String getName(){
return data.getContactLastName() +
", " +
data.getContactFirstName();
}
public String getPhoneNumber(){
String number = data.getPhoneNumber() + "";
for (int i = 0; i < 10-number.length(); i++){
number = "0" + number;
}
return "+" + data.getCountryPhoneCode() +
"(" +
number.substring(0, 3) +
")" +
number.substring(3, 6) +
"-" +
number.substring(6, 8) +
"-" +
number.substring(8, 10);
}
}
public static interface IncomeData {
String getCountryCode(); // 例如:US
String getCompany(); // 例如:CodeGym Ltd.
String getContactFirstName(); // 例如:约翰
String getContactLastName(); // 例如:史密斯
int getCountryPhoneCode(); // 例如:1
int getPhoneNumber(); // 例如:991234567
}
public static interface Customer {
String getCompanyName(); // 例如:CodeGym Ltd.
String getCountryName(); // 例如:美国
}
public static interface Contact {
String getName(); // 例如:史密斯, 约翰
String getPhoneNumber(); // 例如:+1(099)123-45-67
}
}