SHARE
TWEET

task 6java

Mary_99 Apr 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3.  class Task6
  4. {
  5.   public static void main(String[] args)
  6.   {
  7.  
  8.     List<TelephoneEntry> addressList = new ArrayList<TelephoneEntry>();
  9.     addressList.add(new Person("Bruno  ", "Mars", new Address("Los Angeles", "Gold Street 1", new TelephoneNumber(48, 857845356))));
  10.  
  11.     TreeMap<TelephoneNumber, TelephoneEntry> addressBook = new TreeMap<TelephoneNumber, TelephoneEntry>();
  12.  
  13.     Iterator<TelephoneEntry> addressListIterator = addressList.iterator();
  14.  
  15.     while (addressListIterator.hasNext())
  16.     {
  17.         TelephoneEntry input = addressListIterator.next();
  18.         addressBook.put(input.getAddress().getTeleNumber(), input);
  19.     }
  20.  
  21.     for (Map.Entry<TelephoneNumber, TelephoneEntry> input : addressBook.entrySet())
  22.     {
  23.         System.out.println("Description: " + input.getKey().countryCode + "-" + input.getKey().localNumber + "\t" + input.getValue().description());
  24.     }
  25.   }
  26. }
  27.  
  28. class TelephoneNumber implements Comparable<TelephoneNumber>
  29. {
  30.   int countryCode;
  31.   long localNumber;
  32.  
  33.   public TelephoneNumber(int countryCode, long localNumber)
  34.   {
  35.     this.countryCode = countryCode;
  36.     this.localNumber = localNumber;
  37.   }
  38.  
  39.   public int getCountryCode()
  40.   {
  41.     return countryCode;
  42.   }
  43.  
  44.   public long getLocalNumber()
  45.   {
  46.     return localNumber;
  47.   }
  48.  
  49.   public int compareTo(TelephoneNumber otherNumber) {
  50.     int compare = (Integer.toString(countryCode)).compareTo(Integer.toString(otherNumber.countryCode));
  51.  
  52.     if (compare == 0){
  53.       return (String.valueOf(localNumber)).compareTo(String.valueOf(otherNumber.localNumber));
  54.     }
  55.     return compare;
  56.   }
  57. }
  58.  
  59. abstract class TelephoneEntry
  60. {
  61.   Address address;
  62.  
  63.   abstract Address getAddress();
  64.   abstract String description();
  65. }
  66.  
  67. class Address
  68. {
  69.   String city;
  70.   String street;
  71.   TelephoneNumber teleNumber;
  72.  
  73.   public Address(String city, String street, TelephoneNumber teleNumber)
  74.   {
  75.     this.city = city;
  76.     this.street = street;
  77.     this.teleNumber = teleNumber;
  78.   }
  79.  
  80.   public String getCity()
  81.   {
  82.     return city;
  83.   }
  84.  
  85.   public String getStreet()
  86.   {
  87.     return street;
  88.   }
  89.  
  90.   public TelephoneNumber getTeleNumber()
  91.   {
  92.     return teleNumber;
  93.   }
  94. }
  95.  
  96. class Company extends TelephoneEntry
  97. {
  98.   String name;
  99.   Address address;
  100.  
  101.   public Company(String name, Address address)
  102.   {
  103.     this.name = name;
  104.     this.address = address;
  105.   }
  106.  
  107.   public String description()
  108.   {
  109.     return (name + " " + address.getCity() + " " + address.getStreet());
  110.   }
  111.  
  112.   public Address getAddress()
  113.   {
  114.       return address;
  115.   }
  116. }
  117.  
  118. class Person extends TelephoneEntry
  119. {
  120.   String name;
  121.   String lastName;
  122.   Address address;
  123.  
  124.   public Person(String name, String lastName, Address address)
  125.   {
  126.     this.name = name;
  127.     this.lastName = lastName;
  128.     this.address = address;
  129.   }
  130.  
  131.   public String description()
  132.   {
  133.     return (name + " " + lastName + " " + address.getCity() + " " + address.getStreet());
  134.   }
  135.  
  136.   public Address getAddress()
  137.   {
  138.       return address;
  139.   }
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top