Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package model;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Comparator;
  11.  
  12.  
  13. /**
  14.  *
  15.  * @author mga
  16.  */
  17. public class City implements Comparable <City> {
  18.     private final int id;
  19.     private String cityName;
  20.     private String country;
  21.     private List<YearData> yearDataCollection;
  22.    
  23.      
  24.     private static int lastIdAllocated = 0;
  25.      
  26.     static final char EOLN='\n';      
  27.     static final String QUOTE="\"";
  28.     private ArrayList<Object> CityNameComparator;
  29.  
  30.     public City() {
  31.         this.id = ++lastIdAllocated;
  32.         this.cityName = "TBC";
  33.         this.country = "TBC";
  34.         this.yearDataCollection = new ArrayList<>();  
  35.         this.CityNameComparator=new ArrayList<>();
  36.     }
  37.  
  38.     public City(String cityName, String country) {
  39.         this.id = ++lastIdAllocated;
  40.         this.cityName = cityName;
  41.         this.country = country;
  42.         this.yearDataCollection = new ArrayList<>();
  43.     }
  44.  
  45.     public City(String cityName, String country, List<YearData> yearDataCollection) {
  46.         this.id = ++lastIdAllocated;        
  47.         this.cityName = cityName;
  48.         this.country = country;
  49.         this.yearDataCollection = yearDataCollection;
  50.     }
  51.  
  52.     public City(int id, String cityName, String country, List<YearData> yearDataCollection) {
  53.         this.id = id;
  54.         this.cityName = cityName;
  55.         this.country = country;
  56.         this.yearDataCollection = yearDataCollection;
  57.         if (id > City.lastIdAllocated)
  58.             City.lastIdAllocated = id;            
  59.     }
  60.  
  61.    public City (int id, String cityName, String country)
  62.    {
  63.        this.id=id;
  64.        this.cityName=cityName;
  65.        this.country=country;
  66.    }
  67.    
  68.    
  69.     /**
  70.      * @return the id
  71.      */
  72.     public int getId() {
  73.         return this.id;
  74.     }    
  75.      public String getCityName()
  76.     {
  77.         return this.cityName;
  78.     }
  79.      
  80.     public String getCountry(){
  81.         return this.country;
  82.     }
  83.      
  84.    
  85.      
  86.    public void setCityName (String cityName){
  87.        this.cityName=cityName;
  88.    }
  89.      
  90.    public void setCountry (String country)
  91.    {
  92.        this.country=country;
  93.    }
  94.    
  95.      public List<YearData> getyearDataCollection() {
  96.         return this.yearDataCollection;
  97.     }
  98.    
  99.    public void setYearDataCollection (List<YearData> yearDataCollection)
  100.            {
  101.                this.yearDataCollection=yearDataCollection;
  102.            }
  103.            
  104.      
  105.    
  106.    
  107.           public boolean addYearData( YearData yearData ) {
  108.       yearDataCollection.add( yearData ); //yearData is added to the end of the YearDataCollection list.
  109.           System.out.println("contents:"+yearData);
  110.           System.out.println("contents2:"+yearDataCollection);
  111.            return true;
  112.      
  113.    }
  114.  
  115.      
  116.     @Override
  117.     public String toString() {
  118.        
  119.          String str="";
  120.         int i=1;
  121.         for (YearData yeardata:yearDataCollection) {
  122.             str += Integer.toString(i++) + ": " +yeardata + "\n";
  123.         }        
  124.        
  125.         return "\nCity Id: " +Integer.toString(this.id)
  126.                 + " - City Name: " + cityName +            
  127.                 " - Country: " + country + "\nData: " + yearDataCollection + "\n"+str;
  128.     }
  129.      
  130.      
  131.      
  132.     public String toString(char delimiter) {
  133.          
  134.          
  135.         String output= this.id+delimiter
  136.                       +QUOTE+this.cityName+QUOTE+delimiter
  137.                 +this.country+QUOTE+delimiter+Integer.toString(this.yearDataCollection.hashCode());  
  138.         for (YearData str: this.yearDataCollection)
  139.          
  140.             output+=delimiter+QUOTE+str+QUOTE;
  141.          
  142.                 output+=EOLN;
  143.                
  144.                
  145.         return output;
  146.                  
  147.      
  148.      
  149. }
  150.    
  151.    
  152.        
  153.     @Override
  154.     public int hashCode()
  155.     {
  156.         return getId()*31+getCityName().hashCode()*31+ getCountry().hashCode()*31+getyearDataCollection().hashCode()*31;
  157.     }
  158.          
  159.      @Override
  160.     public boolean equals(Object o) {
  161.         if (o instanceof City) {
  162.             City c = (City)o;
  163.             return  c.getId() == getId() &&
  164.                     c.getCityName().equals(getCityName()) &&
  165.                     c.getCountry().equals(getCountry()) &&
  166.                     c.getyearDataCollection().equals(getyearDataCollection());
  167.                    
  168.         } else {
  169.             return false;
  170.         }
  171.     }
  172.      
  173.     @Override
  174.     public int compareTo(City compareCity) {
  175.      
  176.         int Id = ((City) compareCity).getId();
  177.                  
  178.                  
  179.          
  180.         //ascending order
  181.         return this.id - Id;
  182.                  
  183.          
  184.         //descending order
  185.         //return - this.id;
  186.          
  187.     }    
  188.  
  189.     // Methods required: getters (done), setters(done), add(done), hashCode (done), equals (done), compareTo (done), comparator (done)
  190.  
  191.      
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement