CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Ivan
Level 22
Nope
  • 16.03.2020
  • 775views
  • 7comments

Where is the issue with my lines 53-57?

Question about the task Adapting multiple interfaces
Java Core,  Level 9,  Lesson 3
Resolved

Adapt IncomeData to the Customer and Contact interfaces.
The adapter class is IncomeDataAdapter.
Initialize countries before running the program. Mapping between country codes and country names:
UA Ukraine
US United States
FR France
If necessary, pad phone numbers with zeros to get 10 digits long (see the examples).
Pay attention to the format of people's first and last names.

Requirements:
  • The Solution class must have a public static Map<String, String> field called countries.
  • Initialize the countries field in a static block in the Solution class using the test data provided in the task.
  • The IncomeDataAdapter class must implement the Customer and Contact interfaces.
  • The IncomeDataAdapter class must have a private IncomeData field called data.
  • The IncomeDataAdapter class must have a constructor with an IncomeData parameter.
  • Using the hints in the interfaces' comments, implement the methods of the Customer and Contact interfaces in the IncomeDataAdapter class.
package com.codegym.task.task19.task1903; /* Adapting multiple interfaces */ import java.util.HashMap; import java.util.Map; public class Solution { public static Map<String, String> countries = new HashMap<>(); static { countries.put("UA", "Ukraine"); countries.put("US", "United States"); countries.put("FR", "France"); } public static void main(String[] args) { } public static class IncomeDataAdapter implements Customer, Contact { private IncomeData data; 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() { //taking the phonenumber (which method is already returning integer) //and format it to padd zeroes on the left of the provided integer //example: provided integer is 123456 and you need it to be 10 //using String.format("%010d") adding zeroes until it gets 10 numbers //result is: 0000123456 String phoneNumber = String.format("%010d", data.getPhoneNumber()); //returning the countryphonecode + formatting the result from above - 0000123456 //result would be +1(000)012-34-56 return '+' + data.getCountryPhoneCode() + phoneNumber.replaceFirst("(.{3})(.{3})(.{2})(.{2})","($1)$2-$3-$4"); } } public static interface IncomeData { String getCountryCode(); // For example: US String getCompany(); // For example: CodeGym Ltd. String getContactFirstName(); // For example: John String getContactLastName(); // For example: Smith int getCountryPhoneCode(); // For example: 1 int getPhoneNumber(); // For example: 991234567 } public static interface Customer { String getCompanyName(); // For example: CodeGym Ltd. String getCountryName(); // For example: United States } public static interface Contact { String getName(); // For example: Smith, John String getPhoneNumber(); // For example: +1(099)123-45-67 } }
0
Comments (7)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
16 March 2020, 13:37
Line 53, inside the String.format method, ""%010S" is an illegal argument. You will need to change that to ""%10S"" for it to compile. There is still an issue with the code, try testing it out in main and seeing if you can spot the output error:
public static void main(String[] args) {
   String phoneNumber = String.format("%10S", "123456");

   // countryphonecode + formatting the result from above - 0000123456
   // result would be +1(000)012-34-56
   System.out.println(('+' + "1" // <-- "1" being used as country code
      + phoneNumber.replaceFirst("(.{3})(.{3})(.{2})(.{2})", "($1)$2-$3-$4")));
}
0
Ivan
Level 22 , Nope, Bulgaria
16 March 2020, 14:57
With %10S does not work in testing. I see that you made 123456 a string in your test With "%010d", number it works - like this: String phoneNumber1 = String.format("%010d", 123456); - Output - String phoneNumber1 = String.format("%010d", 123456); The methods data.getPhoneNumber() is returning a direct integer as I see it. Shouldn't it be alright?
0
Guadalupe Gagnon
Level 37 , Tampa, United States
16 March 2020, 15:03
you're right. I didn't see that the getPhoneNumber() was returning an int and just assumed it was a string.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
16 March 2020, 15:20solution
ok. after testing your code a little bit it looks like using '+' is the problem. Because the primitive char is also a number, it is actually adding its value of 43 to whatever is returned by getCountryCode(). If you output that line with any phone number:
System.out.println(('+' + 1 // <-- "1" being used as country code
      + phoneNumber.replaceFirst("(.{3})(.{3})(.{2})(.{2})", "($1)$2-$3-$4")));
you will see that it output 44(###)###-##-## Try changing that to "+" instead of '+'
+2
Ivan
Level 22 , Nope, Bulgaria
16 March 2020, 15:25
As usual, you are dead right! Thank you very much!
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
16 March 2020, 15:26
dead right the second time. Thanks for sharing this problem, I actually learned something from it.
0
Ivan
Level 22 , Nope, Bulgaria
16 March 2020, 15:57
Doesn't matter, sharp eye as always. And I learn a lot from you, if i can't understand/solve something, I go to help section and search your comments :D
+4
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.