Guest User

Untitled

a guest
Apr 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.92 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.lang.Comparable;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.PrintStream;
  8. import java.io.FileReader;
  9. import java.util.Collections;
  10. import java.util.Comparator;
  11.  
  12. class Htyo {
  13.    
  14.     public static void main(String[] args) throws IOException {
  15.         if(args.length != 1) {
  16.             System.exit(1);
  17.         }
  18.         String filename = args[0];
  19.         CityReader reader = new CityReader(filename);
  20.         reader.read();
  21.         CityPrinter printer = new CityPrinter(reader.getCities());
  22.         printer.printByName(System.out);
  23.         printer.printByPop(System.out);
  24.         printer.print(System.out);
  25.     }
  26. }
  27.  
  28. class City {
  29.     private String name;
  30.     private int pop;
  31.     private int cityLength;
  32.     private int popLength;
  33.    
  34.     public void setName(String name) {
  35.         this.name = name;
  36.     }
  37.    
  38.     public City withName(String name) {
  39.         setName(name);
  40.         return this;
  41.     }
  42.    
  43.     public String getName() {
  44.         return name;
  45.     }
  46.    
  47.     public void setPop(int pop) {
  48.         this.pop = pop;
  49.     }
  50.    
  51.     public City withPop(int pop) {
  52.         setPop(pop);
  53.         return this;
  54.     }
  55.    
  56.     public int getPop() {
  57.         return pop;
  58.     }
  59.    
  60.     public void setPopLength(int popLength) {
  61.         this.popLength = popLength;
  62.     }
  63.    
  64.     public City withPopLength(int popLength) {
  65.         setPopLength(popLength);
  66.         return this;
  67.     }
  68.    
  69.     public int getPopLength() {
  70.         return popLength;
  71.     }
  72.    
  73.     public void setCityLength(int cityLength) {
  74.         this.cityLength = cityLength;
  75.     }
  76.    
  77.     public City withCityLength(int cityLength) {
  78.         setCityLength(cityLength);
  79.         return this;
  80.     }
  81.    
  82.     public int getCityLength() {
  83.         return cityLength;
  84.     }
  85.    
  86.     public String toString() {
  87.         int stars = pop / 10000;
  88.         StringBuilder starStr = new StringBuilder();
  89.         for(int i = 0; i < stars; ++i) {
  90.             starStr.append("*");
  91.             if(starStr.length() >= 40) {
  92.                 starStr.append("...(");
  93.                 starStr.append(stars);
  94.                 starStr.append(")");
  95.                 break;
  96.             }
  97.         }
  98.         return String.format("%1$-" + cityLength + "s: ", name) + starStr.toString();
  99.     }
  100.    
  101.     public boolean equals(Object other) {
  102.         if(other instanceof City) {
  103.             City c = (City) other;
  104.             return name.equals(c.getName()) && pop == c.getPop();
  105.         }
  106.         return false;
  107.     }
  108. }
  109.  
  110. class CityReader {
  111.     private List<City> cities;
  112.     private BufferedReader in;
  113.     public CityReader(String filename) throws IOException {
  114.         cities = new ArrayList<City>();
  115.         in = new BufferedReader(new FileReader(filename));
  116.     }
  117.    
  118.     public void read() throws IOException {
  119.         String line = in.readLine();
  120.         while(line != null) {
  121.             String[] parts = line.split(", ");
  122.             cities.add(new City().withName(parts[0]).withPop(Integer.parseInt(parts[1])));
  123.             line = in.readLine();
  124.         }
  125.     }
  126.    
  127.     public List<City> getCities() {
  128.         return cities;
  129.     }
  130. }
  131.  
  132. class CityPrinter {
  133.     List<City> cities;
  134.     int maxNameLength;
  135.    
  136.     public CityPrinter(List<City> cities) {
  137.         this.cities = cities;
  138.         maxNameLength = 0;
  139.         for(City city : cities) {
  140.             if(city.getName().length() > maxNameLength) {
  141.                 maxNameLength = city.getName().length();
  142.             }
  143.         }
  144.     }
  145.    
  146.     private void printCity(City city, PrintStream out) {
  147.         out.println(city.withCityLength(maxNameLength + 2).withPopLength(40));
  148.     }
  149.    
  150.     public void print(PrintStream out) {
  151.        
  152.         for(City city : cities) {
  153.             printCity(city, out);
  154.         }
  155.     }
  156.    
  157.     public void printByName(PrintStream out) {
  158.         List<City> temp = new ArrayList<City>(cities);
  159.         Collections.sort(temp, new Comparator<City>() {
  160.                 public int compare(City c1, City c2) {
  161.                     return c1.getName().compareTo(c2.getName());
  162.                 }
  163.                 public boolean equals(City c1, City c2) {
  164.                     return c1.equals(c2);
  165.                 }
  166.         });
  167.         for(City city : temp) {
  168.             printCity(city, out);
  169.         }
  170.     }
  171.    
  172.     public void printByPop(PrintStream out) {
  173.         List<City> temp = new ArrayList<City>(cities);
  174.         Collections.sort(temp, new Comparator<City>() {
  175.                 public int compare(City c1, City c2) {
  176.                     return c2.getPop() - c1.getPop();
  177.                 }
  178.                 public boolean equals(City c1, City c2) {
  179.                     return c1.equals(c2);
  180.                 }
  181.         });
  182.         for(City city : temp) {
  183.             printCity(city, out);
  184.         }
  185.     }
  186. }
Add Comment
Please, Sign In to add comment